Recently I wrote a C# helper class to dynamically create a folder in SharePoint. This library is being used within a BizTalk orchestration while posting documents. The workflow has the following main purpose:
- Dynamically create the target SharePoint folder in SharePoint 2013
- Upload media to the target SharePoint folder
During load conditions, the helper class sometimes throws the exception error below:
CreateDocumentFolder Exception: Save Conflict.
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.
Apparently, SharePoint manages folder creation requests sluggishly. Nevertheless, without the proper folder created in SharePoint, the BizTalk orchestration will not be able to upload media.
You can easily resolve the issue by injecting retry logic via Enterprise Library 5.0 Fault Handling Application Block. You can create intricate retry policies to manage exception scenarios.
In a more simple approach, a while loop will do, as seen below:
public static void CreateContactDocumentFolder(string siteURL, string firstName, string lastName, string contactGuid)
{
Int16 intRetried = 0;
Boolean blnCompleted = false;
while (blnCompleted == false)
{
try
{
Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteURL);
Microsoft.SharePoint.Client.List lists = context.Web.Lists.GetByTitle("Contact");
context.Load(lists);
context.Load(lists.RootFolder);
context.ExecuteQuery();
lists.EnableFolderCreation = true;
//Create dynamic subfolder under "Contact" folder in SharePoint
lists.RootFolder.Folders.Add("Contact/" + firstName + " " + lastName + "_" + contactGuid);
lists.Update();
context.ExecuteQuery();
blnCompleted = true;
context.Dispose();
}
catch (Exception ex)
{
//retry 100 times
intRetried++;
if (intRetried == 100)
{
blnCompleted = true; // exit loop
// Write error to log
}
}
}
}