1

I am trying to create a user group from a item in a SharePoint list. The list has a column person that is typed as person or group. In the event handler I am trying to take this column and create a group from the people in that list item. I know how to create a group using the method

public void EnsureSPGroup(string spGroup, string spGroupOwner, string SiteURL)

And I know how to add users with

public void AddUser(String userLoginName, int groupID, string SiteURL)

What I'm not sure how to do is create this group with users from the list item.

2 Answers 2

1

Try this code to loop the person column:

SPFieldUserValueCollection objUserFieldValueCol = new SPFieldUserValueCollection(web, listitem["UserGroupField"].ToString());
for (int i = 0; i < objUserFieldValueCol.Count; i++)
{
    SPFieldUserValue singlevalue = objUserFieldValueCol[i];
    if (singlevalue.User == null) // value is a SharePoint group if User is null
    {
        SPGroup group = web.Groups[singlevalue.LookupValue];
        foreach (SPUser user in group.Users)
        {
            //do stuff with the user
        }
    }
    else // singlevalue.User value is a user
    {
        //do stuff for the singlevalue.User
    }
}

Source: How to retrieve Users and Groups seperately from a "Person or Group" column via C# code?

1
  • I'm not trying to loop the whole column just create the group from one item in the list. However, I think SPFieldUserValuCollection should lead me to the answer.
    – belmer1416
    Jun 26, 2017 at 18:54
0

You can use sharepoint designer site workflows to create sp group. Need to write custom activity Please refer this http://www.jrjlee.com/2014/01/custom-workflow-activity-for-creating_23.html?m=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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