Forum Discussion
Raj4100
Sep 24, 2019Copper Contributor
Not able to connect SharePoint online through SharePoint CSOM package.
I am trying to connect to SharePoint online using SharePoint CSOM package in C#. I am using a service account to get access. When I tried to logon to site directly through Website, I can connect. But...
Vikram_Samal
Sep 24, 2019MCT
Raj4100 Please find the below code make sure you add the following package as well
Spoiler
using (ClientContext clientContext = new ClientContext("https://youdomain.sharepoint.com/sites/test")
{
clientContext.Credentials = new SharePointOnlineCredentials("vsamal@yourdomain.onmicrosoft.com", GetSPOSecureStringPassword("YourPassword"));
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
Console.WriteLine("Web site name " + web.Title);
}
{
clientContext.Credentials = new SharePointOnlineCredentials("vsamal@yourdomain.onmicrosoft.com", GetSPOSecureStringPassword("YourPassword"));
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
Console.WriteLine("Web site name " + web.Title);
}
An you will need one more function which will convert your text password to secure password:
Spoiler
private static SecureString GetSPOSecureStringPassword(string spoAccPwd)
{
try
{
SecureString secureString = new SecureString();
foreach (char c in spoAccPwd)
{
secureString.AppendChar(c);
}
return secureString;
}
catch (Exception ex)
{
Console.WriteLine("Got error Reading the Password. Error: " + ex.Message);
throw ex;
}
}
{
try
{
SecureString secureString = new SecureString();
foreach (char c in spoAccPwd)
{
secureString.AppendChar(c);
}
return secureString;
}
catch (Exception ex)
{
Console.WriteLine("Got error Reading the Password. Error: " + ex.Message);
throw ex;
}
}