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
11  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments


Jaylyn  commented on  Tuesday, January 24, 2012  6:10 AM 
So much info in so few words. Tolosty could learn a lot.


ryepywz  commented on  Wednesday, January 25, 2012  9:48 AM 
22si9e lpurfeqeymxp


usnyacsugxe  commented on  Wednesday, January 25, 2012  1:57 PM 
oDgb6O , [url=http://ocnydknadive.com/]ocnydknadive[/url], [link=http://wjputjlavxnn.com/]wjputjlavxnn[/link], http://hkpdpbwggrom.com/


Bahutsauvage  commented on  Friday, January 27, 2012  5:55 AM 
topamax 8-) synthroid :(((


kiopelus  commented on  Thursday, February 02, 2012  5:52 AM 
tamoxifen %-D accutane 8]]]


Tibetisbusted  commented on  Thursday, February 16, 2012  6:17 PM 
Hey Mike! Thank you for the comments as well. I’ve updated my post to hopefully make it more clear to future readers that my intentions were not to confuse them about what you can’t do with SharePoint workflows but to use it as a lead-in to future posts about third-party SharePoint workflow/BPM solutions.



cakeroo  commented on  Sunday, February 19, 2012  2:42 AM 
NOVEMBER 19TH!!!The Tower Group's MONTHLY SOBRIETY DANCE!! 7pm till ??Food and DJ Entertainment!! It's going to be a great night and we look forward to seeing you there!! God Bless!



hendrahacker  commented on  Thursday, February 23, 2012  3:01 AM 
Paki, aunque con retraso, te comunico que todos los que lo solicist teis a trav s de los coemntarios est is admitidos/as



neocinetv  commented on  Saturday, February 25, 2012  2:40 AM 
Paki, aunque con retraso, te comunico que todos los que lo solicist teis a trav s de los coemntarios est is admitidos/as



ajjejaxn  commented on  Thursday, April 05, 2012  2:50 AM 
ajjejaxn


gxdcbhzt http://mypaydayloanscanada.ca/ payday Canada VajxNO instant Payday Loans --> 3152 [url=http://paydayloans2012.net/cash-advance.html]cash advances[/url] >:]]   commented on  Thursday, April 12, 2012  8:35 PM 
gxdcbhzt http://mypaydayloanscanada.ca/ payday Canada VajxNO instant Payday Loans 3152 [url=http://paydayloans2012.net/cash-advance.html]cash advances[/url] >:]]

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation