【问题标题】:How do I get WorkFlowActivityBase class in Visual Studio?如何在 Visual Studio 中获取 WorkFlowActivityBase 类?
【发布时间】:2019-11-05 20:33:51
【问题描述】:

我正在 Microsoft Dynamics CRM 中创建自定义工作流,以便在保存记录时自动更新字段。

论坛上的一位开发者提供了以下源代码;但他没有回答我的问题。

public class SalesRepActivity2 : WorkFlowActivityBase
{
    [Input("Sales Rep Name")]
    public InArgument<string> SalesRepName { get; set; }

    [Output("Sales Rep")]
    [ReferenceTarget("systemuser")]
    public OutArgument<EntityReference> SalesRep { get; set; }

    [Output("IsSuccess")]
    public OutArgument<bool> IsSuccess { get; set; }

    [Output("Message")]
    public OutArgument<string> Message { get; set; }

    protected override void Execute(
        CodeActivityContext activityContext,
        IWorkflowContext workflowContext,
        IOrganizationService CrmService,
        ITracingService trace)
    {
        try
        {
            string salesRepName = SalesRepName.Get(activityContext);

            if (string.IsNullOrWhiteSpace(salesRepName))
            {
                IsSuccess.Set(activityContext, false);
                Message.Set(activityContext, "Sales Rep Name not provided");
            }


            var QEsystemuser = new QueryExpression("systemuser");
            QEsystemuser.ColumnSet.AddColumns("salesrepname");
            QEsystemuser.Criteria.AddCondition("salesrepname", ConditionOperator.Equal, salesRepName);

            var results = CrmService.RetrieveMultiple(QEsystemuser);

            if (results == null || !results.Entities.Any())
            {
                IsSuccess.Set(activityContext, false);
                Message.Set(activityContext, "User with " + salesRepName + " not found");
                return;
            }

            if (results.Entities.Count > 1)
            {
                IsSuccess.Set(activityContext, false);
                Message.Set(activityContext, "Multiple users found with same name : " + salesRepName);
                return;
            }


            IsSuccess.Set(activityContext, true);
            SalesRep.Set(activityContext, results.Entities.Single().ToEntityReference());
        }
        catch (Exception ex)
        {
            IsSuccess.Set(activityContext, false);
            Message.Set(activityContext, "An error occurred trying to find user : " + ex.Message);
        }

    }

我试图让代码在我的机器上编译。

我安装了以下 NuGet 包,它们解决了大部分错误: Microsoft.Xrm.Sdk.Workflow.2015 Microsoft.Xrm.Sdk.2015

但我的项目无法解析 WorkFlowActivityBase 类。

是否有我应该设置的参考或我应该安装的 NuGet 包来解决这个问题?

谢谢。

【问题讨论】:

    标签: visual-studio dynamics-crm crm


    【解决方案1】:

    WorkFlowActivityBase 是一个自定义基类,它实现了任何人都可以编写的CodeActivity (System.Activities),它不是官方的 Dynamics 类。你可以在网上找到几十个课程。 基本上,您应该使用CodeActivity。这里举个例子:

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/workflow/sample-create-custom-workflow-activity

    【讨论】:

    • 我尝试改用 CodeActivity,但在这种情况下,我需要 CrmService 来执行以下操作: var results = CrmService.RetrieveMultiple(QEsystemuser);如何访问 CrmService?
    • 你引用的代码中的CrmService对象是一个实现了IOrganizationService的对象。查看我在回答中链接的文档,特别是“获取上下文信息”和“使用组织服务”部分
    • 看起来 Ziv 还添加了第二个答案,提供了这种实现的示例。
    【解决方案2】:

    您发布的代码不是一个好的起点,因为它大量使用了专有对象。 Ziv 提供了有关扩展 CodeActivity 类的良好信息,但我不建议从那里开始。而是阅读有关如何开发自定义工作流活动以及使用基本 Microsoft 类编写工作流的信息,以便了解它们的工作原理:
    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/workflow/workflow-extensions

    一旦您对创建自定义工作流程有了一定的经验,并且了解了基础对象的局限性,然后再回去实现一些更奇特的东西。

    【讨论】:

      【解决方案3】:

      这里以documentation 为例:

      namespace Microsoft.Crm.Sdk.Samples
      {
          public sealed class SimpleSdkActivity : CodeActivity
          {
              protected override void Execute(CodeActivityContext executionContext)
              {
                  //Create the tracing service
                  ITracingService tracingService = executionContext.GetExtension<ITracingService>();
      
                  //Create the context
                  IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                  IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                 ...
                 var systemUsers = service.RetrieveMultiple(QEsystemuser)
             }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-23
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        相关资源
        最近更新 更多