Muhammad Ali Khan

MS CRM 3.0, MS CRM 4.0, Sharepoint 2007 & Sharepoint 2010

MS CRM 4.0 Plugin Integeration With Sharepoint using Sharepoint.dll

Posted by Ali Khan on July 19, 2009

 
Using Microsoft.Sharpoint.dll inside the MS CRM 4.0 Plugin

Well there may be points in your MS CRM 4.0 development, where you need integration with SharePoint. I came across one such scenario where I have to create a SharePoint Folder in a document library on the Create Event of an Entity “Account”. Also once the account is renamed, I have to rename the folder as well.

Now SharePoint gives you two options to customize its document library either using the webservices of lists.asmx or Dws.asmx or using the SPList and SPSite Object from Microsoft.SharePoint.Dll. Since in my case I wanted to create a folder on the create event of an account so I used this code for my application.

For creating Folder in Sharepoint

Add the reference to the Dws.asmx webservices. This webservice has a create folder method and use this code in the plugin where you want to create a folder in Sharepoint

       Dws objDws = new Dws();

       objDws.Url = http://moss:32901/sites/testsite/_vti_bin/Lists.asmx”;

      if(MossSiteUser.Length == 0)

                 objDws.Credentials =System.Net.CredentialCache.DefaultNetworkCredentials;

     else

                objDws.Credentials = new System.Net.NetworkCredential(MossSiteUser, MossSitePwd, MossSiteDomain);

     string returnResult = objDWS.CreateFolder(newFolderName);

For Renaming the folder in Sharepoint

So far so good, but to my surprise L, Dws.asmx doesn’t provide any method for RenameFolder() nor does List.asmx. However, the SPFolder Class in Microsoft.SharePoint.dll does have one such
method (Actually it’s still not RenameFolder(), but it has one method MoveFolder() which can serves the purpose of the RenameFolder). The problem how-ever is if you try to use this code to RenameFolder() inside the plugin. It will Fail with 401 Access Denied Error.

            SPSite spSite = new SPSite(siteURL);

           SPWeb site =spSite.OpenWeb();

            site.AllowUnsafeUpdates = true;

            SPFolder folder =site.GetFolder(sourceFolderName);

            folder.MoveTo(destinationFolderName);

The above code will never work inside the plugin, because the plugin normally runs under the security context of the “Network Service”. Even though it should impersonate but you are never mentioning in your code (above which user to use, nor there is a way to mentioned it). I check on some sites which recommends to use the put the code in SPSecurity.RunWithElevatedPrivileges but I didn’t have any success with this method either, because the SPUserToken object was always null L.

So what is the solution? The solution is to create a custom webservice and deploy this webservice in SharePoint as a custom webservice and then it will behave just like the normal MOSS webservice.

So here we go:-

Step 1, Create a windows Library which will contain the code of the Custom Webservice:-

Create a Windows Library project e.g. “MyCustomFolder.dll”. Sign the assembly (Add the sign.key from the properties window). Add the Reference to the Microsoft.Sharepoint.dll and System.Web.Services.dll as show in the picture below. 

References 

Create a class, name it “SPFolderWS” add the following method to it, and add the following methods as it is:-

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using Microsoft.SharePoint;

[WebService(Namespace= http://tempuri.org/”)] 

[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]

public class SPFolderWS : System.Web.Services.WebService

{

    public SPFolderWS ()

    {

    }

 

       [WebMethod]

      public bool IsFolderExits(string siteURL, string sourceFolderName)

      {

            SPSite spSite = new SPSite(siteURL);

            SPWeb site = spSite.OpenWeb();

            site.AllowUnsafeUpdates = true;

            SPFolder folder = site.GetFolder(sourceFolderName);

            return folder.Exists;

      }

 

       [WebMethod]

      public bool RenameFolder(string siteURL, string sourceFolderName,    string destinationFolderName)

      {

            SPSite spSite = new SPSite(siteURL);

            SPWeb site = spSite.OpenWeb();

             site.AllowUnsafeUpdates = true;

            SPFolder folder =site.GetFolder(sourceFolderName);

            folder.MoveTo(destinationFolderName);

             return true; 

      }  

}

 As you may have guessed from the above code, we created a class which inherited from Webservice in order to make our component as
webservice. Build this class library and install this library in GAC. (You can simply drag and drop the file from the folder to c:\windows\assembly folder or use the GacUtil command line tool).

Step 2, Create the Webservice Project:-

 For the 2nd step, we will create a Webservice project (which only has the asmx file, no .cs file). Add the reference to the above class library “MyCustomFolder.dll”.  After adding the asmx file modify the asmx file to point to the correct webservice class (SPFolderWS) from “MyCustomFolder.dll”

<%@ WebService Language=”C#” Class=”SPFolderWS, MyCustomFolder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a7dfbe963f8254e” %>

Please note that the public key token will vary for your dll. Now Build the Project.

Step 3, Create WSDL & Disco File for Webservice and Deploy in MOSS:-

Now the 3rd Step, Now you have to deploy your custom webservice in sharepoint. You can go through the hectic way of  generating the WSDL and DISCO file yourself. But I would perfer you use the great SPDev tool which you can download form this site, which can geneate the disco and wsdl file for you.

http://www.crsw.com/spdev/Wiki%20Pages/GenWS.aspx

Just type this in the command line.

SPDev.exe -o GenWS -url http://moss:32901 -asmx SPFolderWS.asmx –deploy

The above command line will generate the disco and wsdl file in the directory and two .aspx files. Simply copy these files to the following directory “C:\Program Files\Common
Files\Microsoft Shared\Web Server Extensions\12\ISAPI”
. Now you have successfully registered a custom webservice inside Microsoft Sharepoint. Browse the following URL to confirm your custom webservice is properly generated.

http://moss:80/SPFolder/_vti_bin/SPFolderWS.asmx

Now go to the plugin project and add the reference to the above webservice and write the following code.

SPFolderWS objService = new SPFolderWS();

objService.Url = “http://moss:32901/sites/test_ali_site/_vti_bin/SPFolder.asmx;

if(MossSiteUser.Length == 0)

objService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

else

      objService.Credentials  = new System.Net.NetworkCredential(MossSiteUser,MossSitePwd, MossSiteDomain);

if  (objService.IsFolderExits(siteURL, folderName))

      objService.RenameFolder(siteURL, folderName, newFolderName);

It should work greatJ.

 
For step by step details of creating a custom webservice in sharepoint, see my following post Custom WebService In SharePoint

Advertisements

2 Responses to “MS CRM 4.0 Plugin Integeration With Sharepoint using Sharepoint.dll”

  1. unlock codes said

    I came across your article, and i think you are very good writer, keep us posting

  2. Artikelverzeichnis ohne Backlink…

    […]MS CRM 4.0 Plugin Integeration With Sharepoint using Sharepoint.dll « Muhammad Ali Khan[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: