从控制台应用程序在sharepoint中执行站点工作流

时间:2018-02-21 13:22:19

标签: sharepoint csom

我正在尝试从控制台应用程序执行网站工作流程。当执行工作流程的代码运行时,它会发生错误

  

未处理的类型' Microsoft.SharePoint.Client.ServerException'发生在Microsoft.SharePoint.Client.Runtime.dll

其他信息:

  

无法从null对象调用方法或检索属性。以下调用堆栈返回的对象为null。 " GetWorkflowInteropService new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager()"

string userName = "username";
string password = "password";
string siteUrl = "https://share.example.com/sites/workflowsite";

string workflowName = "MyWorkflow";

using (ClientContext clientContext = new ClientContext(siteUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);

    clientContext.Credentials = new NetworkCredential(userName, securePassword);

     Web web = clientContext.Web;

     WorkflowAssociationCollection wfAssociations = web.WorkflowAssociations;

     WorkflowAssociation wfAssociation = wfAssociations.GetByName(workflowName);
     clientContext.Load(wfAssociation);

     clientContext.ExecuteQuery();

     WorkflowServicesManager manager = new WorkflowServicesManager(clientContext, web);
     InteropService workflowInteropService = manager.GetWorkflowInteropService();
     clientContext.Load(workflowInteropService);
     clientContext.ExecuteQuery();

     workflowInteropService.StartWorkflow(wfAssociation.Name, new Guid(), Guid.Empty, Guid.Empty, null);
     clientContext.ExecuteQuery( 
 }

1 个答案:

答案 0 :(得分:0)

以下代码供您参考:

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WorkflowServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace CSOMStartWorkflow {
    class Program {
        static void Main(string[] args) {

            Console.WriteLine("Enter the Office 365 Login Name");
            string loginId = Console.ReadLine();
            string pwd = GetInput("Password", true);

            Console.WriteLine("Web Url:");
            string webUrl = Console.ReadLine();

            Console.WriteLine("List Name:");
            string listName = Console.ReadLine();

            Console.WriteLine("Workflow Name");
            string workflowName = Console.ReadLine();

            var passWord = new SecureString();
            foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);

            using (var ctx = new ClientContext(webUrl)) {
                ctx.Credentials = new SharePointOnlineCredentials(loginId, passWord);

                var workflowServicesManager = new WorkflowServicesManager(ctx, ctx.Web);
                var workflowInteropService = workflowServicesManager.GetWorkflowInteropService();
                var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();
                var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();
                var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();

                var publishedWorkflowDefinitions = workflowDeploymentService.EnumerateDefinitions(true);
                ctx.Load(publishedWorkflowDefinitions);
                ctx.ExecuteQuery();

                var def = from defs in publishedWorkflowDefinitions
                          where defs.DisplayName == workflowName
                          select defs;

                WorkflowDefinition workflow = def.FirstOrDefault();

                if(workflow != null) {


                    // get all workflow associations
                    var workflowAssociations = workflowSubscriptionService.EnumerateSubscriptionsByDefinition(workflow.Id);
                    ctx.Load(workflowAssociations);
                    ctx.ExecuteQuery();

                    // find the first association
                    var firstWorkflowAssociation = workflowAssociations.First();

                    // start the workflow
                    var startParameters = new Dictionary<string, object>();

                    if (ctx.Web.ListExists(listName)) {
                        List list = ctx.Web.GetListByTitle(listName);

                        CamlQuery query = CamlQuery.CreateAllItemsQuery();
                        ListItemCollection items = list.GetItems(query);

                        // Retrieve all items in the ListItemCollection from List.GetItems(Query).
                        ctx.Load(items);
                        ctx.ExecuteQuery();
                        foreach (ListItem listItem in items) {
                            Console.WriteLine("Starting workflow for item: " + listItem.Id);
                            workflowInstanceService.StartWorkflowOnListItem(firstWorkflowAssociation, listItem.Id, startParameters);
                            ctx.ExecuteQuery();
                        }
                    }
                }
            }

            Console.WriteLine("Press any key to close....");
            Console.ReadKey();
        }

        private static string GetInput(string label, bool isPassword) {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("{0} : ", label);
            Console.ForegroundColor = ConsoleColor.Gray;

            string strPwd = "";

            for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true)) {
                if (keyInfo.Key == ConsoleKey.Backspace) {
                    if (strPwd.Length > 0) {
                        strPwd = strPwd.Remove(strPwd.Length - 1);
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                        Console.Write(" ");
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    }
                } else if (keyInfo.Key != ConsoleKey.Enter) {
                    if (isPassword) {
                        Console.Write("*");
                    } else {
                        Console.Write(keyInfo.KeyChar);
                    }
                    strPwd += keyInfo.KeyChar;

                }

            }
            Console.WriteLine("");

            return strPwd;
        }
    }
}

参考:Starting a SharePoint Online Workflow with the Client Side Object Model (CSOM)