Muhammad Ali Khan

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

Archive for the ‘MS CRM Plugin’ Category

Close Service Activity On Create Message In PlugIn

Posted by Ali Khan on February 19, 2010

Here is a simple code to close the service activity in a MS CRM Plugin.

  1: SetStateServiceAppointmentRequest req = new SetStateServiceAppointmentRequest();
  2: req.ServiceAppointmentState = ServiceAppointmentState.Closed;
  3: req.ServiceAppointmentStatus = 8;
  4: req.EntityId = new Guid(context.OutputParameters["id"].ToString());
  5: SetStateServiceAppointmentResponse resp = (SetStateServiceAppointmentResponse)crmService.Execute(req);

But the above code will work perfectly only if the message is Post Update but doesn’t work if the message is Post Create means it doesn’t close the service activity. Strangely, it doesn’t give any error as well. To add to my surprise, if you had configure any plugin on “SetState” or “SetStateDynamic” for serviceappointment even those plugin will be trigger, but the state of the service activity will not change. Why?, i am not sure.

But does this means we cannot close service activity on the “Create” message?, well the trick is to register the above plug-in as Asynchronous mode (as shown below). now the above message will Close the Service Activity even on the Create Message.



Posted in MS CRM 4.0, MS CRM Plugin | Tagged: | 2 Comments »

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;


                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);


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. 


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;


[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]

public class SPFolderWS : System.Web.Services.WebService


    public SPFolderWS ()





      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;




      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);


             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=, 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.

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.


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;


      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

Posted in MS CRM 4.0, MS CRM Plugin, Sharepoint 2007 | Tagged: | 2 Comments »