Welcome to MSDN Blogs Sign in | Join | Help

Code snippet for creating a SharePoint document library

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: http://blogs.msdn.com/crm/archive/2006/06/20/639918.aspx
Web part page Iframe: http://blogs.msdn.com/crm/archive/2006/07/06/658157.aspx

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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/soapmListsAddList_SV01034342.asp

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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/CrmSdk3_0/htm/v3d0calloutcomponentdevelopment.asp
Link to building callouts using Visual Studio 2005: http://blogs.msdn.com/arash/archive/2006/08/25/719626.aspx

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

Published Monday, September 11, 2006 6:28 PM by crmblog
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Code snippet for creating a SharePoint document library

Thanks for all the Sharepoint integration info. I'll be very interested in your thoughts on creating folders - I think this would work well for our situation, where we want to organize documents for individual opportunities without the overhead of creating a separate library for each.
Monday, October 09, 2006 10:53 AM by Jon

# Creating folders in SharePoint document libraries

Last month I posted on how to create a SharePoint document library using a CRM callout. Using a document

Tuesday, October 24, 2006 11:14 AM by Microsoft Dynamics CRM Team Blog

# SharePoint 3.0 Document Libraries & CRM

Flood... Firstly let me appologise for not posting regularly these last few months. Work has been expremely

Friday, April 11, 2008 10:38 AM by Microsoft Dynamics CRM UK Blog

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker