SharePoint 工作流开发最佳做法SharePoint workflow development best practices

为使用 Visual Studio 在 SharePoint 中创建工作流的开发人员提供最佳实践集合。Provides a collection of best practices for developers using Visual Studio to create workflows in SharePoint.

工作流开发最佳实践Workflow development best practices

若要开发无差错的 SharePoint 工作流,最好遵循一些通用准则,或"最佳实践"。无论使用 SharePoint Designer 2013 还是 Visual Studio 2008 开发工作流,这一点都毋庸置疑。To develop error-free workflows for SharePoint, it is best to follow some general guidelines, or "best practices." This is the case whether you are using SharePoint Designer 2013 or Visual Studio 2012 for workflow development.

包含集成工作流的 SharePoint 相关应用程序必须编辑 workflowmanifest.xml 文件中的标记Apps for SharePoint that contain integrated workflows must edit a tag in the workflowmanifest.xml file

可通过将应用程序包的 workflowmanifest.xml 文件中的以下标记更改为 true,来区分包含集成工作流(可与父 Web 上的列表相关联)的 SharePoint 外接程序与普通工作流应用程序:SharePoint Add-ins that contain integrated workflows (which can be associated with lists on the parent web) are differentiated from normal workflow apps by changing the following tag to true in the workflowmanifest.xml file in the app package:


<SPIntegratedWorkflow xmlns="http://schemas.microsoft.com/sharepoint/2014/app/integratedworkflow">
    <IntegratedApp>true</IntegratedApp>
</SPIntegratedWorkflow>

当您使用记录到历史记录列表操作时,信息越多越好When you use the Log To History List action, more information is better

The Log To History List action (or LogToHistoryListActivity class if you are using Visual Studio) lets you record information about what a workflow has done at a given point in the workflow's lifecycle. This makes it one of the most important troubleshooting tools you have. The more information you provide at important points in the workflow, the easier it is to troubleshoot unexpected events.The Log To History List action (or LogToHistoryListActivity class if you are using Visual Studio) lets you record information about what a workflow has done at a given point in the workflow's lifecycle. This makes it one of the most important troubleshooting tools you have. The more information you provide at important points in the workflow, the easier it is to troubleshoot unexpected events.

有关详细信息,请参阅以下资源:For more information, see the following:

将您构建的每个字符串和变量的值都写入到历史记录列表中Write the value of every string and variable that you construct to the history list

如果您使用 Log to History List 操作将字符串和变量写入到历史记录列表中,则对使用 SharePoint 设计器创建的工作流进行调试将变得更加容易。Debugging workflows that were created using SharePoint Designer is much easier if you write strings and variables to the history list by using the Log to History List action.

有关详细信息,请参阅以下资源:For more information, see the following:

在工作流中的每个工作步骤或重要单元执行前后,都输出跟踪日志Output a trace log before and after each step or important unit of work in the workflow

为了帮助调试工作流,在每个工作的重要单元执行前后捕获有意义的信息非常重要;应该将这些重要信息提交到跟踪日志中。有关详细信息,请参阅以下资源:To assist with debugging workflows, it is important that you capture meaningful information prior to and following each significant unit of work; this information should be committed to trace logs. For more information, see the following:

验证变量均为非空且包含预期值Verify that variables are non-null and contain expected values

在工作流中使用变量之前,请确保没有空变量。此外,也要确保变量包含预期值,并且具有正确的数据类型。有关详细信息,请参阅 变量和参数Before using variables in your workflows, ensure there are no null variables. Also, ensure that variables contain expected values and are of the correct data type. For more information, see Variables and Arguments.

确保工作流文本字段中的字符串不超过 255 个字符Ensure that strings in workflow text fields do not exceed 255 characters

工作流文本字段中字符串的最大允许长度是 255 个字符。如果您将文本字段设置为超出此限制,则其超出 255 个字符的内容将被截掉。The maximum allowable length for strings in workflow text fields is 255 characters. If you set your text field to exceed this limit, its content will be truncated to 255 characters.

使用模拟时,在中立帐户上使用提升的权限Use elevated permissions on a neutral account when using impersonation

当在工作流中使用模拟步骤时,您应该使用中立帐户(即不与特定用户绑定的帐户)来编写工作流。这样可以防止您的工作流在作者帐户因故废弃时发生中断。When using impersonation steps in a workflow, you should author the workflow using a neutral account (that is, an account that is not tied to a specific user). This prevents your workflows from breaking if the author's account becomes obsolete for any reason.

有关详细信息,请参阅 使用 SharePoint 工作流平台通过提升的权限创建工作流For more information, see Create a workflow with elevated permissions by using the SharePoint Workflow platform.

在可重用工作流中,使用关联栏以确保列表字段无差错In reusable workflows, use Association columns to ensure error-free list fields

如果您创建的可重用工作流依赖于具有特定字段的列表,您可以 (1) 将工作流限制为含有指定字段的内容类型,或者 (2) 使字段成为关联栏。建议使用选项 2,这是因为内容类型有可能会有变化并导致工作流发生中断。If you create a reusable workflow that relies on its list having a specific field, you may either (1) restrict the workflow to a content type that has the specified field, or (2) make the field an association column. Option 2 is recommended because it's possible that a content type will change and cause the workflow to break.

工作流设计:在单一的工作流中模拟一个业务流程Workflow design: Model a business process in a single workflow

在可能的情况下,比起将工作流逻辑分解成几个小的工作流,在单一工作流中模拟业务流程的做法更好。Where possible, it is much better to model a business process in a single workflow than to break the workflow logic into several smaller workflows.

工作流设计:有效地使用审批操作Workflow design: Using the Approval action effectively

在可能的情况下,比起创建多个 Approval 操作,更有效的做法是在 Approval 操作中使用 Stages 功能。Where possible, instead of creating multiple Approval actions, it is more effective to use the Stages feature within an Approval action.

另请参阅See also