0

I have a piece of code that uses CSOM to get search results from Sharepoint.

using (var clientContext = new ClientContext(serverUrl)
{
    Credentials = CredentialCache.DefaultCredentials
})
{
    var keywordQuery = new KeywordQuery(clientContext);
    keywordQuery.QueryText = query;
    var searchExecutor = new SearchchExecutor(clientContext);
    var results = searchExecutor.ExecuteQuery(keywordQuery);
    clientContext.ExecuteQuery();
}

This code works when it's executed on the Sharepoint server locally, when I'm logged in.

However if I put this piece of code in an web application hosted on IIS, I get 401 Unauthorized on ExecuteQuery()

The Windows authentication is enabled in IIS application settings enter image description here

as well as in Sharepoint Central administration enter image description here

0

Instead of authenticating with the default credentials, we need to pass the credential explicitly using the NetworkCredential where we need to pass the username and password.

Connect to SharePoint from Asp. Net using NetworkCredential:

using (srcContext = new  ClientContext("siteURL"))
  { 
     string userName="MyUserName"
     string password="MyPassword"
     NetworkCredential credentials = new NetworkCredential(userName, password); 
}
  • Well, that's not an option. I have integrated authentication enabled on my site and I need to reuse it for the Sharepoint connection. – Szybki yesterday
  • What account are you using for the application pool of the Web Application where this code is running? Does that account have permission to your SharePoint site? – Matiur Rahman 22 hours ago
  • @Szybki if this is multihop (client-webserver-SharePoint) then Windows authentication isn’t enough. You need to set up Kerberos Auth (as NTLM cannot double-hop). You’ll need SPNs set up alto match the host names and accounts used for your SharePoint site and your website. – Greg W 18 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.