Create a Role to User in SharePoint using CSOM

Steps

  • Open Visual Studio in your system
  • Select Console Applciation template and give as name .
  • Add a Microsoft.Cleint Assembly refrence file in right side refrence tab in visual studio.
  • Replace Program.cs with the source code file.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using Microsoft.SharePoint.Client;  
    7. namespace GowthamSamples   
    8. {  
    9.     class Program  
    10.     {  
    11.         static void Main(string[] args)  
    12.       {  
    13.             // ClientContext - Get the context for the SharePoint Site  
    14.             ClientContext clientContext = new ClientContext("http://gauti.sharepoint.com/sites/sp1/");  
    15.             BasePermissions Roleperm = new BasePermissions();  
    16.             Roleperm.Set(PermissionKind.CreateAlerts);  
    17.             Roleperm.Set(PermissionKind.ManageAlerts);  
    18.             RoleDefinitionCreationInformation RolecreationInfo = new RoleDefinitionCreationInformation();  
    19.             RolecreationInfo.BasePermissions = Roleperm;  
    20.             RolecreationInfo.Description = "create a Role for this user";  
    21.             RolecreationInfo.Name = "Manager Role";  
    22.             RolecreationInfo.Order = 0;  
    23.             RoleDefinition Roledef = context.Web.RoleDefinitions.Add(RolecreationInfo);  
    24.             context.ExecuteQuery();  
    25.         }  
    26.     }  
    27. }  
Thanks for reading my blogs.