I want to access sharepoint list from my local machine VS2012 using CSOM but getting The remote server returned an error: (401) Unauthorized. error message
My Code:
- using Microsoft.SharePoint.Client;
- using System;
- using System.Security;
- using System.Configuration;
- namespace SP_Console
- {
- class Program
- {
- static void Main(string[] args)
- {
- ClientContext clientContext = new ClientContext("http://sphkcl.sharepoint.com/");
- clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
-
- Web web = clientContext.Web;
-
- ListCollection listColl = web.Lists;
-
- clientContext.Load(listColl);
- clientContext.ExecuteQuery();
-
- foreach (List list in listColl)
- {
-
- Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);
- }
- Console.ReadLine();
- }
- private static SecureString GetSPOSecureStringPassword()
- {
- try
- {
- var secureString = new SecureString();
- foreach (char c in ConfigurationManager.AppSettings["SPPass"])
- {
- secureString.AppendChar(c);
- }
- return secureString;
- }
- catch
- {
- throw;
- }
- }
- private static string GetSPOAccountName()
- {
- try
- {
- return ConfigurationManager.AppSettings["SPAccount"];
- }
- catch
- {
- throw;
- }
- }
- }
- }
I have added username and password in app.config.
- <appSettings>
- <add key="SPAccount" value="[email protected]"/>
- <add key="SPPass" value="*********"/>
- </appSettings>