Skip to content

Instantly share code, notes, and snippets.

@madhur
Created January 9, 2012 18:22
Show Gist options
  • Save madhur/1584225 to your computer and use it in GitHub Desktop.
Save madhur/1584225 to your computer and use it in GitHub Desktop.
SharePoint 2010 Approval workflow with dynamic approvers
<div id='intake'>
<img src="/sites/nishantverma/GBSRequest/SiteAssets/loading.gif" />
<br/>
Submitting your request for approval
</div>
<script type="text/javascript" src="/sites/nishantverma/GBSRequest/SiteAssets/jquery-1.7.1.js"></script>
<script type="text/javascript" src="/sites/nishantverma/GBSRequest/SiteAssets/jquery.SPServices-0.7.0.min.js"></script>
<script type="text/javascript">
// Get the Item from the Query String
var ItemID= getParameterByName("ItemID");
var loginName, approverName,approverID;
var debug=0;
// Construct the CAML Query
var CamlQuery = "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+ItemID+"</Value></Eq></Where></Query>";
// Make AJAX call
// Fire GetListItems web service to find the Approval contact
$().SPServices({
operation: "GetListItems",
async: false,
CAMLQuery: CamlQuery ,
listName: "Intake Requests",
CAMLViewFields: "<ViewFields><FieldRef Name='Band_x0020_45_x0020_Approval' /></ViewFields>",
completefunc: function (xData, Status)
{
events = new Array();
$(xData.responseXML).find("z\\:row").each(function()
{
approverName=$(this).attr("ows_Band_x0020_45_x0020_Approval");
var index=approverName.indexOf(";");
approverID=approverName.slice(0,index);
approverName=approverName.substring(index+2,approverName.length);
if(debug)
{
alert(approverName);
console.log(approverName);
alert(approverID);
console.log(approverID);
}
})
}
})
// We will just get the DisplayName and ID, but not the Login Name, we will fire another web service to fetch the loginName of the User
if(approverName!=null)
{
var xmlData ="<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>User Information List</listName><query><Query xmlns=''><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+approverID+"</Value></Eq></Where><OrderBy><FieldRef Name='Title' /></OrderBy></Query></query><viewFields><ViewFields xmlns='' /></viewFields><queryOptions><QueryOptions xmlns='' /></queryOptions></GetListItems></soap:Body></soap:Envelope>";
$.ajax({
url: "/sites/nishantverma/_vti_bin/lists.asmx",
type: "POST",
async: false,
dataType: "xml",
data: xmlData,
complete:SuccessFunc,
error: ErrorFunc,
contentType: "text/xml; charset=\"utf-8\""
});
function SuccessFunc(result)
{
//xml node with namespace need to be handled differently for jQuery
$(result.responseXML).find("z\\:row").each(function()
{
if(debug)
{
alert($(this).attr("ows_Name"));
console.log($(this).attr("ows_Name"));
}
loginName=$(this).attr("ows_Name");
});
}
function ErrorFunc(result)
{
alert(result.responseText);
}
// Fire the workflow
if(loginName!=null)
{
var assocData='<dfs:myFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<dfs:queryFields></dfs:queryFields>'+
'<dfs:dataFields>'+
'<d:SharePointListItem_RW>'+
'<d:Approvers>'+
'<d:Assignment>'+
'<d:Assignee>'+
'<pc:Person><pc:DisplayName></pc:DisplayName><pc:AccountId>'+loginName+'</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person>'+
'</d:Assignee>'+
'<d:Stage xsi:nil="true" />'+
'<d:AssignmentType>Serial</d:AssignmentType>'+
'</d:Assignment>'+
'</d:Approvers>'+
'<d:ExpandGroups>true</d:ExpandGroups>'+
'<d:NotificationMessage>Please approve</d:NotificationMessage>'+
'<d:DueDateforAllTasks xsi:nil="true" /><d:DurationforSerialTasks xsi:nil="true" />'+
'<d:DurationUnits>Day</d:DurationUnits>'+
'<d:CC />'+
'<d:CancelonRejection>true</d:CancelonRejection>'+
'<d:CancelonChange>false</d:CancelonChange>'+
'<d:EnableContentApproval>false</d:EnableContentApproval>'+
'</d:SharePointListItem_RW>'+
'</dfs:dataFields>'+
'</dfs:myFields>';
$().SPServices({
operation: "StartWorkflow",
item: "http://sp.madhurmoss.com/sites/nishantverma/GBSRequest/Lists/Intake%20Requests/" + ItemID + "_.000",
templateId: "{10E4D465-A9B6-4146-BD50-E9A78888548D}",
workflowParameters: assocData,
completefunc: function()
{
if(debug)
{
alert('Workflow Started');
}
$("#intake").html("Your Intake Request has been sent to "+approverName+" for his approval. Thanks for submitting the request. This page will redirect to homepage after few seconds");
window.setTimeout(function()
{
window.location.href = '/sites/nishantverma/GBSRequest';
}, 5000)
}
});
}
}
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment