4

Currently, I'm able to access sharepoint list using user id and password as given below. But would like to understand on how can i access the list using Client Id and Client secret ?

string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
ClientContext clientContext = new ClientContext(siteUrl);
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
System.Security.SecureString passWord = new System.Security.SecureString();
foreach (char c in password.ToCharArray()) 
{    
    passWord.AppendChar(c);
}

clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
MethodMan
  • 18,625
  • 6
  • 34
  • 52
AKR
  • 171
  • 1
  • 3
  • 10

2 Answers2

11

You can use the GetAppOnlyAuthenticatedContext method of PnP CSOM core.

After that you can use the code as below:

string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
string clientId = "<client-id>";
string clientSecret = "<client-secret>";

using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret))
{       
    Web oWebsite = clientContext.Web;
    ListCollection collList = oWebsite.Lists;
    clientContext.Load(collList);
    clientContext.ExecuteQuery();
}

To add PnP CSOM core, go to your project references > manage nuget packages.

Add the SharePointPnPCoreOnline package.

enter image description here

References - Authenticate SharePoint using PnP Authentication Manager

Expose on public web your SharePoint Online information

Gautam Sheth
  • 2,442
  • 1
  • 17
  • 16
  • I am using same code but getting below error : System.TypeInitializationException: The type initializer for 'OfficeDevPnP.Core.Utilities.TokenHelper' threw an exception. ---> System.TypeLoadException: Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. at OfficeDevPnP.Core.Utilities.TokenHelper..cctor() Please help me on this. – Dnyaneshwar Suryawanshi Oct 09 '20 at 12:41
  • FWIW, the SharePointPnPCoreOnline package seems to be retired; using PnP.Framework is the new version, and `GetACSAppOnlyContext` replaces `GetAppOnlyAuthenticatedContext`. – Steve Friedl Feb 23 '21 at 20:22
2

SharePointPnPCoreOnline had retired , and install it will cause library conflict issue , if your project target framework is .Net Core.

This is code was tested and work for .Net Core.

Successor Nuget Package Name : PnP.Framework

NameSpace :

using PnP.Framework;  
using Microsoft.SharePoint.Client;

Sample Code :

    string siteUrl = "https://xyz.sharepoint.com/sites/MySubSite" ;
    string ClientSecret = "<client-secret>";
    string ClientId = "<client-id>";


    using (var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, ClientId, ClientSecret))
    {
        Web oWebsite = clientContext.Web;
        ListCollection collList = oWebsite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQuery();
        string result = "Total SP Library count is " + collList.Count();

    }
HO LI Pin
  • 1,441
  • 13
  • 13
  • 1
    I am getting "The remote server returned an error: (403) Forbidden." while doing `clientContext.ExecuteQuery()`. Any Idea? – Ishaan Oct 15 '21 at 07:24
  • obviously SPO is complaining about access , make sure client ID have sufficient access to the site collection \ Site \ List that you are try to get data . I am not sure how to setup the client ID because is my IT setup for me , but this article shall tell you the relevant info how to setup client ID with full control access , https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs – HO LI Pin Oct 16 '21 at 07:34
  • Can we get token by using Client Id, Secret and use it ? – zainul Feb 22 '22 at 11:29
  • I am getting "No such host is known. No such host is known." after ExecuteQuery(). I am sure I've given correct Url and ClientId & Secert. Any tips? – san Mar 27 '22 at 09:38
  • Zainul , you can use https://portal.azure.com/ > Manage Azure Active Directory > Left Nav > App Registrations there to generate\renew Client ID and secret – HO LI Pin Aug 08 '22 at 02:52
  • San , check your DNS name resolution to make sure your dot net code host , able connect to your SPO instance using name – HO LI Pin Aug 08 '22 at 02:52