Saturday, February 19, 2011

Create SharePoint Document Locations in CRM 2011 - Part 3

This is the last of 3 posts describing how to add SharePoint document locations into CRM.
Part 1
Part 2

This code should be added to that in the previous blog post. I had a devil of a job with a 401 error, but this finally cracked it.

Add a web reference to
http://server01/_vti_bin/Lists.asmx and name the reference listservice
http://server01/_vti_bin/Views.asmx and name the reference views

Add a using statement
using system.Xml;

Add this method into the code in previous blog post.

private static void CreateSharePointFolder(string docfolderUrl)
if (docfolderUrl == String.Empty || docfolderUrl.IndexOf("/") == -1)
// last part is the folder name
string folderName = docfolderUrl.Substring(docfolderUrl.LastIndexOf("/") + 1);
// remove the folder name
docfolderUrl = docfolderUrl.Replace("/" + folderName, "");
// get the document libray name
string docLib = docfolderUrl.Substring(docfolderUrl.LastIndexOf("/") + 1);
// now remove the doc lib to leave the sharepoint site url
string sharePointSiteUrl = docfolderUrl.Replace("/" + docLib, "");

listservice.Lists myLists = new listservice.Lists();
views.Views myViews = new views.Views();

myLists.Url = sharePointSiteUrl + "/_vti_bin/lists.asmx";
myViews.Url = sharePointSiteUrl + "/_vti_bin/views.asmx";
myLists.UseDefaultCredentials = true;
myViews.UseDefaultCredentials = true;

XmlNode viewCol = myViews.GetViewCollection(docLib);
XmlNode viewNode = viewCol.SelectSingleNode("*[@DisplayName='All Documents']");
string viewName = viewNode.Attributes["Name"].Value.ToString();

/*Get Name attribute values (GUIDs) for list and view. */
System.Xml.XmlNode ndListView = myLists.GetListAndView(docLib, viewName);

/*Get Name attribute values (GUIDs) for list and view. */
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
// load the CAML query
XmlDocument doc = new XmlDocument();
string xmlCommand;
xmlCommand = "<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" + folderName + "</Field> <Field Name='ID'>New</Field></Method>";
XmlElement ele = doc.CreateElement("Batch");
ele.SetAttribute("OnError", "Continue");
ele.SetAttribute("ListVersion", "1");
ele.SetAttribute("ViewName", strViewID);

ele.InnerXml = xmlCommand;

XmlNode resultNode = myLists.UpdateListItems(strListID, ele);

// check for errors
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("tns", "");
if (resultNode != null)
{ // look for error text in case of duplicate folder or invalid folder name
XmlNode errNode = resultNode.SelectSingleNode("tns:Result/tns:ErrorText", nsmgr);
if (errNode != null)
// Write error to log;

catch (Exception ex)
throw ex ;

Create SharePoint Document Locations in CRM 2011 - Part 2

This is part 2 of three posts
Part 1
Part 3

This code expects the Guid for the Contact and then returns the SharePointDocumentLocation AbsoluteUrl. For example it should return <a href="http://server01/contact/Charles Emes">http://server01/contact/Charles Emes</a>. That folder should exist in the SharePoint document library 'Contact'. Note that the code for creating the SharePoint folder is in the following blog post.

Add references to Microsoft.Crm.Sdk.Proxy.dll and Microsoft.Xrm.Sdk.dll to your project. Add the file crmsdktypes.cs into your project (note the SharePointDocumentLocation object is defined within this class and the code won't work without it). All of these files you will find in the CRM SDK directory.

Add the following using statements
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;

public class IntegrationService

// private static string ContactId = "3E339B88-C632-E011-BCDE-00155D110735";

public static string GetSharePointLocation(string ContactId)

// Connect to the Organization service.
System.ServiceModel.Description.ClientCredentials cred = new System.ServiceModel.Description.ClientCredentials();
cred.Windows.ClientCredential = new System.Net.NetworkCredential("Username", "Password", "Domain");

Uri organizationUri = new Uri("http://server01:5555/orgName/XRMServices/2011/Organization.svc");

Uri homeRealmUri = null;

OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, homeRealmUri, cred, null);

// This statement is required to enable early-bound type support.
orgService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

Guid guidSPDocLoc = RetrieveSharePointLocation(orgService, ContactId);

string absouteUrl = GetAbslouteUrl(orgService, guidSPDocLoc);


return absouteUrl;


// Catch any service fault exceptions that Microsoft Dynamics CRM throws.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
// You can handle an exception here or pass it back to the calling method.
return ex.Message;

private static Guid RetrieveSharePointLocation(OrganizationServiceProxy orgService, string ContactId)
Guid _spDocLocId = Guid.Empty;
// get the fullname from the contactid - that will be foolder name
string FolderName = GetEntityNamefromGuid(orgService, ContactId);
// replace any illegal chars with '-'
// TO DO
string fetch2 = @"
<fetch mapping='logical'>
<entity name='sharepointdocumentlocation'>
<attribute name='sharepointdocumentlocationid'/>

<filter type='and'>
<condition attribute='regardingobjectid' operator='eq' value='[GUID]' />

</fetch> ";
fetch2 = fetch2.Replace("[GUID]", ContactId);

EntityCollection result = orgService.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var c in result.Entities)
// TO DO there can be more than one so add condition
_spDocLocId = (Guid) c.Attributes["sharepointdocumentlocationid"];
if (_spDocLocId == Guid.Empty)
// there is no location so create one
_spDocLocId = CreateSharePointDocLocation(orgService, FolderName, ContactId);

// get the abslouteURL from the doc location just created
string absouteUrl = GetAbslouteUrl(orgService, _spDocLocId);
// We still need to create a SharePoint folder

return _spDocLocId;

private static string GetEntityNamefromGuid(OrganizationServiceProxy orgService, string ContactId)
string fetch1 = @"
<fetch mapping='logical'>
<entity name='contact'>
<attribute name='fullname'/>

<filter type='and'>
<condition attribute='contactid' operator='eq' value='[GUID]' />

</fetch> ";
fetch1 = fetch1.Replace("[GUID]", ContactId);
string fullname = string.Empty;
EntityCollection result = orgService.RetrieveMultiple(new FetchExpression(fetch1));
foreach (var c in result.Entities)
{ // there can be more than one so add condition
fullname = c.Attributes["fullname"].ToString();
return fullname;


private static Guid CreateSharePointDocLocation(OrganizationServiceProxy _serviceProxy, string FolderName, string ContactId)

// use the Parent Location Id NOT the SharePointSiteId
// Parent Location will create url http://sharepoint/contact/CharlesEmes
// SharePointSiteID will create url http://sharepoint/CharlesEmes
Guid _spParentLocId = new Guid("415FF5BA-CA39-E011-92D1-00155D110735");
// Instantiate a SharePoint document location object.

SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation
Name = "Documents on Default Site 1",
Description = null,
// Set the Regarding Object id - in this case its a contact
RegardingObjectId = new EntityReference(Contact.EntityLogicalName , new Guid(ContactId)),

// Set the Parent Location ID
ParentSiteOrLocation = new EntityReference(SharePointDocumentLocation.EntityLogicalName, _spParentLocId),
RelativeUrl = FolderName

// Create a SharePoint document location record named Documents on Default Site 1.
Guid _spDocLocId = _serviceProxy.Create(spDocLoc);
// Console.WriteLine("{0} created.", spDocLoc.Name);
return _spDocLocId;


private static string GetAbslouteUrl(OrganizationServiceProxy orgService, Guid _spDocLocId)
IOrganizationService _service = (IOrganizationService)orgService;

RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest
Target = new EntityReference(SharePointDocumentLocation.EntityLogicalName, _spDocLocId)
RetrieveAbsoluteAndSiteCollectionUrlResponse retrieveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)_service.Execute(retrieveRequest);

return retrieveResponse.AbsoluteUrl.ToString();


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.

Friday, February 11, 2011

BizTalk Receive Location Error - Verify the schema deployed properly.

I was getting an error with Biztalk Server when receiving a file from MSMQ although you get a similar error on FILE receive locations. The message was

There was a failure executing the receive pipeline: "Microsoft.BizTalk.DefaultPipelines.XMLReceive, Microsoft.BizTalk.DefaultPipelines, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Source: "XML disassembler" Receive Port: "Portname" URI: "FORMATNAME:DIRECT=OS:.\PRIVATE$\QNAME" Reason: Finding the document specification by message type "RootNode" failed. Verify the schema deployed properly.

It is caused because the message does not conform to a schema that Biztalk recognises, in my case because the RootNode is not recognised. The problem is BizTalk has not saved the original message. It's gone. The method described here shows how to keep a copy of the original file. There maybe other solutions but this works well for me.

The solution I outline is for FILE ports but once you understand the principle you will see how to apply it for MSMQ. The principle is to use a Receive Location and a Send Port Group configured for the Pass Through pipeline.

The Receive Location becomes the new port which polls for inbound messages that arrive in the directory c:\inbound. The Receive Location is bound to a Recieve Port which I'll call ReceivePort1.

The Send Port Group has 2 Send Ports in it. Both Send Ports, for the sake of simplicity, are FILE ports that are configured to write out the file using %SourceFileName% to the following directories c:\filecopy and c:\biztalkinbound. Both are configured to use the Pass Through pipeline.

Got to the Filter on the Send Port Group and add the following filter
BTS.ReceivePortName == ReceivePort1

Enable the Receive Location and start both Send Ports and the Send Port Group. If you drop a file into c:\inbound it will appear in both directories c:\filecopy and c:\biztalkinbound and it will retain the original file name.

Your original Receive Location should now point to the location c:\biztalkinbound and it will be configured for whatever pipeline you were using (XML Receive or a custom Flat File pipeline).

The benefit of this is you always have a copy of the original file in its original format in the directory c:\filecopy. As an added treat I got my orchestration to delete files that were successfully processed from this directory. So c:\filecopy became the place which stored files that failed to be processed - the classic being that they failed to match the correct schema as described by the error at the beginning of the post. Send an email alert to the Sysem Administrator and you'll be a hero.

Access Denied message when dropping a file into the GAC (or it doesn't work)

I was using Windows Server 2008 R2 and trying to add a file to the GAC. As you know the easiest way to do this is to drag and drop the file from one window into a window with the location C:\windows\assembly.

I kept getting the Access Denied error message. I tried running the command prompt as the Administrator, navigating to C:\Windows\assembly and then typing "Start . " to open an explorer window as the Administrator. Repeated the file drop but got Access Denied again.  At the time this was a .NET 3.5 assembly.  Now with .NET 4.0 the location of the GAC is no longer C:\Windows\assembly but C:\Windows\Microsoft.Net\assembly and drag and drop is disabled.

OK, so  the other way to install it is to use GACUTIL but guess what, its not installed on the production server. And no you can't use an old version of GACUTIL from .Net Framework 1.1, it has to be the .NET version that your DLL is using. Google said it was in the Microsoft SDK for Windows but I didn't have time to download it.

I managed to copy it from a Hyper-V image I had which had Visual Studio 2010 installed. The file for .Net v3.5 is located in C:\Program Files (x86) \Microsoft SDKs\Windows\v7.0A\bin. You just need to copy GACUTIL.EXE and GACUTIL.EXE.CONFIG.

For .Net 4.0 the location is C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools and you need a third file 1033\gacutlrc.dll for gacutil to work.  When you copy this to the production server all 3 files can be in the same directory. 

The syntax is
gacutil /i myassemblyname.dll /f

To check the file is in the GAC use
gacutil /l myassemblyname           (don't add the .dll)

Hope this gets you out of the same hole I was in.

Biztalk Server 2010 Receive Location won't Enable

I configured my Receive Location on Biztalk Server 2010 but when I tried to enable it, it remained stubbornly disabled. No error message, it just won't Enable.

In my case it was a FILE receive port and what I had forgotten to do was to make sure the service account the Biztalk Application Host is running under has FULL permissions on the file location.

Remember "Read" is not enough because BizTalk insists on deleting the inbound file. That was it. Simples!

BizTalk Server 2010 ConfigurationFailure

I had a failure configuring BizTalk Server 2010. The Enterprise Single Sign-On installed but I got an error with the Group and the BizTalk RunTime. When I tried to run the installation again it showed and error beside the BizTalk Managment database which stated that it had been installed and I needed to choose another database name.

This told me that the account I was using to inst all BizTalk did have the correct permissions to create databases. The error was because MSDTC was not configured correctly. This needs to be done on BOTH the BizTalk Server and the SQL Server. The correct settings are shown below.

First though, delete the Biztalk Management database from the SQL Server because you don't want to end up with two. To configure MSDTC on Windows Server 2008 R2 go to Server Manager, select Roles -> Application Server -> Component Services. Select Distributed Transaction Coordinator and then Local DTC. Right click, select Properties then click on the Security tab. Make sure you are using the Network Service account as well as checking the boxes shown.

Remember you have to do this on both the SQL Server box and the Biztalk Server.

After I had configured MSDTC I re-ran the Biztalk Server Configuration tool and the configuration completed successfully. Happy Days!