SharePoint 2103 Check user permission on List

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.