Saturday, February 19, 2011

Create SharePoint Document Locations in CRM 2011 - Part 1

This post is the first of three on how to programmatically create SharePoint 2010 Document Folders in CRM 2011 so that documents can be uploaded.
Part 2
Part 3

There is quite a lot of code for this solution which is why I've broken it up into 3 parts. This first part outlines the scenario and the assumptions.

So here is the scenario. I have CRM 2011 and I want to store documents related to the Contact entity in SharePoint 2010. In my development environment I have both of these on the same virtual image but I've designed the code to work as a web service so it can sit it anywhere.

What CRM 2011 does is create a document library for each Entity that is enabled for document storage, and then creates a folder for each record. This example focuses on the Contact entity but it can be easily applied to other entities. When a new entity record is created in CRM 2011 and the Document menu item is clicked, it will create a) a SharePointDocumentLocation record in CRM that has the path to the SharePoint folder and b) warns you that it is about to create a document folder. The name of the folder for a Contact if based on full name. So what happens if you have two John Smiths? If the first already has created a document folder it detects that and prompts with 'the folder already exists do you want to use it?' If not, then you can modify the name of the folder to ensure it is unique.

This is an imprtant point. Assume you have a contact called Robin Wright. When you create a document folder it will be called 'Robin Wright'. When Robin gets married and changes her name to 'Robin Wrigth-Penn' then the document folder name remains the same name. CRM 2011 uses the SharePoint document location record to point to the orginal folder name. So you need to be careful, you can't assume that the full name in CRM is the same as the document folder name.

For this example I am going to assume you have the Guid for the Contact in question. This unique identifier will be used to determine if there is already a SharePoint Document Location created for this record. If so, it returns the url for you. If not, it will create the SharePoint Document Location in CRM and then create the actual folder in SharePoint based on the full name and finally returns the url to the folder.

So this code either uses the existing SharePoint Document Location or will create a new one for you and create the folder in SharePoint.

I have made another assumption. I assume you have created the SharePoint site already with the document library for the entity "Contact". It will help if you have a few SharePoint document locations already created because then you will be able to see how the database field 'ParentSiteorLocation' is populated in the SharePointDocumentLocationBase table. You need to use the 'root' Guid for the Contact document library and it should be obvious what this is if you have a few records in the table.

You can recognise this row in the table as the relativeurl reads 'contact' and the RegardingObjectID is null. The Guid you need is the SharePointDocumentLocationId from this record. It will become clearer (I hope) when you see the code.

There is another issue you will need to be aware of. Folder names in SharePoint will not allow certain characters. If they exist in your CRM Contact record e.g. the first name reads 'John & Jane' then CRM replaces the & with -. I have not checked all illegal characters are substituted with hyphen but it is my working assumption. So be warned, you will need to modify full name of contact to replace illegal characters. Also please note this code does NOT handle duplicate names - you will need to check for a duplicate and create a unique folder name.


So the next blog has the CRM code for using an existing SharePoint Document Location or create a new location. There is a reference in the code to a function that will create the actual SharePoint folder but the code for that is in the last blog.

12 comments:

.NetCodeSnippets said...

I am new to CRM-SharepointIntegration, Can you guide me how to do this programmatically for custom entities?

Maria said...

Excellent posts. I hope you can help me with my current pickle. I need to programmatically add documents to SharePoint from CRM. I can find lots of information on how to add document folders (yours being the most detailed by FAR, so thank you for that!), but none that show how to add the actual documents. Is this even possible? Do you know?

Thanks in advance.

Chaitanya... said...

Thank you so much for sharing this information. Most of the links are half written.
Your approach and solution are very easy to digest from the stand point of development.

Thanks agaiin,
Chaitanya...

Charles Emes said...

Maria,
I have published a blog on how to move an attachment from CRM to SharePoint. Sorry, may be too late to help you.

Memtech said...

check out this link...
Create a Document in SharePoint and add folder in SharePoint Document using C#

it might be useful for you.

Charles Emes said...

Hi Memtech, thanks for your comment. The code in the link is fine except it only works if you are running it on the SharePoint server. In most CRM deployments that use SharePoint as a document repository the SharePoint is on a different server to CRM.
That forces you to use the SharePoint web services, as I have done, rather than the API.
Cheers
Charles

ChrisS said...

Great post. Thanks for sharing all the helpful information you have on your blog.

Anonymous said...

Hi Charles, thanks for the code. I am not sure how you invoke the code? Plugin, onCreate of Record? or is there a Plugin Event for Click on Documents Link?
e.g. you invoke IntegrationService from a Plugin? or Called from JavaScript, Form.onSave()? Any help will be much appreciated.

Charles Emes said...

Hi JELS,
I was using this for a mail merge to move the attachment to SharePoint. So I has it executing the plug-in on creation of an annotation. But sure you could incorporate it as a plug-in on create of the entity where you store documents. I don't think there is a way to trap the on-click of the Documents link. Good luck. Glad it helped.

Unknown said...

Hi there,

My company is planning an integration between sharepoint online and dynamic crm online. we have investigated the OFTB document management integration with sharepoint and it looks great. We understand the requirements of the configuration required to get started but we have one outstanding question. When enabling document management you select what entities and folder structure to use, in order to turn on document management. After you perform this step, to my understanding, the documentation management feature is available for all of the selected entities. However, an additional manual step is required to actually create the folders on an entity by entity basis. Essentially the first time you click on the document management link in the entity it ask if you want to go ahead and create the folder in sharepoint.

I was wondering if there is a way to automatically create these folders for all entities in one shot, perhaps via a script. We have file server with all of our existing documentation for accounts and opportunities and we need to migrate this into sharepoint. As I really dont forsee my sales team manually creating folders for each entity (too time consuming) I figure there must be a way that i can automate the creation of the folders in sharepoint so that I can then copy over the existing content of the file server.

Does anyone have any experience with this, or advice?

Withers said...

Is there way to post to a different library type say asset. We want a view that will store images and show a preview of the images. the current library type of document doesn't do this. I'm guessing you'd have to use an iframe to do this?
Scenario
User goes to custom entity, opens iframe that creates unique folder, user uploads multiple images to asset type library. next time they load custom entity it shows previews (large icons) of the uploaded images.

Incubeus said...

Hello,

Not sure if you updated to RU 14, but I have a problem since I did. Sharepoint folders are no longer created automatically. Have you ran into this yet? O have any suggestions to resolve?

Thank you :)