Monday 30 November 2009

Finding Program Equivalent of Share point UI Actions

Have you ever wondered all the functions you want to do is available in the sharepoint pages, through UI, but you can't find the API to do it pragmatically. I'm here to reveal the secret to find all the sharepoitn supported method calls that you are hunting for ages.

You will need .NET developers Bramastra(The ultimate wepone), the Red Gate's .NET Reflector. you can download this from http://www.red-gate.com/products/reflector/

Now you are ready for the discovery of your lifetime.

We will run through the steps using an example. In our case lets say we want to find out how to remove a workflow association with no new instance option. If you directly look in the SPWorkflowAssociation class, there is a remove association option but it does not have a option for No New instance. We are going to find it how Sharepoint does it.

Before we start, let's list out all the location where to look for web pages source and assemblies:
  • Web pages:
  1. \Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
  2. \Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\ADMIN
  • Assemblies
  1. \WINDOWS\assembly
  2. \Inetpub\wwwroot\wss\VirtualDirectories\[yourwebsitename or port]\_app_bin
Step 1:Lets remove the association using UI.

Open List setting









Open workflow setting










Click the remove workflwo link







This the page we have to look care fully. This page has multiple radio button for complete removal, no new instance and allow new instance. Form is simple. From address bar lets note down the aspx page name, i.e. RemWrkfl.aspx.
We can find this file in \Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

Open this file in any text editor. First couple of line are of our interest. First line will tell the assembly file name this page uses for code behind. And the second line will show the class name i.e Microsoft.SharePoint.ApplicationPages.RemWrkflPage for our example.

We can also find that the Ok button in the form invokes BtnSubmit_Click method.

Lets open the assembly using Reflector. We can find the assembly in \Inetpub\wwwroot\wss\VirtualDirectories\[yourwebsitename or port]\_app_bin. In our case the assembly name is Microsoft.SharePoint.ApplicationPages.dll

After you drill down to the Microsoft.SharePoint. ApplicationPages.RemWrkflPage class and BtnSubmit_Click event handler the code looks like as shown in the image on the right side.
If we look carefully, we ca see that for No New Instance option it's simply setting association.Enabled = false;

So that's the magical thing. We have to disable the association instead of removing it.
We are done!



And using the same approach you can discover all functionality, as long as an example is available.

A bit of caution, some MOSS functionality is not developed using managed code. You wont be able to see code for those functionality.

No comments:

Post a Comment