Let me start with the setting: - Outlook 2010 VSTO App - Running on Win8 Enterprise - Process spawned by Visual Studio 2010 debugger - Destination is a SharePoint 2010 Form Library accessed over SSL
The scenario is an instance in code where I call-out to a SharePoint 2010 Form Library to get a maxID, using a service account that has read rights to all form library items, the ICredentials are created for a Microsoft.SharePoint.Client.ClientContext object in the following manner:
return new System.Net.NetworkCredential(settings.Username, securePassword, settings.Domain);
Nothing fancy or unusual as far as I can see. Then later on in the function I write to the form library,dynamically generating an InfoPath 2010 form. I'm performing an HTTP PUT to the same URI using System.Net.WebClient. I set the WebClient.Credentials = CredentialCache.DefaultCredentials with the intention of using the current user context to upload the form. However, no matter what I do, I keep getting the user account that I set for the Microsoft.SharePoint.Client.ClientContext that was used further upstream in the process. And yes, I'm calling Microsoft.SharePoint.Client.ClientContext.Dispose() after I'm done with the process mentioned above (calling out to Form Library to get a maxID). Can anyone please offer any insight as to why I can't seem to shed the said 1st account?
Posting code sample per lem.mallari's request:
public void CreateRequestForm()
{
...
// Build our InfoPath form on current thread
infoPathFormData = InfoPathFormWriter.CreateForm(requestDetailsFormRegion, appointmentItem);
this.backgroundWorker.RunWorkerAsync(); // Hop to BackgroundWorker_DoWork
}
private void BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
...
documentID = SharePointUtility.GetMaxDocumentItemID();
...
UploadToRequestLibrary(infoPathFormData, submittedFormName);
...
backgroundWorker.ReportProgress(100);
}
public static string GetMaxDocumentItemID()
{
ClientContext clientContext = new ClientContext(settings.SharePointSite.ToString());
// NEED TO USE ELEVATED SERVICE ACCOUNT IN ORDER TO CARRY OUT OPERATION
ICredentials credentials = new System.Net.NetworkCredential(settings.Username, password, settings.Domain);
clientContext.Credentials = credentials;
...
clientContext.ExecuteQuery();
...
clientContext.Dispose();
return itemID;
}
private void UploadToRequestLibrary(byte[] data, string formName)
{
...
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
// WANT TO USE LOGGED-IN USER'S CREDENTIALS (WANT TO SUBMIT ITEM UNDER CURRENT USER CONTEXT)
webClient.Credentials = CredentialCache.DefaultCredentials;
// BTW, if I explicitly create network credentials using currently logged in user's username/password/domain (hard-coding my own creds) the new creds stick.
// ICredentials credentials = new = NetworkCredential(myUsername, mypassword, myDomain); // WORKS
byte[] result = webClient.UploadData(settings.FormLibraryPath + formName + ".xml", "PUT", data);
...
}