简体   繁体   English

更改SharePoint组的说明

[英]Change description of a SharePoint group

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. 我正在尝试更改现有SharePoint组的描述,这不应该是一项非常艰巨的工作,但不幸的是它不能按预期工作。 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. 但是当我尝试通过检查相应网站集的组设置页面(UI)上的组描述来验证这一点时,该组的描述仍然是旧值。

UPDATE 2: 更新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. 组的重命名立即显示在UI中。

I found a solution in another forum. 我在另一个论坛找到了解决方案。 The description shown in the UI is stored within the UserInformationList. UI中显示的描述存储在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();   

2 things: 2件事:

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? 您是否尝试以其他用户身份运行它? ie SPSecurity.RunWithElevatedPrivileges. 即SPSecurity.RunWithElevatedPrivileges。

SPWeb. 的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. 无论是否定义了任何权限,SPWeb.SiteGroups都将提取所有跨站点组。

You can try with CSOM (Client Side Object Model). 您可以尝试使用CSOM(客户端对象模型)。 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();
}

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;
        }
    }

确保没有任何东西被缓存在任何地方 - 众所周知,未知的缓存可以让完美的开发人员疯狂。

Maybe you need to AllowUnsafeUpdates on the SPWeb or SPSite? 也许您需要在SPWeb或SPSite上使用AllowUnsafeUpdates? This is typically necessary if you are not doing a POST, but a GET. 如果您没有进行POST,而是进行GET,这通常是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM