Introduction
In this article, you will learn how to update documents permission in SharePoint Document Library, using C# Client-Side Object Model (CSOM). This will be applicable for all SharePoint On premise platforms.
Prerequisite
- Create a document library in the site/subsite.
- Add the documents to the document library.
- Check the default permission, assigned to the each document. Select document -> in header Files menu -> Click “Shared with” under Manage group -> Click “ADVANCE” -> List of permissions will appear.
Update Permission using C# CSOM
The following section explains the flow for updating documents permission in SharePoint document library.
- Add the references to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.
- Initialize the client context object with the site URL.
- ClientContext ctx = new ClientContext("https://SiteURL.com");
- If you are trying to access SharePoint site, you need to setup the site credentials with the credentials parameter and get it set to the client context.
- ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
- ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891", "Password");
- Gets the document library, using GetByTitle method.
- var list = ctx.Web.Lists.GetByTitle("TestDocLibrary");
- Create CamlQuery to filter the documents. In the code, given below, the user will get the first 1000 documents from the document library. Similarly, we can filter the documents by writing the CamlQuery.
- var query = CamlQuery.CreateAllItemsQuery(1000);
- Get the items, using GetItems method.
- var result = list.GetItems(query);
- Load and execute the query.
- ctx.Load(result);
- ctx.ExecuteQuery();
- After getting the items to iterate through for each loop, initialize RoleDefinitionBindingCollection object with the client context.
- Get the role by GetByName method.
- Get the user or custom claim, using EnsureUser method.
- Break the role set for the item, using BreakRoleInheritance method.
- Add the role created for the user or custom claim to the item.
- Load and execute the query.
- foreach(ListItem item in result)
- {
- RoleDefinitionBindingCollection rd = new RoleDefinitionBindingCollection(ctx);
- rd.Add(ctx.Web.RoleDefinitions.GetByName("Read"));
- Principal user = ctx.Web.EnsureUser("Domain\\ID12345");
- item.BreakRoleInheritance(false, false);
- item.RoleAssignments.Add(user, rd);
- item.Update();
- ctx.ExecuteQuery();
- }
Thus, the permission is set to the documents. To check the permissions of the items, please follow the steps, given below:
- Select the documents to check for the permission.
- In the ribbon, click “Shared with” under Manage group in Edit tab.
- Click on Advanced, shown in Share With popup.
- Permission set for the documents/items are shown.
Summary
Thus, you have learned, how to update documents permission in SharePoint document library, using C# Client-Side Object Model (CSOM).