• How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically


    Here is a piece of code (a function) to set Item Level Permission. You can use it as a Web Method in a custom Web Service. This method can be used from Applications outside of SharePoint, provided the user using this application has sufficient privilege to update lists/libraries etc.

        public string ItemPermission(string SitePath)

        {

            string ReturnVal = "";

            try

            {

                SPSite WebApp = new SPSite(SitePath);

                SPWeb Site = WebApp.OpenWeb();

                SPList list = Site.Lists["TestDocLib"];

                SPListItem item = list.Items[0];

                SPRoleDefinition RoleDefinition = Site.RoleDefinitions.GetByType(SPRoleType.Contributor);

                SPRoleAssignment RoleAssignment = new SPRoleAssignment("<domain>\\<user>", "email", "name", "notes");

                RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

                if(!item.HasUniqueRoleAssignments)

                {

                    item.BreakRoleInheritance(true);               

                }

                item.RoleAssignments.Add(RoleAssignment);

                item.Update();

            }

            catch (Exception ex)

            {

                ReturnVal += "Permission not set, reason: " + ex.Message;

            }

            return ReturnVal;

        }

    =========================================================

    预期在 SPSecurity.RunWithElevatedPrivileges 中得到操作权限提升的任何对象都必须是来之这个新的安全上下文创建的对象,在其内部引用外部创建的对象,还是没有权限操作


      SPSecurity.RunWithElevatedPrivileges(delegate
      {
      using (SPSite site = new SPSite(properties.SiteId))
      {
      using (SPWeb web = site.OpenWeb(properties.ListItem.ParentList.ParentWeb.ID))
      {
      web.AllowUnsafeUpdates = true;
      // Make sure referring to the new objec created under the evelvated security context
      // there seems to be some bug in web.Lists[properties.ListId].Items[properties.ListItemId] // IndexOutOfRange
      SPListItem item = web.Lists[properties.ListId].Items[properties.ListItem.UniqueId];
      item.BreakRoleInheritance(false);

      SPRoleDefinition readRoleDef = web.RoleDefinitions["Read"];
      SPRoleDefinition contributeRoleDef = web.RoleDefinitions["Contribute"];

      // the user creating this item have the Contribute permisioin level
      SPRoleAssignment roleAssOfCurrentUser = new SPRoleAssignment(web.AllUsers[properties.UserLoginName]);
      roleAssOfCurrentUser.RoleDefinitionBindings.Add(contributeRoleDef);
       
      // all the authenticated user can read
      SPRoleAssignment roleAssOfAllUser = new SPRoleAssignment(web.AllUsers["NT AUTHORITY\\Authenticated Users"]);
      roleAssOfAllUser.RoleDefinitionBindings.Add(readRoleDef);

      item.RoleAssignments.Add(roleAssOfCurrentUser);
      item.RoleAssignments.Add(roleAssOfAllUser);

      //properties.ListItem.SystemUpdate(); // NO NEED
      }
      }
      });

  • 相关阅读:
    ArrayList和LinkedList的区别
    线程的基本概念、线程的基本状态以及状态之间的关 系
    当一个线程进入一个对象的一个synchronized方法后, 其它线程是否可进入此对象的其它方法?
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.thinkplatform.dao.UserLogDao' available: expected at least 1 bean which qualifies as autowi
    IDEA设置热部署
    spring的核心组件及作用(一)
    解决Linux启动redis时出现提示权限不够问题
    Struts自动装配和四种放入Session作用域的方式
    Struts第一个案例搭建
    当List<String> list =new ArrayList<String>(20); 他会扩容多少次
  • 原文地址:https://www.cnblogs.com/icedog/p/1776896.html
Copyright © 2020-2023  润新知