Question

I have a custom PeoplePicker which has SelectionSet = "User,SecGroup".

I want to add all entities within this PeoplePicker (which can be a SharePoint user, AD group/user) into a proper SPGroup.

How do I know that an entity is a SharePoint User or a AD user/group? How do I add set to the SPGroup?

Was it helpful?

Solution

Where is your code running? Web part, application page etc?

Regardless, you can probably do something similar to the following

try
{
    //first get the SPGroup you want to add users to
    SPGroup addToGroup = SPContext.Current.Web.AssociatedVisitorGroup;

    //now loop through each entity in your user picker
    foreach (PickerEntity entity in userPicker.ResolvedEntities)
    {
        //entity key will be the username
        string userName = entity.Key;

        //create a SPUser in the web
        SPUser user = SPContext.Current.Web.EnsureUser(userName);

        if (user.IsDomainGroup)
        {
            //user is an ad group
        }
        else
        {
            //user is an ad user
        }

        //add the user to the group
        addToGroup.AddUser(user);
    }
}
catch (UnauthorizedAccessException uaex)
{
    //handle any errors that occur when not enough rights to manage users
}
catch (Exception)
{
    //handle other errors
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top