Ads

Showing posts with label SharePoint Online. Show all posts
Showing posts with label SharePoint Online. Show all posts

Tuesday, 14 February 2017

Connect SharePoint online using CSOM from console application


Create new console application project in VS 2013
R-click on References in the Solution Explorer --> click on Manage NuGet Packages.
Search for Microsoft.SharePointOnline.CSOM and then click on Install

Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    namespace CSOMOffice365  
    {
        class Program  
        {
            static void Main(string[] args)  
          {
                string userName = "BlaBla.onmicrosoft.com";
                Console.WriteLine("Enter password.");
                SecureString password = GetPassword();  
                using(var clientContext = new ClientContext("SharePoint site URL"))  
                {
                    // SharePoint Online Credentials
                    clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
                    Web web = clientContext.Web;
                    clientContext.Load(web);
                    clientContext.ExecuteQuery();
                    Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);
                    Console.ReadLine();
                }
            }
            private static SecureString GetPassword()
          {
                ConsoleKeyInfo info;
                SecureString securePassword = new SecureString();
                do  
                {
                    info = Console.ReadKey(true);
                    if (info.Key != ConsoleKey.Enter)  
                    {
                        securePassword.AppendChar(info.KeyChar);
                    }
                }
                while (info.Key != ConsoleKey.Enter);
                return securePassword;
            }
        }
    }


Ads