Starting a workflow with a initiated parameters through JavaScript   

Tags: SharePoint 2010
Technorati Tags:

The past day I was searching how you could trigger a workflow through JavaScript  that needed initiated parameters.

There are 2 possible solutions that you can use.

 

1 jQuery and SPService

SPService a jQuery library which abstracts SharePoint's Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install.

With SPService it is also possible to trigger a workflow and this site shows you how you can do it.

http://spservices.codeplex.com/wikipage?title=StartWorkflow

Now if you want to use the initial screen from your workflow there is an other approach and you don’t have to use jQuery or the SPService.

 

 

2 SP.UI.ModalDialog.showModalDialog

If you use the standard SharePoint master page you probably don’t have to include the following tag.

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true"   Localizable="false" />

What do we need to know before we can start writing our JavaScript code.

  1. We need to know our workflow templateID
  2. We need to know our item id
  3. We need to know our list id


1 Getting our templateID

  1. Go to your list workflow settings.
  2. Click the workflow you want to start through SharePoint.
  3. Search in the url for this property “&TemplateID=
  4. copy the guid after the “&TemplateID=” 
    If your guid looks like this %7B5D1A8A88%2D0E2A%2D4F2C%2D8610%2DFB6E9ACF3159%7D .
    Go to http://meyerweb.com/eric/tools/dencoder/ to decode your guid. It should look like this know {5D1A8A88-0E2A-4F2C-8610-FB6E9ACF3159} .


2 Getting our Item ID

For getting our item id we are going to use JavaScript.

var id = SP.ListOperation.Selection.getSelectedItems()[0];

Note: This can be used if you are using a custom ribbon button. If you are using a custom menu button you have to use tokens.
e.g:

<UrlAction Url="javascript:TriggerWorkflow({ItemId});"/>


List of tokens:

~site : SPContext.Current.Web.ServerRelativeUrl

~sitecollection: SPContext.Current.Site.ServerRelativeUrl

{ItemId}: SPListItem.ID.ToString() or SPListItem["BdcIdentity"] (external data source)

{ItemUrl}: SPListItem.Url

{SiteUrl}: SPWeb.Url

{ListId}: SPList.ID.ToString(“B”)

{RecurrenceId}: SPListItem.RecurrenceID

{ListUrlDir}: SPList.RootFolder.Url

{Source}: Current Request Url


3 Getting our list id

 

For getting our list id we can use JavaScript our a Token.

var listId = SP.ListOperation.Selection.getSelectedList();

Token:

<UrlAction Url="javascript:TriggerWorkflow({ItemId},{ListId});"/>

 

4 Writing our JavaScript code

 

1 Without tokens

Create a new function.

function TriggerWorkflow(){
 
}

 

Getting the item id.

var id = SP.ListOperation.Selection.getSelectedItems()[0];

 

Getting the list id.

var listId = SP.ListOperation.Selection.getSelectedList();

 

Creating the url for the initial screen of the workflow. Here we need our templateID.

var templateID = "{5D1A8A88-0E2A-4F2C-8610-FB6E9ACF3159}";
 
var url = "/_layouts/IniWrkflIP.aspx?List=" + listId + "&ID=" + id + "&TemplateID=" + templateID;

 

Call the SP.UI.ModalDialog.showModalDialog.

var options{
     url: url,
     width:1000,
     height:700,
     title:"Triggered workflow"
}
 
SP.UI.ModelDialog.showModalDialog(options);

 

Full script.

function TriggerWorkflow(){
    var id = SP.ListOperation.Selection.getSelectedItems()[0];
 
    var listId = SP.ListOperation.Selection.getSelectedList();
 
    var templateId = "{5D1A8A88-0E2A-4F2C-8610-FB6E9ACF3159}";
 
    var url = "/_layouts/IniWrkflIP.aspx?List=" + listId + "&ID=" + id + "&TemplateID=" + templateID;
 
    var options{
        url:url,
        width:1000,
        height:700,
        title:"Triggered workflow"
    }
 
    SP.UI.Modeldialog.showModalDialog(options);    
}

 

2 Using Tokens

If you are using tokens the script stays the same you only have to change some small details.

function TriggerWorkflow(id,listId){
 
    var templateId = "{5D1A8A88-0E2A-4F2C-8610-FB6E9ACF3159}";
 
    var url = "/_layouts/IniWrkflIP.aspx?List=" + listId + "&ID=" + id + "&TemplateID=" + templateID;
 
    var options{
        url:url,
        width:1000,
        height:700,
        title:"Triggered workflow"
    }
 
    SP.UI.Modeldialog.showModalDialog(options);    
}
 
Posted by  Gilissen Timmy  on  9/15/2011
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation