2017-12-03 23 views
0

現在、以下のようにユーザIDとパスワードを使用して共有リストにアクセスできます。しかし、クライアント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(); 
} 

をあなたのプロジェクトの参照に行き、PnPのCSOMコアを追加するには> nugetパッケージを管理。

SharePointPnPCoreOnlineパッケージを追加します。

enter image description here

参照 - Authenticate SharePoint using PnP Authentication Manager

Expose on public web your SharePoint Online information

関連する問題