Kumar Sourabh

Kumar Sourabh

  • NA
  • 294
  • 1.1k

Not able to access Sharepoint O365 list from my Local System

Apr 24 2019 12:31 AM
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:
  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.Security;  
  4. using System.Configuration;  
  5. namespace SP_Console  
  6. {  
  7. class Program  
  8. {  
  9. static void Main(string[] args)  
  10. {  
  11. ClientContext clientContext = new ClientContext("http://sphkcl.sharepoint.com/");  
  12. clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());  
  13. // Get the SharePoint web  
  14. Web web = clientContext.Web;  
  15. // Get the SharePoint list collection for the web  
  16. ListCollection listColl = web.Lists;  
  17. // Retrieve the list collection properties  
  18. clientContext.Load(listColl);  
  19. clientContext.ExecuteQuery();  
  20. // Loop through all the list  
  21. foreach (List list in listColl)  
  22. {  
  23. // Display the list title and ID  
  24. Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);  
  25. }  
  26. Console.ReadLine();  
  27. }  
  28. private static SecureString GetSPOSecureStringPassword()  
  29. {  
  30. try  
  31. {  
  32. var secureString = new SecureString();  
  33. foreach (char c in ConfigurationManager.AppSettings["SPPass"])  
  34. {  
  35. secureString.AppendChar(c);  
  36. }  
  37. return secureString;  
  38. }  
  39. catch  
  40. {  
  41. throw;  
  42. }  
  43. }  
  44. private static string GetSPOAccountName()  
  45. {  
  46. try  
  47. {  
  48. return ConfigurationManager.AppSettings["SPAccount"];  
  49. }  
  50. catch  
  51. {  
  52. throw;  
  53. }  
  54. }  
  55. }  
  56. }  
I have added username and password in app.config.
  1. <appSettings>  
  2. <add key="SPAccount" value="[email protected]"/>  
  3. <add key="SPPass" value="*********"/>  
  4. </appSettings>  

Answers (2)