Home

Jul 8, 2007

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.aspxWeb 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 /_vti_bin directory. Below is the The lists web service handles all of the functionality we need for this post.
For example: http:///_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.aspLink to building callouts using Visual Studio 2005: http://blogs.msdn.com/arash/archive/2006/08/25/719626.aspx

No comments: