Office365 SharePoint Online Add User To Role Using CSOM

Microsoft introduced a new functionality to interact with CSOM. It has SharePointOnlineCredentials, which will handle any authentication; you should need to do.

This class is included in version 15 of Microsoft.SharePoint.Client dll.

Method

Using this method, it is a simple way to connect to Sharepoint Online sites.

var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);

Configure your credentials in a separate configuration class.
  1. private class Configuration  
  2.         {  
  3.   public static string ServiceSiteUrl = "https:// https://gowthamr.sharepoint.com";  
  4.   public static string ServiceUserName = "[email protected]";  
  5.   public static string ServicePassword = "xxxxxxxxxx";  
  6.         }  
  7.           
  8.           
  9. //Create new Context with interating SharePoint Online sites,  
  10. static ClientContext GetonlineContext()  
  11.         {  
  12.             var securePassword = new SecureString();  
  13.             foreach (char c in Configuration.ServicePassword)  
  14.             {  
  15.                 securePassword.AppendChar(c);  
  16.             }  
  17.   
  18.             var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);  
  19.   
  20.             var context = new ClientContext(Configuration.ServiceSiteUrl);  
  21.             context.Credentials = onlineCredentials;  
  22.   
  23.             return context;  
  24.         }  
  25.   
  26. //Now we will see about Create A SharePoint List using CSOM method,  
  27. using System;  
  28. using System.Collections.Generic;  
  29. using System.Linq;  
  30. using System.Text;  
  31. using Microsoft.SharePoint.Client;  
  32.    
  33. namespace O365SPAddUsertoRole  
  34. {  
  35.     class Program  
  36.     {  
  37.       
  38.       
  39.      private class Configuration  
  40.         {  
  41.                 public static string ServiceSiteUrl = "https:// https://gowthamr.sharepoint.com";  
  42.                 public static string ServiceUserName = "[email protected]";  
  43.                 public static string ServicePassword = "xxxxxxxxxx";  
  44.         }  
  45.   
  46.                  static ClientContext GetonlineContext()  
  47.                           {  
  48.                                     var securePassword = new SecureString();  
  49.                                     foreach (char c in Configuration.ServicePassword)  
  50.                                     {  
  51.                                         securePassword.AppendChar(c);  
  52.                                     }  
  53.                                     var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);  
  54.                                     var context = new ClientContext(Configuration.ServiceSiteUrl);  
  55.                                     context.Credentials = onlineCredentials;  
  56.                                     return context;  
  57.                           }  
  58.   
  59.   
  60.         static void Main(string[] args)  
  61.         {  
  62.             var context=GetonlineContext();  
  63.             Web web = context.Web;   
  64.             Principal user = context.Web.SiteUsers.GetByLoginName(@"X");   
  65.             RoleDefinition readDef = context.Web.RoleDefinitions.GetByName("Read");   
  66.             RoleDefinitionBindingCollection roleDefCollection = new RoleDefinitionBindingCollection(context);   
  67.             roleDefCollection.Add(readDef);   
  68.             RoleAssignment newRoleAssignment = context.Web.RoleAssignments.Add(user, roleDefCollection);   
  69.             context.ExecuteQuery();     
  70.             
  71.         }  
  72.     }  
  73. }  
Run this code and check your SharePoint Site. New Site list is created successfully.
 
Was my blog helpful?

If the blog was helpful, please let us know at the bottom of this page. If you did not like the  blog, let us know what was confusing or missing. I’ll use your feedback to double-check the facts, add info and update this article.