Posts tonen met het label start workflow from code. Alle posts tonen
Posts tonen met het label start workflow from code. Alle posts tonen

donderdag 29 maart 2012

Starting workflow from Code Sharepoint


I wanted to start a Nintex workflow by clicking a button. As a beginner I have been stuck on this subject for a few days and thought I should share my solution ! This should also be possible with a "regular" Sharepoint workflow.

I needed a button in my list which I could use to lend out or return a book. I wanted this button in my list to improve usability. I have used Steven Van Craen's advanced calculated field to insert a button in my list.

http://www.moss2007.be/blogs/vandest/archive/2009/03/31/advanced-computed-field.aspx

The code behind my button:
This code triggers a function in my webservice called SayHello.
It does not really matter which data type is used in this case I use json.
 <HTML><![CDATA[  
      <script type="text/javascript">  
      function TriggerWorkflow(){  
       var vRootUrl = document.location.protocol + "//" + document.location.hostname + "/_vti_bin/Service1.svc/";  
           var id = SP.ListOperation.Selection.getSelectedItems()[0].id;  
     var parameter = $('#txtInput').val();  
     $.ajax(  
           {  
                cache: false,  
                type: "POST",  
                contentType: "application/json; charset=utf-8",  
                url: (vRootUrl + "SayHello"),  
                data: JSON.stringify({ id : id }),  
                dataType: "json",  
                success: function (data) {  
                  alert('success');  
                },  
                error: function () {  
                  alert('failed');  
                }  
           });  
      }  
           if(']]></HTML>  
 <field Name="Status" />  
 <HTML><![CDATA[' != 'Uitgeleend') {  
           document.write('<input type="button" value ="Leen uit" onclick="TriggerWorkflow();" />');  
      }  
      }  
 </script>]]></HTML>  
Now let's build the webservice
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Runtime.Serialization;  
 using System.ServiceModel;  
 using System.ServiceModel.Web;  
 using System.Text;  
 namespace WcfService1  
 {  
   [ServiceContract]  
   public interface IService1  
   {  
     [OperationContract]  
     [WebInvoke(Method = "POST",  
           BodyStyle = WebMessageBodyStyle.Wrapped,  
           ResponseFormat = WebMessageFormat.Json,  
           RequestFormat = WebMessageFormat.Json)]  
     void SayHello(int id);  
   }  
 }  
code triggering the workflow :
 namespace WcfService1  
 {  
   // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.  
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
   public class Service1 : IService1  
   {  
     public void SayHello(int id)  
     {  
       using (SPSite site = new SPSite("http://intranet/"))  
       {  
         using (SPWeb web = site.RootWeb)  
         {  
           SPList destination = web.Lists["Books_lend_out"];  
           SPList source = web.Lists["Books"];  
           SPListItem sourceList = source.GetItemById(id);  

           //'obtain an instance of SPWorkflowManager which will be used to start the workflow  
           var manager = site.WorkflowManager;

           //'get all workflows associated with the list  
           var associationCol = source.WorkflowAssociations;
  
           //'iterate through all the workflow and lookup for the workflow to be started  
           foreach (SPWorkflowAssociation association in associationCol)  
           {  
             if (association.Name == "New Loan")  
             {  
               //'get workflow association data  
               var assData = association.AssociationData;
 
               //'start the workflow  
               web.AllowUnsafeUpdates = true;  
               manager.StartWorkflow(sourceList, association, assData);  
               web.AllowUnsafeUpdates = false;  
             }  
           }  
         }  
       }  
     }  
   }  
 }