Launch SharePoint 2007 workflows from javascript

A typical customer request is a personalized menu item on a List or Document Library , for example it was requested to move the current document library item into an hystorical library identical to the first one (deleting the current item) and immediately call the upload page in order to substitute the moved item.

In our sample i have simplified the things, assuming the workflow does not delete the current item: otherwise the original code is a lot more complicated (it is necessary to save some info because at a certain point the current item is “vanished”) and the scope of this post is to show the basic technique.

In this example we works on a Document library named “CEO Documents”, the request is to implement a custom menu item when we are working with the default “All Documents” view , tipically corresponding to AllItems.aspx, but every view page can be customized in this manner.

From Site Actions ->Edit Page i have added an CEWP (Content Editor Web Part) ,clic on Source Editor .

SharePoint 2007 use, as SharePoint 2010, an principal javascript module named core.js that provides entry points for add a custom item menu when the user clic on the List /Document Library item.

For a Document Library this entry point is the Custom_AddDocLibMenuItems function (Custom_AddListMenuItems for a List).

By default in SharePoint 2007 there is not an “out of the box” system for to call an WorkFlow or to access server side functionalities as in SharePoint 2010, but we can use Ajax and the best solution is to use together JQuery and SPServices.

For our luck SPServices provide methods for various tasks , including the workflow call.

After downloading the library place the min.js version into the layouts folder (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\LAYOUTS)

In divSpin we provide an ajax spinning image (the sharepoint default spin image) so we can provide to the user a feedback that something is working, that by default is not visible. Could be useful even to lock the interface.

In AddDocLibMenuItems we obtain the id of the current item with the SharePoint function (is in core.js) GetAttributeFromItemTable, and the current Item file Url .

In CAMOpt call we define the new item , that has a “Substitute” caption, use the UPDATEPAGEVARIATION.GIF image as icon.

In this call we note an GUID: this is the workflow guid.

It is necessary to pass the GUID and not the workflow name: this has the drawback that when we modify a workflow (in this sample we are using a simple SharePoint Designer Workflow) this GUID changes, so when the workflow is modified is necessary to review all customized code that call this workflow from javascript.

A solution could be to create a custom web service (a future post…) that for a given workflow name get the current GUID; in every case we can provide an alert as in the example.

Where to read this GUID: simple, on the document library or list right clic on a Item, choose workflows, pointing to the workflow you can see on the status bar the TemplateID parameter:

For to copy the value right clic on workflow link and choose properties, here you can copy the value in the clipboard from the Address.

Note in the completefunc parameter of the SPServices call the spin image hiding .