I. Requirements: Check user's permission to SharePoint list
The code is as follows:
1 Private Static stringGetlistpermission (SPList list,stringloginName)2 {3 stringPerstr =string. Empty;4Spsecurity.runwithelevatedprivileges (() =5 {6 Try7 {8SPUser user =list. Parentweb.users[loginname];9SPRoleAssignment roleassignment =list. Roleassignments.getassignmentbyprincipal (user);TenSproledefinitionbindingcollection Defcoll =roleassignment.roledefinitionbindings; One foreach(Sproledefinition roledefinchdefcoll) A { -Perstr + = Roledef.name +";"; - } the } - Catch(Exception) - { -Logger. Debug ("Get user permission by list. Getusereffectivepermissioninfo method, list title: {0}, LoginName: {1}.", List. Title, loginName); + Try - { +Sppermissioninfo Permissioninfo =list. Getusereffectivepermissioninfo (loginName); A varRoleassignments =permissioninfo.roleassignments; at foreach(SPRoleAssignment roleassignmentinchroleassignments) - { -Sproledefinitionbindingcollection Roledefcoll =roleassignment.roledefinitionbindings; - foreach(Sproledefinition roledefinchroledefcoll) - { -Perstr + = Roledef.name +";"; in } - } to } + Catch(Exception ex) - { theLogger. Error ("An error occurred while getting permission by list. Getusereffectivepermissioninfo method, list title: {0}, LoginName: {1}, exception; {2}.", * list. Title, LoginName, ex. ToString ()); $ }Panax Notoginseng } - }); the returnPerstr; +}
View Code
Note: The code in catch is check, when user is a member in AD group, but does not exist in the Web userinformation list alone, if you get the user directly SPRoleAssignment, the "Index is out of range" is thrown, so the user can
List. Getusereffectivepermissioninfo (LoginName); To get Sppermissioninfo, and then get the user's sproledefinition, some readers will ask, why not directly from the catch method, so whether the user is only in ad group will not throw an exception
can be correctly obtained to sproledefinition, in fact, is possible, the reason for this is the efficiency problem.
Second, demand: Set permission to List
The code is as follows:
1 Private Static voidSetlibpermission (SPList list,BOOLisread)2 {3 Try4 {5Spsecurity.runwithelevatedprivileges (() =6 {7 BOOLHasunique =list. hasuniqueroleassignments;8List. Parentweb.allowunsafeupdates =true;9 if(!hasunique)Ten { OneList. Breakroleinheritance (false); A list. Update (); - } - Try the { -SPUser user =list. Parentweb.ensureuser (Userinfo.key); -Sproledefinitioncollection Objdeficoll =list. parentweb.roledefinitions; -SPRoleAssignment objroleassign =Newsproleassignment (user); +Sproledefinition roledefination =NULL; - if(isread) + { ARoledefination =Objdeficoll.getbytype (sproletype.reader); at } - Else - { -Roledefination =Objdeficoll.getbytype (sproletype.contributor); - } - ObjRoleAssign.RoleDefinitionBindings.Add (roledefination); in list. Roleassignments.add (objroleassign); - } to Catch(Exception ex) + { - the } * list. Update (); $List. Parentweb.allowunsafeupdates =false;Panax Notoginseng }); - the } + Catch(Exception ex) A { the + } -}
View Code
Note: To assign permissions to the list, you need to break the inheritance, depending on the actual needs
The Userinfo.key in the code is LoginName
List. Parentweb.ensureuser (Userinfo.key); Saves the user to the user information list
SharePoint 2103 Check user permission on List