SharePoint 通过各种对象触发工作流

  前言

  真的是好久好久之前存的草稿了,当时只保存了代码,大家看看还有用不,哈哈

  正文

  通过JavaScript去触发列表工作流,代码如下:

function startWF(itemID, listId) {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var workflowServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
    var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
    var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);
    //run all workflows associated with the list
    context.load(subscription);
    context.executeQueryAsync(function(sender, args) {
        foreach(var workflowSubscription in subscriptions) {
            var inputParameters = {};
            workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
        }
        context.executeQueryAsync(Function.createDelegate(this, this.onStartSucceeded), Function.createDelegate(this, this.onStartFailed));
    },
    Function.createDelegate(this, this.onLoadFailed));

}

function onStartSucceeded(sender, args) {
    alert("Workflow Started Successfully.");
}

function onStartFailed(sender, args) {
    alert('Workflow Initiation Failed  ' + args.get_message() + '\n' + args.get_stackTrace());
}

function onLoadFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

  代码二,启动站点工作流,如下:

function LoadScripts() {
    SP.SOD.executeFunc("sp.js", "SP.ClientContext",
    function() {
        SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));
        SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager", StartSiteWorkflow);
    })
}

function StartSiteWorkflow() {

    var context = SP.ClientContext.get_current();

    var web = context.get_web();

    //Workflow Services Manager
    var wfServicesManager = new SP.WorkflowServices.WorkflowServicesManager(context, web);

    //Workflow Interop Service used to interact with SharePoint 2010 Engine Workflows
    var interopService = wfServicesManager.getWorkflowInteropService()

    //Initiation Parameters have to be in a plain JS Object.
    var initiationParameters = {
        FirstName: "Vardhaman",
        LastName: "Deshpande"
    };

    //Start the Site Workflow by Passing the name of the Workflow and the initiation Parameters.
    interopService.startWorkflow("My Workflow", null, null, null, initiationParameters);

    context.executeQueryAsync(function() {
        console.log("workflow started");
    },
    function(sender, args) {
        console.log(args.get_message());
    });
}

jQuery(document).ready(function() {
    LoadScripts();
});

  代码三,通过PowerShell去启动列表工作流

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$sourceWebURL = 'http://devserver'
$TargetWorkflow = 'AutoUpdateAppointmentStatus'
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists['ServiceAppointments']
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
$sub = $wfm.GetWorkflowSubscriptionService()
$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq "$TargetWorkflow"}
$wfis = $wfm.GetWorkflowInstanceService()
$object = New-Object 'system.collections.generic.dictionary[string,object]'
$object.Add("itemId", 42);
$object.Add("WorkflowStart", "StartWorkflow");
$wfis.StartWorkflowOnListItem($WF, 42, $object)

  第四部分,通过CSOM操作工作流,自己只收藏了文档连接,大家看一看吧

https://docs.microsoft.com/en-us/sharepoint/dev/general-development/working-with-the-sharepoint-workflow-services-client-side-object-model

  结束语

 

  真的是好久之前的代码了,应该是之前研究工作流,保存的。

  大家可以试试,自己肯定也是觉得靠谱,才保存在博客草稿的~

posted @ 2022-03-08 21:38  霖雨  阅读(84)  评论(0编辑  收藏  举报