首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在moss中获取当前用户组?

如何在moss中获取当前用户组?
EN

Stack Overflow用户
提问于 2009-12-16 03:06:34
回答 1查看 1.3K关注 0票数 0

我希望你能解决我的问题。

我正在尝试使用事件处理程序为sharepoint列表和文档库设置项目级别的权限,但我无法执行此操作。帮助我这是我的代码。我需要根据登录用户.to所属的组为项目分配权限。我无法检索当前用户组。这是我的代码。

代码语言:javascript
复制
public override void ItemAdded(SPItemEventProperties properties)
{
  using (SPSite _site = new SPSite(properties.WebUrl))
  {
    using (SPWeb spWeb = _site.OpenWeb(properties.RelativeWebUrl))
    {
      SPUser currentUser = spWeb.CurrentUser;
      SPListItem listItem = properties.ListItem;

      listItem.BreakRoleInheritance(true);
      SPGroupCollection spgroup = currentUser.Groups;

      foreach (SPGroup group in spgroup)
      {
        SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);
        SPRoleDefinition roleDefinition = spWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);

        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

        listItem.RoleAssignments.Add(roleAssignment);

        spWeb.AllowUnsafeUpdates = true;

        listItem.Update();

        spWeb.AllowUnsafeUpdates = false;
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2009-12-16 08:44:25

试试这个:

代码语言:javascript
复制
public override void ItemAdded(SPItemEventProperties properties)
{
  // run the code impersonating the web application account, this works better than the regular RunWithElevatedPrivileges
  using (var site = new SPSite(properties.SiteId, properties.ListItem.ParentList.ParentWeb.Site.SystemAccount.UserToken))
  {
    using (var web = site.OpenWeb(properties.RelativeWebUrl))
    {
      web.AllowUnsafeUpdates = true;
      var item = web.Lists[properties.ListId].GetItemById(properties.ListItemId);

      item.BreakRoleInheritance(false);

      foreach (SPGroup group in web.CurrentUser.Groups)
      {
        var assignment = item.Web.RoleAssignments.GetAssignmentByPrincipal(group);
        var roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);

        assignment.RoleDefinitionBindings.Add(roleDefinition);
        item.RoleAssignments.Add(assignment);
      }

      DisableEventFiring();
      item.SystemUpdate(false);
      EnableEventFiring();
      web.AllowUnsafeUpdates = false;
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1909627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档