51CTO 博客地址:https://blog.51cto.com/13969817
需求:某企业开发了一个SharePoint应用程序来创建费用报告的审批工作流,应用程序必须在开始工作流之前收集用户输入,在工作流中必须使用ASP.net forms,需要使用哪一种类型的forms?
解决方案:我们可以考虑从工作流启动它的用户那里获取信息,然后在稍后的工作流中引用这些信息,比如:我们正在设计一个分配文档审查任务的工作流,在这种类型的工作流中,总是自动地将审批任务分配给相同的参与者,并总是使用相同的公式计算到期日,这种情况非常有意义,但是在工作流中,你希望手动启动工作流的人同时指定审阅者和到期日期,你可以通过向工作流初始化表单添加自定义字段并要求手动启动工作流来设置这些属性。
该解决方案适用于SharePoint 2013/2016
具体操作过程:
1. 创建一个SP List作为Workflow Initiation form,并添加相应的column
2. 修改Start InitiationForm列表上的默认新表单。
3. 编写custominitial.js文件,这里我们只是从目标列表项中提取信息,以便在自定义启动表单中显示,参照(CSOM)脚本如下:
//Create your JavaScript Namespace
var INCWORX = INCWORX || {}
INCWORX.SP = INCWORX.SP || {}
INCWORX.SP.WFInitiation = {
itemID: null,
targetItem: null,
init: function() {
// Hide columns that the user does not need to see
$ (“[id^=’Title’]”).closest(‘tr’).hide();
$ (“[id^=’TargetItemID’]”).closest(‘tr’).hide();
// Change the “Save” button to say “Start Workflow”
$ (“[id$=’SaveItem’]”).prop(‘value’, ‘Start Workflow’)
// Retrieve the List Item ID from the query string that this workflow will be targeting
INCWORX.SP.WFInitiation.itemID = $.getUrlVar(‘itemid’);
if(!INCWORX.SP.WFInitiation.itemID)
{
// Add some custom message incase the target item ID was not found in the QueryString
$ (“#CustomDisplayDiv”).html(“ERROR: The item ID was not located.”);
$ (“[id$=’SaveItem’]”).hide(); // Hide the Save button so the form cannot be submitted}
// Set the target item ID in the custom column we created so we have access to it after the form is saved
$ (“[id^=’TargetItemID’]”).val(INCWORX.SP.WFInitiation.itemID);
$ (“[id^=’Title’]”).val(“Custom Initation for ” + INCWORX.SP.WFInitiation.itemID);
// Build your client context
var clientContext = new SP.ClientContext();
var web = clientContext.get_web();
// Retrieve the list of the target item
var list = web.get_lists().getByTitle(“RandomApproval”);
// Retrieve the target item with the columns we will display on the initiation form
INCWORX.SP.WFInitiation.targetItem = list.getItemById(INCWORX.SP.WFInitiation.itemID, “Include(Title,ApprovalType,DateRequested,Reason,Author)”);
clientContext.load(INCWORX.SP.WFInitiation.targetItem);
clientContext.executeQueryAsync(Function.createDelegate(this, INCWORX.SP.WFInitiation.onQuerySucceeded), Function.createDelegate(this, INCWORX.SP.WFInitiation.onQueryFailed));
},
onQuerySucceeded: function() {
var displayDiv = $ (“#CustomDisplayDiv”);
// Retrieve the columns to be displayed on the initiation form
var itemTitle = INCWORX.SP.WFInitiation.targetItem.get_item(“Title”);
var itemApprovalType = INCWORX.SP.WFInitiation.targetItem.get_item(“ApprovalType”);
var itemDate = new Date(INCWORX.SP.WFInitiation.targetItem.get_item(“DateRequested”));
var itemReason = INCWORX.SP.WFInitiation.targetItem.get_item(“Reason”);
var itemAuthor = INCWORX.SP.WFInitiation.targetItem.get_item(“Author”).get_lookupValue();
displayDiv.append(“<h2>” + itemTitle + “</h2>”);
displayDiv.append(“<strong>Requestor: </strong>” + itemAuthor);
displayDiv.append(“<br >”);
displayDiv.append(“<strong>Approval Type: </strong>” + itemApprovalType);
displayDiv.append(“<br >”);
// Format the date, remember that getMonth is 0 based
displayDiv.append(“<strong>Date Requested: </strong>” + (itemDate.getMonth()+1) + “/” + itemDate.getDate() + “/” + itemDate.getFullYear());
displayDiv.append(“<br >”);
displayDiv.append(“<strong>Reason: </strong>” + itemReason);
},
onQueryFailed: function(sender, args) {
$ (“#CustomDisplayDiv”).html(“ERROR: ” + args.get_message());
}
}
jQuery (document).ready(function ()
{
// Function to retrieve QueryString parameters from the URL
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf(‘?’) + 1).split(‘&’);
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split(‘=’);
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
// Make sure the custom JavaScript is loaded after the SharePoint client object model is loaded
SP.SOD.executeFunc(‘sp.js’,’SP.ClientContext’,INCWORX.SP.WFInitiation.init);
});
4. 使用SharePoint Designer针对Initiation List创建Workflow,目标List命名为RandomApproval
之后就可以在列表中新建item时,使用该Workflow了。
说明:并非每个工作流初始化表单都这样复杂,但你可以通过该示例了解可以使用SharePoint App或者部署代码的方式来创建一个自定义工作流初始化表单。