views:

607

answers:

6

Hi,

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 immedatiely.

A: 

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;
        }
    }
Johan Leino
+2  A: 

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.

Colin
I'm trying to change the group's description not the title as I wrote in my post. I fixed this mistake. My code is part of a SPTimerJob which I run with elevated privileges, so this shouldn't be a problem.
Flo
Did you try debugging the method? See if the method actually goes through? If it's in a TimerJob you won't see any "readable" errors popping up (do you ever in SharePoint...)
Colin
Yes I stepped through my code in debug mode. The Description property of the SPGroup object is set to correctly.
Flo
A: 

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

Greg Hurlman
Mh, this might be a point to start further investigations. Thx.
Flo
A: 

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

Kirk Liemohn
As you can see in my code I've already set the AllowUnsafeUpdates property of the SPWeb object before and after I edit the groups. Now I also set it for the SPSite object, but this doesn't help either. As I've already mentioned, the changes are made to the database but are not displayed in the UI.
Flo
Duh. I didn't read close enough. My bad.Side note: I have noticed that if you do a RunWithElevatedPrivileges it _may_ be important to actually set AllowUnsafeUpdates on the SPContext.Current.Web/Site instead of the new one created. Makes sense when you think about it, I suppose.
Kirk Liemohn
RunWithElevatedPrivileges will reset the AllowUnsafeUpdates flag to false it seems (read that somewhere).
Colin
I think I said that already here, my code snippet is part of a TimerJob. The first line of code of its Execute method is "SPSecurity.RunWithElevatedPrivileges(..." and the group object is initial retrieved within this context, so that shouldn't be the problem. Beside this debugging showed me that the modification is transfered and persisted within the SharePoint database.
Flo
+1  A: 

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();
Flo
MagicAndi
A: 

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.

eliza sahoo