We had a requirement to associate a SharePointDocumentLocation with a case record when it is created. We wanted to use our own folder structure for it. Something similar to this
Sharepointdocumentlibraryurl/[CustomerName]/[CaseNumber]
i.e. folder name with Customer Name and a subfolder with the name of Case Number.
Normally SharePoint document location is created when we click on Documents on the left navigation pane for the record. It doesn’t get created or auto associated to a SharePoint folder on create of record.
So the solution was to write a plugin in on create event and then create a folder in SharePoint and then associate it with the created record.
The source code for implementing it (just posting the source code i hope it is self explanatory 🙂 )
// following logic within the Post Create plugin of Case (incident)
if (this.Context.MessageName.ToUpper() == "CREATE" && Context.InputParameters.Contains("Target")
&& Context.InputParameters["Target"] is Entity)
{
Entity targetIncident = (Entity)Context.InputParameters["Target"];
//// Create doc folder for case
Guid incidentId = Context.PrimaryEntityId;
string ticketNumber = targetIncident["ticketnumber"].ToString();
Guid customerId = ((EntityReference)targetIncident["customerid"]).Id;
Contact contact = // code to get the contact's fullname
SharePointHelper sharepoint = new SharePointHelper();
sharepoint.CreateFolderForCase(organizationService, contact.FullName, ticketNumber, incidentId);
}
// Helper functions used
public string CreateFolderForCase(OrganizationService coreCrmService, string contacFolderName, string caseFolderName, Guid incidentId)
{
string docLibraryUrl = "http://SharePointSiteCollection/incident";
string applicantFolderUrl = docLibraryUrl + "/" + applicantFolderName;
string caseUrl = applicantFolderUrl + "/" + caseFolderName;
//// Create folder and subfolder inside SharePoint site
this.CreateFolderStructure(applicantFolderUrl, caseUrl);
//// create and associate the url of the folder to SharePointDocumentLocation and the incident record.
this.CreateAndAssociateSharePointDocumentLocation(coreCrmService, incidentId, caseFolderName, caseUrl);
return caseUrl;
}
private bool CreateFolderStructure(string applicantFolderUrl, string caseFolderUrl)
{
if (this.CreateFolder(applicantFolderUrl))
{
return this.CreateFolder(caseFolderUrl);
}
return false;
}
private bool CreateFolder(string folderUrl)
{
try
{
WebRequest request = WebRequest.Create(folderUrl);
request.Credentials = this.credentials;
request.Method = "MKCOL";
WebResponse response = request.GetResponse();
response.Close();
}
catch(Exception ex)
{
TraceHelper.TraceEvent(ex, "CreateFolder", folderUrl, TraceHelper.AMSApplication.AMSService);
}
return true;
}
public static bool CreateAndAssociateSharePointDocumentLocation(OrganizationService coreCrmService, Guid incidentId, string caseNumber, string sharePointDocumentUrl)
{
SharePointDocumentLocation sharePointDocumentLocation = new SharePointDocumentLocation();
sharePointDocumentLocation.Name = "SharePoint Document Location for " + caseNumber;
sharePointDocumentLocation.Description = "SharePoint Document Location created for storing documents related to case";
sharePointDocumentLocation.AbsoluteURL = sharePointDocumentUrl;
sharePointDocumentLocation.RegardingObjectId = new EntityReference("incident", incidentId);
coreCrmService.Create(sharePointDocumentLocation);
return true;
}
Please refer these wonderful posts that were extremely helpful.
http://charlesemes.blogspot.com/2011/02/create-sharepoint-document-locations-in.html
http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

Both URL’s on the bottom point to the the same place(The first one)
LikeLike
Thanks Shai 🙂
LikeLike
Hey Nishant, thanks for the reference to my blog. I’m a fan of yours too!!!
Regards
Charles Emes
LikeLike
Thanks Charles, your post was very helpful.
LikeLike
Hi Nishant,
This looks like just what I need but with custom entity, i’m not much of a developer and i have some problems to get this to work. Could you list the references used in this?
Thanks
Jussi
LikeLike
Hello Nishant
I have similar requirement and I tried your code with one change, in CreateFolderForCase function I replaced applicantFolderName with contacFolderName.
When I create new case, plugin completed its execution without throwing any error. Also, i can see a new record created under Sharepoint Document Location with correct Absolute URL.
But when I try to click on Documents link from within Case left navigation pane I get an message saying “This location doesnt exist on Sharpoint Server. Change the location URL in Crm.” Also, If i go and check my sharpoint site, the folder structure is not created.
Can you please help me out with this.
Regards,
Bhavika Patel
LikeLike