2017-12-03 157 views
0

當前,我可以使用用戶ID和密碼訪問SharePoint列表,如下所示。但想了解如何使用客戶端ID和客戶端密碼訪問列表?在asp.net中使用客戶端ID和客戶端密碼訪問Sharepoint列表C#

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(); 

回答

3

您可以使用PnP CSOM內核的GetAppOnlyAuthenticatedContext方法。

在此之後,你可以使用如下代碼:

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(); 
} 

要添加即插即用CSOM核心,去你的項目引用>管理的NuGet包。

添加SharePointPnPCoreOnline包。

enter image description here

參考 - Authenticate SharePoint using PnP Authentication Manager

Expose on public web your SharePoint Online information