Access Sharepoint list using client Id and Client secret in asp.net C#
10,269
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.
References - Authenticate SharePoint using PnP Authentication Manager
Expose on public web your SharePoint Online information
Author by
AKR
Updated on June 13, 2022Comments
-
AKR almost 2 years
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();
-
Dnyaneshwar Suryawanshi over 3 yearsI 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.
-
Steve Friedl about 3 yearsFWIW, the SharePointPnPCoreOnline package seems to be retired; using PnP.Framework is the new version, and
GetACSAppOnlyContext
replacesGetAppOnlyAuthenticatedContext
. -
Ishaan over 2 yearsI am getting "The remote server returned an error: (403) Forbidden." while doing
clientContext.ExecuteQuery()
. Any Idea? -
HO LI Pin over 2 yearsobviously 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 , docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/…
-
zainul about 2 yearsCan we get token by using Client Id, Secret and use it ?
-
san about 2 yearsI 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?