首先我们需要确定windows workflow foundation 已经安装.
创建之后先移除MyCustomWorkflows 里面的 Activity.xaml
从packages\Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.0.2.12\tools 路径中添加以下两个reference
复制下面的代码到我们新建的GetTaxWorkflow.cs
因为我们在CRM里面定义的custom entity是键值对的形式出现, 所以我们需要input值和output值.
我们有以下几个方式获取数据. 这里我们使用Query By Attribute
1. UsingRetrieve (必须获得GUID)
2. QueryExpression (可以实现复杂的逻辑)
3. Query By Attribute (简化的QueryExpression)
4. FatchXML
5. LINQ
public class GetTaxWorkflow : CodeActivity
{
[Input("Key")]
public InArgument<string> Key { get; set; }
[Output("Tax")]
public OutArgument<string> Tax { get; set; }
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 key = Key.Get(executionContext);
// Get data from Configuraton Entity
// Call organization web service
QueryByAttribute query = new QueryByAttribute("contoso_configuration");
query.ColumnSet = new ColumnSet(new string[] { "contoso_value" });
query.AddAttributeValue("contoso_name", key);
EntityCollection collection = service.RetrieveMultiple(query);
if (collection.Entities.Count != 1)
{
tracingService.Trace("Something is wrong with configuration");
}
Entity config = collection.Entities.FirstOrDefault();
tracingService.Trace(config.Attributes["contoso_value"].ToString());
Tax.Set(executionContext, config.Attributes["contoso_value"].ToString());
}
}
记得注册这个assembly
然后让我们build一下项目. 我们的workflow dll就会在debug中