SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from Nov 28 '11 at 21:00

This question came from our site for professional and enthusiast programmers.

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
Hi, thanks, Please move it to SharePoint SE site. – user217648 Nov 28 '11 at 20:17
up vote 4 down vote accepted

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

Regardless, you can probably do something similar to the following

    //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
            //user is an ad user

        //add the user to the group
catch (UnauthorizedAccessException uaex)
    //handle any errors that occur when not enough rights to manage users
catch (Exception)
    //handle other errors
share|improve this answer
Thank you so much. it is what I wanted. – Medes Nov 30 '11 at 9:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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