Update Documents Permission In SharePoint Document Library Using C# Client-Side Object Model (CSOM)

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

  1. Create a document library in the site/subsite.
  2. Add the documents to the document library.
  3. 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.

  1. Add the references to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

  2. Initialize the client context object with the site URL.
    1. ClientContext ctx = new ClientContext("https://SiteURL.com"); 
  3. 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.
    1. ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;   
    2. ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891""Password");   
  4. Gets the document library, using GetByTitle method.
    1. var list = ctx.Web.Lists.GetByTitle("TestDocLibrary");   
  5. 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.
    1. var query = CamlQuery.CreateAllItemsQuery(1000);   
  6. Get the items, using GetItems method.
    1. var result = list.GetItems(query);   
  7. Load and execute the query.
    1. ctx.Load(result);   
    2. ctx.ExecuteQuery();   
  8. After getting the items to iterate through for each loop, initialize RoleDefinitionBindingCollection object with the client context.
  9. Get the role by GetByName method.
  10. Get the user or custom claim, using EnsureUser method.
  11. Break the role set for the item, using BreakRoleInheritance method.
  12. Add the role created for the user or custom claim to the item.
  13. Load and execute the query.
    1. foreach(ListItem item in result)  
    2. {  
    3.     RoleDefinitionBindingCollection rd = new RoleDefinitionBindingCollection(ctx);  
    4.     rd.Add(ctx.Web.RoleDefinitions.GetByName("Read"));  
    5.     Principal user = ctx.Web.EnsureUser("Domain\\ID12345");  
    6.     item.BreakRoleInheritance(falsefalse);  
    7.     item.RoleAssignments.Add(user, rd);  
    8.     item.Update();  
    9.     ctx.ExecuteQuery();  
    10. }  

Thus, the permission is set to the documents. To check the permissions of the items, please follow the steps, given below:

  1. Select the documents to check for the permission.

    documents

  2. In the ribbon, click “Shared with” under Manage group in Edit tab.

    Shared with
  3. Click on Advanced, shown in Share With popup.

    ADVANCED

  4. Permission set for the documents/items are shown.

    Permission

Summary

Thus, you have learned, how to update documents permission in SharePoint document library, using C# Client-Side Object Model (CSOM).