您需要将您的插件注册到SetState
和SetStateDynamicEntity
消息,并将Pre-Operation
作为执行阶段。以此代码为例:
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace TestPlugin
{
public class UpdateBoolFields : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
try
{
if (context.InputParameters.Contains("EntityMoniker") &&
context.InputParameters["EntityMoniker"] is EntityReference)
{
EntityReference targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
if (state.Value == 1)// I'm not sure is 1 for deactivate
{
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity contact = service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet(true));
contact["field1"] = false;
contact["field2"] = false;
contact["field3"] = false;
service.Update(contact);
}
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
我回复了您的问题。有一件事:如果你说“微软不以他们精心编写的手册而闻名”,至少向我们展示了你的陈述的一些证明,否则就是对MSDN Library工作(和工作)的所有人的侮辱。 –
@GuidoPreite 非常感谢!我不是说**全部** MSDN文档不好。我说的是他们中的一些人可以使用更多的工作。我可以使用[this](http://msdn.microsoft.com/zh-cn/library/gg328574.aspx)为例。这个文档并不能帮助像我这样的初学者理解那里正在发生的事情。根据条目的评分:不是它也可以帮助很多其他用途......我不打算在这里讨论StackOverflow。 –