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

