关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复244或者20170306可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me 。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Activities; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk; using System.Reflection; namespace CrmVSSolution.Workflow { public sealed class PostTestUpdate : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { ITracingService tracingService = executionContext.GetExtension<ITracingService>(); tracingService.Trace("进入自定义工作流活动CrmVSSolution.Workflow.PostTestUpdate"); IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId); StringBuilder sb = new StringBuilder(); PropertyInfo[] properties = context.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo proInfo in properties) { sb.Append(proInfo.Name); sb.Append(" = "); if (proInfo.CanRead) { sb.Append(proInfo.GetValue(context, null)); } else { sb.Append("Cannot read value"); } sb.Append("("); sb.Append(proInfo.PropertyType.ToString()); sb.Append(");"); sb.Append("\n"); } tracingService.Trace("工作流中context的所有参数如下:\n"); tracingService.Trace(sb.ToString()); sb.Clear(); foreach(var item in context.InputParameters) { sb.Append(item.Key); sb.Append(" = "); if (item.Value != null)//注意有的属性的值为null,比如InputArguments { sb.Append(item.Value.ToString()); } else { sb.Append("null"); } sb.Append("\n"); } tracingService.Trace("工作流中context.InputParameter的参数如下:\n"); tracingService.Trace(sb.ToString()); sb.Clear(); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { sb.Append("工作流中包括了Target参数,它是Entity类型.\n"); var currentEntity = context.InputParameters["Target"] as Entity; sb.Append("Target参数包括如下属性:\n"); foreach (var attr in currentEntity.Attributes) { sb.Append(attr.Key); sb.Append(" = "); if (attr.Value != null)//注意有的属性值为null,比如modifiedonbehalfby,所以要加上判断 { sb.Append(attr.Value.ToString()); } else { sb.Append("null"); } sb.Append("\n"); } tracingService.Trace(sb.ToString()); } tracingService.Trace("结束自定义工作流活动CrmVSSolution.Workflow.PostTestUpdate"); throw new InvalidWorkflowException("有时候不抛出异常不行啊!"); } } }
Dynamics 365中自定义工作流活动获取的上下文分析及注意事项
原文:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Custom_Activity_Context.html