Search code examples
c#sharepointdynamics-crm

How to Connect to SharePoint What User Is Used Determine User Communicating From CRM to SharePoint inside of CRM Plugin


Here is what I know:

  • I have a plugin that is running 8.2 on prem in full trust mode (aka, not sandbox).

  • It creates a Microsoft.SharePoint.Client.ClientContext using the credentials from CredentialCache.DefaultCredentials to perform an ExecuteQuery call to a SharePoint server. This is causing an exception with a 401 unauthorized error.

  • There is some custom exception handler that is logging the user that is unable to connect as the WindowsIdentity.GetCurrent().Name. The value that is returned is the domain credentials of the user that triggers the plugin to run, even though the plugin is setup to run as a Service Account in the "Run in User's Context" of the Plugin Registration tool.

  • In CRM 2013, this appeared to work in such a way that the credentials of the service account that was setup in the Plugin Registration Tool as the "Run in User's Context" user, were the credentials that were used to communicate to SharePoint. After the upgrade, this code is failing.

I attempted to get the username from the CredentialCache.DefaultCredentials, but it is always empty. Is there any way in which to use the Network Credentials of the "Run in User's Context" user?


Solution

  • After further review of the logs, I realized that there where two SharePoint Requests being made, one worked, and the other didn't. When looking at the call that was failing, it was being called in a multi-threaded methodology using Task.WaitAll. Found this SO question How do I set the user identity for tasks when calling Task.WaitAll()? and decided to try and make the calls synchronous. This did the trick!