8

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?

1
  • If you want we can push this over to the SharePoint SE site, you might get a better response there. If you'd like us to do that then flag a mod to action this. Also it would be helpful to post a snippet of code which would go a long way to helping others understand your problem. – Kev Nov 28 '11 at 18:01
4

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
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.