Check drop down value with the sharePoint Group User In sharepoint 2010

Hi,

We have one requirement, When user selects any value from the Drop down we need to compare that value to the SharePoint Group.

If that User is there in the group then only user allow to add new item to the list otherwise we need to display message(i.e. U r not a correct person).

We used below code:

 public static bool IsUserMemberOfGroup(SPUser user, string grpname)
    {
        bool result = false;
        
        if (!String.IsNullOrEmpty(grpname) && user != null)
        {
            foreach (SPGroup group in user.Groups)
            {
                if (group.Name == grpname)
                {
                    // found it
                    result = true;
                    break;
                }
            }
        }

        return result;
    }

IsUserMemberOfGroup(ddlusers.selected value,"GrpName")

But it's showing error.

April 28th, 2015 1:24am

With regards to the error message: I'm assuming you are not using SPUser objects within the dropdown and that the first parameter you are passing is missing the . (period) between selected and value is just a typo. IsUserMemberOfGroup(ddlusers.selected value,"GrpName")

Assuming that, it may be because (for the first parameter of IsUserMemberOfGroup) you are trying to pass in a string value (ddlusers.selected.value) when it is expecting an SPUser object.

Two options (both requiring you to convert string to SPUser) are:

1. Before you call the function convert the string to an SPUser object then pass in the SPUser

2. Modify the first parameter of the function to be a string and do the conversion within the function (probably what I would do as more reusable).

To convert to an SPUser it depends on what is listed within the drop down.

If it is usernames then you can do something like: SPUser user = SPContext.Current.Web.EnsureUser(ddlusers.selected.value);

If it is something else (not usernames) then you would need to workout how to convert to SPUser.

A bit confused on what you are trying to achieve here - what if I select another user in the list that isn't me? Can I then add something to the list?

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 2:20am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics