Tutorial :Change description of a SharePoint group



Question:

I'm trying to change the description of an existing SharePoint group which shouldn't be a really tough job but unfortunately it doesn't work as expected. After running the corresponding method the group's description stays the same as before.

Here is the code I use to change the description:

private void ResetGroupDescription(SPWeb rootWeb, string groupName, string groupDescription)  {                  rootWeb.AllowUnsafeUpdates = true;        SPGroup group = rootWeb.SiteGroups[groupName];      group.Description = groupDescription;      group.Update();        rootWeb.Update();      rootWeb.AllowUnsafeUpdates = false;        // Code-Update      SPGroup checkGroup = rootWeb.SiteGroups[groupName];      Trace.WriteLine(checkGroup.Description);  }  

UPDATE:

I added some more lines of code to my method and fetch the group I altered before one more time to check its description property. This shows me that the group's description was changed as expected. But when I try to validate this by checking the group's description on the group settings page (UI) of the corresponding site collection, the group's description is still the old value.

UPDATE 2:

So I did some more testing on that issue and try to change the title of the group instead of the its description. Strange to say, but this one works perfect. The renaming of the group is shown in the UI immediately.


Solution:1

I found a solution in another forum. The description shown in the UI is stored within the UserInformationList. The following code changes the group's description.

SPGroup g = web.SiteGroups["GroupName"];  SPFieldMultiLineText text = (SPFieldMultiLineText)web.SiteUserInfoList.Fields[SPBuiltInFieldId.Notes];  SPListItem groupItem = web.SiteUserInfoList.GetItemById(g.ID);  groupItem[text.InternalName]= groupDescription;  groupItem.Update();     


Solution:2

2 things:

Do you want to change the description or the name of the group? There's a Name and a Description property....

Have you tried running it as a different user? i.e. SPSecurity.RunWithElevatedPrivileges.


Solution:3

SPWeb.Groups will allow you to pull out only the groups which have some / any kind of permissions defined within the site.

SPWeb.SiteGroups will pull out all the cross-site groups irrespective of any permission defined.


Solution:4

That code should work.

If I compare it to code that I use the only difference is that I use code like this to fetch the group. Shouldn´t be a difference but maybe...

    foreach (SPGroup group in web.SiteGroups)      {          if (group.Name.Equals(groupName, StringComparison.OrdinalIgnoreCase))          {              return group;          }      }  


Solution:5

Make sure there isn't anything being cached anywhere - unknown caching has been known to drive perfectly good developers to insanity.


Solution:6

Maybe you need to AllowUnsafeUpdates on the SPWeb or SPSite? This is typically necessary if you are not doing a POST, but a GET.


Solution:7

You can try with CSOM (Client Side Object Model). With below code I am able to update the Group name, description and any thing else you need.

using (ClientContext context = new ClientContext("Your Link goes here."))  {      context.Credentials = new System.Net.NetworkCredential("Your User Name", "Your Password", "Domain name");      GroupCollection groupCollection = context.Web.SiteGroups;      context.Load(groupCollection, groups => groups.Include(group => group.Title, group => group.Id, group => group.Users));      context.ExecuteQuery();      Group myGroup = groupCollection.GetByName(OldGroupNameTB.Text);      context.Load(myGroup);      context.ExecuteQuery();        List myGroupList = context.Web.SiteUserInfoList;      context.Load(myGroupList.Fields);      context.ExecuteQuery();        FieldMultiLineText text = (FieldMultiLineText)context.Web.SiteUserInfoList.Fields[7];      ListItem groupItem = context.Web.SiteUserInfoList.GetItemById(myGroup.Id);        myGroup.Title = NewGroupNameTB.Text;      groupItem[text.InternalName] = GroupDescrioptionTB.Text;        groupItem.Update();      myGroup.Update();      context.ExecuteQuery();  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »