Microsoft Dynamics 365 Blog

Whenever I talk to people about Windows SharePoint Services and Microsoft Dynamics CRM everyone wants to know how to create document libraries and integrate the library into the Microsoft CRM forms. In previous posts I have shown how to use Iframes to show SharePoint pages on Microsoft CRM forms.

Doc lib Iframe:
Web part page Iframe:

The snippet of code below uses the SharePoint web services to create a document library this code can easily be used in a CRM callout to create a document library when creating a new entity. For example, when a new account is created, a complementary document library could be created for the account team to use.

SharePoint Web Service Proxy class

Before you can call the SharePoint web services you need to add a web reference to your project. With SharePoint figuring out the correct URL to use in the web reference wizard requires that you fully specify a path to the asmx file. SharePoint is designed so that the web services are virtualized at every site level. All of the web services binaries are located in the <site>/_vti_bin directory. Below is the The lists web service handles all of the functionality we need for this post.

For example: http://<sharepointservername>/_vti_bin/lists.asmx

Creating a document library

For creating a document library use the AddList method of the lists web service. This method takes three parameters the list name, description, and template id.

API Reference:

For the list name I suggest using the object name concatenated with the object id. This ensures that the document library name is unique and easy to find when setting up a CRM Iframe. In the code below I am passing in the object name and id. For a preCreate callout you would generate a new guid for the object id, with a postCreate callout you can use the guid generated by CRM.

public static System.Xml.XmlNode CreateDocumentLibrary(string objectName,string objectID)


// instatiate list service

      DocLibraryCallouts.SharePointLists.Lists listService = new _ DocLibraryCallouts.SharePointLists.Lists();

      // set credentials

      listService.Credentials = new  System.Net.NetworkCredential(“SomeUser”, “SomePassword”, “SomeDomain”);


      // Concatenate name with GUID for uniqueness

      string listName = string.Concat(objectName,‘-‘,objectID);


      // Create document library templateid 101 is a document library

      System.Xml.XmlNode result = listService.AddList(listName,objectName,101);

      return result;


References for building callouts

Developing callouts
Link to building callouts using Visual Studio 2005:

My next post

In my next post I will explore creating folders within an existing document folder. Depending on the specific business scenario having a document library per account or opportunity may not be very efficient for the sharepoint server.

Hope this helps
Rich Dickinson

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!