Advertisement

Responsive Advertisement

Connect to SharePoint Online/Office 365 sites using CSOM

SharePoint Client Side Object Model (CSOM) was first introduced to use build client applications and enable us to access SharePoint Sites that are hosted outside without using web services. SharePoint Online Client Side Object Model (CSOM) allows developers to interact with SharePoint online objects like web, list, library, and fields.

 The SharePoint Online Client Components SDK can be used to enable development with SharePoint Online.
 Notice that we do recommend using rather NuGet packages than installing CSOM assemblies to GAC. https://www.microsoft.com/en-us/download/details.aspx?id=42038

Here is the sample script to connect to the SharePoint online using Console solution in Visual studio 2013 Environment.

 Use this below reference in your code .

  1. using Microsoft.SharePoint.Client; 
  2. using System.Security; Code Snippet:

// Comment

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Net;  
 using System.Security;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace ConnectSharePointOnline  
 {  
   /// <summary>  
   /// Sample solution that demonstrates working with SharePoint Online Connections.  
   /// </summary>  
   class Program  
   {  
     /// <summary>  
     /// Main entry-point for the application  
     /// </summary>  
     /// <param name="args"></param>  
     static void Main(string[] args)  
     {  
       // Request Office365 site from the user  
       string siteUrl = "https//SharePointOnlineSite.micorosoft.com";  
       /* Prompt for Credentials */  
       Console.WriteLine("Enter Credentials for {0}", siteUrl);  
       string userName = GetUserName();  
       SecureString pwd = GetPassword();  
       if (string.IsNullOrEmpty(userName) || (pwd == null))  
         return;  
       ClientContext ctx = new ClientContext(siteUrl);  
       ctx.AuthenticationMode = ClientAuthenticationMode.Default;  
       ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);  
       try  
       {  
         Web web = ctx.Web;  
         ctx.Load(web);  
         ctx.ExecuteQuery();  
       }  
       catch (Exception ex)  
       {  
         Console.ForegroundColor = ConsoleColor.Red;  
         Console.WriteLine(string.Format("Exception!"), ex.ToString());  
         Console.WriteLine("Press any key to continue.");  
         Console.Read();  
         throw;  
       }  
     }  
     /// <summary>  
     /// Helper to return the password  
     /// </summary>  
     /// <returns>SecureString representing the password</returns>  
     public static SecureString GetPassword()  
     {  
       SecureString sStrPwd = new SecureString();  
       try  
       {  
         Console.Write("SharePoint Password: ");  
         for (ConsoleKeyInfo keyInfo = Console.ReadKey(true);
             keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
        {  
           if (keyInfo.Key == ConsoleKey.Backspace)  
           {  
             if (sStrPwd.Length > 0)  
             {  
               sStrPwd.RemoveAt(sStrPwd.Length - 1);  
               Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);  
               Console.Write(" ");  
               Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);  
             }  
           }  
           else if (keyInfo.Key != ConsoleKey.Enter)  
           {  
             Console.Write("*");  
             sStrPwd.AppendChar(keyInfo.KeyChar);  
           }  
         }  
         Console.WriteLine("");  
       }  
       catch (Exception e)  
       {  
         sStrPwd = null;  
         Console.WriteLine(e.Message);  
       }  
       return sStrPwd;  
     }  
     /// <summary>  
     /// Helper to return the User name.  
     /// </summary>  
     /// <returns></returns>  
     public static string GetUserName()  
     {  
       string strUserName = string.Empty;  
       try  
       {  
         Console.Write("SharePoint Username: ");  
         strUserName = Console.ReadLine();  
       }  
       catch (Exception e)  
       {  
         Console.WriteLine(e.Message);  
         strUserName = string.Empty;  
       }  
       return strUserName;  
     }  
   }  
 }  

Post a Comment

0 Comments