Nat*_*ate 3 .net plugins dynamics-crm-2011
这是我为CRM 2011编写的插件的一个工作示例.我在此插件的插件注册工具中创建了一个"创建"步骤.这执行得很好.我还为插件注册了"更新"步骤.由于返回的主要联系人为空,因此无法执行.这些步骤都注册为异步.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.IO;
using System.Net;
namespace CRMNewsletterPlugin
{
public class NewsletterSignup : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
tracingService.Trace("Begin load");
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
tracingService.Trace("We have a target.");
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (!entity.GetAttributeValue<bool>("new_receivesnewsletter"))
{
try
{
//check if the account number exist
string emailAddress = entity.GetAttributeValue<string>("emailaddress1");
EntityReference primaryContact = entity.GetAttributeValue<EntityReference>("primarycontactid");
// UPDATE STEP FAILS HERE
if (primaryContact == null)
{
tracingService.Trace("Primary Contact is null");
}
Entity contact = service.Retrieve("contact", primaryContact.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(new string[] { "fullname" }));
string fullname = contact.GetAttributeValue<string>("fullname");
string name = entity.GetAttributeValue<string>("name");
WebRequest req = WebRequest.Create("http://localhost");
string postData = "cm-name=" + fullname + "&cm-ddurhy-ddurhy=" + emailAddress + "&cm-f-jddkju=" + name;
tracingService.Trace(postData);
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;
tracingService.Trace("Sending info");
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
tracingService.Trace("Info sent");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
tracingService.Trace(returnvalue);
entity.Attributes["new_receivesnewsletter"] = true;
service.Update(entity);
tracingService.Trace("Newsletter field set");
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}//end class
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*ter 10
InputParameters ["Target"]中包含的实体仅包括更新中提交的已更改字段,而不是所有字段.您的插件适用于Create,因为InputParameters ["Target"]始终包含Create中的所有字段.
要解决此问题,您需要在PluginRegistrationTool中为包含primarycontactid字段的步骤添加PreImage.PreImage将始终包含您在更新之前指定的字段的值.
诀窍是首先检查InputParameters ["Target"]上的primarycontactid,因为它将包含最新值(例如,如果用户在同一个保存期间更新了new_receivesnewsletter和primarycontactid).如果InputParameters ["Target"]不包含primarycontactid,则返回到PreImage,访问类似这样的内容(假设您将PreImage的别名设置为"preimage"):
context.PreEntityImages["preimage"].GetAttributeValue<EntityReference>("primarycontactid")
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!