using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;
using System.Security;
using System.Web;
using System.Net;
namespace GetContextExample
{
class Program
{
//sharepoint online user id
private const string spoUserName = "[email protected]";
//network user id requires System.Net namespace
//private const string netUserName = "abc\\xyz";
private const string password = "password";
private const string siteURL = "your site url";
static void Main(string[] args)
{
//authenticating user
ClientContext context = Auth();
if (context != null)
{
// place your code here, which uses context
}
}
public static ClientContext Auth()
{
ClientContext context = new ClientContext(siteURL);
Web web = context.Web;
SecureString pwd = new SecureString();
foreach (char c in password.ToCharArray()) pwd.AppendChar(c);
// for network credentials
//context.AuthenticationMode = ClientAuthenticationMode.Default;
//context.Credentials = new NetworkCredential(netUserName, pwd);
context.Credentials = new SharePointOnlineCredentials(spoUserName, pwd);
try
{
context.Load(web);
context.ExecuteQuery();
Console.WriteLine("Authentication Succesful for " + web.Title + " site");
return context;
}
catch (Exception e)
{
Console.WriteLine("Authentication failed.");
return null;
}
}
}
}This code for how to create Create Client Context CSOM in .NET.
