SPUser Group Management



SPUser Group Management


How to Create\Update\Retrieve\Delete a Personal UserGroup for a User in SharePoint by using APIs



Personal User Groups can be Created, Updated, Retrieved and Deleted by using SharePoint APIs as given below...


//Create Group
SPUser spUser = mySite.Owner;
SPMember spMemebr = mySite.RootWeb.Users["My User Name"];
spUser.Groups.Add("GroupName", spMemebr, spUser, "This is Group Description");
spUser.Update();


//Update Group
SPUser spUser = mySite.Owner;
SPGroup spGroup = spUser.Groups["GroupName"];
spGroup.Name = "newGroupName";
spGroup.Update();


//Retrieve Group
SPUser spUser = mySite.Owner;
SPGroupCollection spGroupCollection = spUser.Groups;


foreach (SPGroup spGroup in groupCollection)
{
//Store all groups in internal collection
}

//Delete Group
SPUser spUser = mySite.Owner;
spUser.Groups.Remove("newGroupName");
spUser.Update();