插件无法激活工作流程(使用c#系统xaml WF)

时间:2012-06-26 15:35:02

标签: dynamics-crm-2011 dynamics-crm-online

  1. 列表项
  2. 1.>我创建了新的插件(在创建和更新任何自定义实体时触发(假设:ce1))

    2.>在线版注册。

    3.>该插件创建工作流程(使用c#代码的系统xaml工作流程)并激活 那个WF。

    4.>那个WF创建F9 .. :))

    5.>问题是...... :(

    6.>使用不同的C#应用​​程序仅创建WF未在在线版本中激活(即WF处于草稿模式)

    7.>但它在内部部署版本中运行f9。

    8.>它也在网上工作f9但使用CRM界面。

    9.>使用C#应用程序,它会在WF即将激活的行处抛出一个错误...错误是:模拟时无法发布工作流。

    感谢任何形式的帮助......请帮助我......

    感谢。

2 个答案:

答案 0 :(得分:0)

发布您的插件代码会很有帮助。但是如果你使用插件上下文获取IOrganizationService,它可能正在使用模拟,如果是这种情况,我会尝试使用正确的所有者创建一个新的OrganizationServiceProxy,看看是否能解决问题。

我无法访问在线版本,所以这是在黑暗中刺伤。

答案 1 :(得分:0)

//Getting Service as happens in PLUGIN
IOrganizationServiceFactory serviceFactory =          (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

//Configuring xaml to create workflow    

 string xamlWfPrefix = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Activity x:Class=""XrmWorkflow"" 
xmlns=""http://schemas.microsoft.com/netfx/2009/xaml/activities"" 
xmlns:mva=""clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""
xmlns:mxs=""clr-namespace:Microsoft.Xrm.Sdk;assembly=Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" 
xmlns:mxswa=""clr-namespace:Microsoft.Xrm.Sdk.Workflow.Activities;assembly=Microsoft.Xrm.Sdk.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""
xmlns:s=""clr-namespace:System;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" 
xmlns:scg=""clr-namespace:System.Collections.Generic;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" 
xmlns:srs=""clr-namespace:System.Runtime.Serialization;assembly=System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""
xmlns:this=""clr-namespace:"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <x:Members>
    <x:Property Name=""InputEntities"" Type=""InArgument(scg:IDictionary(x:String, mxs:Entity))"" />
    <x:Property Name=""CreatedEntities"" Type=""InArgument(scg:IDictionary(x:String, mxs:Entity))"" />
  </x:Members>
  <this:XrmWorkflow.InputEntities>
    <InArgument x:TypeArguments=""scg:IDictionary(x:String, mxs:Entity)"" />
  </this:XrmWorkflow.InputEntities>
  <this:XrmWorkflow.CreatedEntities>
    <InArgument x:TypeArguments=""scg:IDictionary(x:String, mxs:Entity)"" />
  </this:XrmWorkflow.CreatedEntities>
  <mva:VisualBasic.Settings>Assembly references and imported namespaces for internal implementation</mva:VisualBasic.Settings>
  <mxswa:Workflow>
    <Sequence DisplayName=""CreateStep1"">
      <Sequence.Variables>
        <Variable x:TypeArguments=""x:Object"" Name=""CreateStep1_1"" />
        <Variable x:TypeArguments=""x:Object"" Name=""CreateStep1_2"" />
      </Sequence.Variables>
      <Assign x:TypeArguments=""mxs:Entity"" To=""[CreatedEntities(&quot;CreateStep1_localParameter#Temp&quot;)]"" Value=""[New Entity(&quot;new_crm_new_sfeed&quot;)]"" />
      <mxswa:ActivityReference AssemblyQualifiedName=""Microsoft.Crm.Workflow.Activities.EvaluateExpression, Microsoft.Crm.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" DisplayName=""EvaluateExpression"">
        <mxswa:ActivityReference.Arguments>
          <InArgument x:TypeArguments=""x:String"" x:Key=""ExpressionOperator"">CreateCrmType</InArgument>
          <InArgument x:TypeArguments=""s:Object[]"" x:Key=""Parameters"">[New Object() { Microsoft.Xrm.Sdk.Workflow.WorkflowPropertyType.String, """;

                    xamlWfPrefix += displayname + @""", ""String"" }]</InArgument>
          <InArgument x:TypeArguments=""s:Type"" x:Key=""TargetType"">
            <mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
          </InArgument>
          <OutArgument x:TypeArguments=""x:Object"" x:Key=""Result"">[CreateStep1_1]</OutArgument>
        </mxswa:ActivityReference.Arguments>
      </mxswa:ActivityReference>
      <mxswa:SetEntityProperty Attribute=""new_name"" Entity=""[CreatedEntities(&quot;CreateStep1_localParameter#Temp&quot;)]"" EntityName=""new_crm_new_sfeed"" Value=""[CreateStep1_1]"">
        <mxswa:SetEntityProperty.TargetType>
          <InArgument x:TypeArguments=""s:Type"">
            <mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
          </InArgument>
        </mxswa:SetEntityProperty.TargetType>
      </mxswa:SetEntityProperty>
      <mxswa:ActivityReference AssemblyQualifiedName=""Microsoft.Crm.Workflow.Activities.EvaluateExpression, Microsoft.Crm.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" DisplayName=""EvaluateExpression"">
        <mxswa:ActivityReference.Arguments>
          <InArgument x:TypeArguments=""x:String"" x:Key=""ExpressionOperator"">CreateCrmType</InArgument>
          <InArgument x:TypeArguments=""s:Object[]"" x:Key=""Parameters"">[New Object() { Microsoft.Xrm.Sdk.Workflow.WorkflowPropertyType.String, """;

                    xamlWfPrefix += displayname;

                    string xamlWfSuffix = @", ""String"" }]</InArgument>
          <InArgument x:TypeArguments=""s:Type"" x:Key=""TargetType"">
            <mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
          </InArgument>
          <OutArgument x:TypeArguments=""x:Object"" x:Key=""Result"">[CreateStep1_2]</OutArgument>
        </mxswa:ActivityReference.Arguments>
      </mxswa:ActivityReference>
      <mxswa:SetEntityProperty Attribute=""new_description"" Entity=""[CreatedEntities(&quot;CreateStep1_localParameter#Temp&quot;)]"" EntityName=""new_crm_new_sfeed"" Value=""[CreateStep1_2]"">
        <mxswa:SetEntityProperty.TargetType>
          <InArgument x:TypeArguments=""s:Type"">
            <mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
          </InArgument>
        </mxswa:SetEntityProperty.TargetType>
      </mxswa:SetEntityProperty>
      <mxswa:CreateEntity EntityId=""{x:Null}"" DisplayName=""CreateStep1"" Entity=""[CreatedEntities(&quot;CreateStep1_localParameter#Temp&quot;)]"" EntityName=""new_crm_new_sfeed"" />
      <Assign x:TypeArguments=""mxs:Entity"" To=""[CreatedEntities(&quot;CreateStep1_localParameter&quot;)]"" Value=""[CreatedEntities(&quot;CreateStep1_localParameter#Temp&quot;)]"" />
      <Persist />
    </Sequence>
  </mxswa:Workflow>
</Activity>";

       //Calling method to create total xaml code that requires to create workflow
       string xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordCreated);
        //Initiating workflow to be create
        Workflow wfOnCreate = new Workflow()
              {
                Name = "CRM_DNNWorkflow_OnCreate",
                Type = new OptionSetValue(1),
                Category = new OptionSetValue(0),
                Scope = new OptionSetValue(2),
                LanguageCode = 1033, // U.S. English
                TriggerOnCreate = true,
                OnDemand = false,
                PrimaryEntity = logicalname, //Name of entity on which WF will be fired
                Description = @"Tracks Creation",
                Xaml = xamlWf
                    };


                    xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordDeleted);
                   //Another WF
                    Workflow wfOnDelete = new Workflow()
                    {
                        Name = "CRM_DNNWorkflow_OnDelete",
                        Type = new OptionSetValue(1),
                        Category = new OptionSetValue(0),
                        Scope = new OptionSetValue(2),
                        LanguageCode = 1033, // U.S. English
                        TriggerOnDelete = true,
                        OnDemand = false,
                        PrimaryEntity = logicalname,
                        Description = @"Tracks Deletion",
                        Xaml = xamlWf
                    };


                    xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordModified);
                    //Another WF
                    Workflow wfOnModify = new Workflow()
                    {
                        Name = "CRM_DNNWorkflow_OnModify",
                        Type = new OptionSetValue(1),
                        Category = new OptionSetValue(0),
                        Scope = new OptionSetValue(2),
                        LanguageCode = 1033, // U.S. English
                        TriggerOnUpdateAttributeList = attributeList,
                        OnDemand = false,
                        PrimaryEntity = logicalname,
                        Description = @"Tracks Modification",
                        Xaml = xamlWf
                    };


                    workflowOnCreateId = service.Create(wfOnCreate);
                    workflowOnDeleteId = service.Create(wfOnDelete);
                    workflowOnModifyId = service.Create(wfOnModify);           

                    //activate workflow (OnCreate)
                    ActivateWorkflow(workflowOnCreateId, service, serviceProvider);

                    //activate workflow (OnDelete)
                    ActivateWorkflow(workflowOnDeleteId, service, serviceProvider);

                    //activate workflow (OnModify)
                    ActivateWorkflow(workflowOnModifyId, service, serviceProvider);
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                tracingService.Trace("Exception: {0}", ex.ToString());
                throw;
                //throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
        }



        /// <summary>
        /// Builds the Xaml to define the function of a Workflow.
        /// </summary>
        /// <param name="prefix">Prefix to Xaml code</param>
        /// <param name="suffix">Suffix to Xaml code</param>
        /// <param name="status">Sets the description field of the new_crm_new_sfeed Entity</param>
        /// <returns></returns>
        private string BuildWorkflowXaml(string prefix,string suffix,string status)
        {
            return prefix + status + suffix;
        }



        /// <summary>
        /// Activates a Workflow.
        /// </summary>
        /// <param name="workflowId">Guid of the workflow to be created.</param>
        /// <param name="service">IOrganizationService Object.</param>
        private void ActivateWorkflow(Guid workflowId,IOrganizationService service,IServiceProvider serviceProvider) 
        {
            //Extract the tracing service for use in plug-in debugging.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            try
            {
                SetStateRequest activateRequest = new SetStateRequest
                {
                    EntityMoniker = new EntityReference(Workflow.EntityLogicalName, workflowId),
                    State = new OptionSetValue((int)WorkflowState.Activated),
                    Status = new OptionSetValue(2)
                };
                //**Error Occurs Here**
                OrganizationResponse respActivation = service.Execute(activateRequest);
                tracingService.Trace("ActivateResponse", respActivation);
                workflowGUIDs.Add(workflowId);
            }
            catch (InvalidPluginExecutionException exe)
            {
                tracingService.Trace("Exception: {0}", exe.ToString());
                throw;
            }            
        }





**FROM MY C# APPLICATION**



// Form the RetrieveOrganization Request.
   RetrieveOrganizationRequest request = new RetrieveOrganizationRequest();
   request.UniqueName = organizationUniqueName;
   RetrieveOrganizationResponse response = (RetrieveOrganizationResponse)discoveryServiceProxy.Execute(request);

// Form the URL.
   Uri uri = new Uri(response.Detail.Endpoints[EndpointType.OrganizationService]);

 //Form the OrganizationServiceProxy Object.
   orgServiceProxy = new OrganizationServiceProxy(uri, null, discoveryServiceProxy.ClientCredentials, discoveryServiceProxy.DeviceCredentials);


Entity Dnn_selectedEntity = new Entity("new_dnn_selected_entity");
Dnn_selectedEntity["new_name"] = entityMetadataId.Value.ToString();
Dnn_selectedEntity["new_trackedentity"] = entityMetadataId.Key;
orgServiceProxy.Create(Dnn_selectedEntity);
//After Creting this record That Plugin fires....



Thanks.