Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Created July 3, 2015 11:10
Show Gist options
  • Save devendrasv/1cc7396e29cabaa05514 to your computer and use it in GitHub Desktop.
Save devendrasv/1cc7396e29cabaa05514 to your computer and use it in GitHub Desktop.
Initiation Form
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="../_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="../_layouts/15/sp.js"></script>
<script type="text/javascript" src="../_layouts/15/sp.workflowservices.js"></script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
Page Title
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderId="PlaceHolderMain" runat="server">
<table>
<tr><td>String:<br /><textarea id="strInput" rows="1" cols="50"></textarea><br /><br /></td></tr>
<tr><td>
<input type="button" name="startWorkflowButton" value="Start" onClick="StartWorkflow()" />
<input type="button" name="cancelButton" value="Cancel" onClick="RedirFromInitForm()" />
<br />
</td></tr>
</table>
<script type="text/javascript">
// ---------- Start workflow ----------
function StartWorkflow() {
var errorMessage = "An error occured when starting the workflow.";
var subscriptionId = "", itemId = "", redirectUrl = "";
var urlParams = GetUrlParams();
if (urlParams) {
//itemGuid = urlParams["ItemGuid"];
itemId = urlParams["ID"];
redirectUrl = urlParams["Source"];
subscriptionId = urlParams["TemplateID"];
}
if (subscriptionId == null || subscriptionId == "") {
// Cannot load the workflow subscription without a subscriptionId, so workflow cannot be started.
alert(errorMessage + " Could not find the workflow subscription id.");
RedirFromInitForm(redirectUrl);
}
else {
// Set workflow in-arguments/initiation parameters
var wfParams = new Object();
var strInputValue = document.getElementById("strInput").value;
if (strInputValue) {
wfParams['strArg'] = strInputValue;
}
// Get workflow subscription and then start the workflow
var context = SP.ClientContext.get_current();
var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
var wfDeployService = wfManager.getWorkflowDeploymentService();
var subscriptionService = wfManager.getWorkflowSubscriptionService();
context.load(subscriptionService);
context.executeQueryAsync(
function (sender, args) { // Success
var subscription = null;
// Load the workflow subscription
if (subscriptionId)
subscription = subscriptionService.getSubscription(subscriptionId);
if (subscription) {
if (itemId != null && itemId != "") {
// Start list workflow
wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, wfParams);
}
else {
// Start site workflow
wfManager.getWorkflowInstanceService().startWorkflow(subscription, wfParams);
}
context.executeQueryAsync(
function (sender, args) {
// Success
RedirFromInitForm(redirectUrl);
},
function (sender, args) {
// Error
alert(errorMessage + " " + args.get_message());
RedirFromInitForm(redirectUrl);
}
)
}
else {
// Failed to load the workflow subscription, so workflow cannot be started.
alert(errorMessage + " Could not load the workflow subscription.");
RedirFromInitForm(redirectUrl);
}
},
function (sender, args) { // Error
alert(errorMessage + " " + args.get_message());
RedirFromInitForm(redirectUrl);
}
)
}
}
// ---------- Redirect from page ----------
function RedirFromInitForm(redirectUrl) {
window.location = redirectUrl;
}
// ---------- Returns an associative array (object) of URL params ----------
function GetUrlParams() {
var urlParams = null;
if (urlParams == null) {
urlParams = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
urlParams[key] = decodeURIComponent(value);
});
}
return urlParams;
}
</script>
</asp:Content>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment