Office 365 How to Connect SharePoint Online site using CSOM

Microsoft introduced a new functionality to interact with CSOM. With any interactions with the release, you can call in a class called "SharePointOnlineCredentials" this will handle any authentication you should need to do.

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

This is a simple way to connect Sharepoint online sites using this method,

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 O365SPCreateList  
  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 ClientContext=GetonlineContext();  
  63.             Web web = clientContext.Web;   
  64.             ListCreationInformation creationInfo = new ListCreationInformation();   
  65.             creationInfo.Title = "CustomList";   
  66.             creationInfo.TemplateType = (int)ListTemplateType.GenricList.;   
  67.             List list = web.Lists.Add(creationInfo);   
  68.             list.Description = "New Description";   
  69.             list.Update();   
  70.             context.ExecuteQuery();   
  71.             
  72.         }  
  73.     }  
  74. }   
Run this code and check your SharePoint Site.New Site List created Successfully.

Was my blog helpful?

If so, please let us know at the bottom of this page. If not, let us know what was confusing or missing and I’ll use your feedback to double-check the facts, add info and update this blog.
Ebook Download
View all
Learn
View all