Daren 的个人资料Daren Turner日志列表 工具 帮助

日志


3月28日

Replicating CRM Records to another CRM System

A friend of mine called me up and asked if it was possible to create records in one CRM system when they are created in another one.  Well here is a quick example of how to do it.   

Assumptions

  • Both CRM Systems have the exact same fields for the records you will copying.
  • All records in the two CRM systems have the same guids for matching records.

If there are discrepencies with the guids in each system, there will be issues with this example.   When the postImageEntityXml is Deserialized into a DynamicEntity, it has the guid of the source system. When it's created in the target system, it's created with the same guid. 

For this example I will show you a simple Post Callout to replicate a Create action, but I recommend sending these transactions to a Message Queue and then processing them from there.   It's also not only limited to Create, the same thing can be done for other actions.

Code

public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml)
{
    //Create the Service and connect to remote server.
   CrmService service = new CrmService();
   service.Url = "http://<CRMSERVER>/mscrmservices/2006/crmservice.asmx"

   //build the credentials for access if the current user doesn't have access.
   service.Credentials = new System.Net.NetworkCredential("username","password","domain");

    DynamicEntity entity = (DynamicEntity)Serialization.DeserializeBusinessEntity(postImageEntityXml);

    TargetCreateDynamic targetCreate = new TargetCreateDynamic();
    targetCreate.Entity = entity;

    CreateRequest request = new CreateRequest();

    request.Target = targetCreate;

    CRMSDK.CreateResponse response = (CRMSDK.CreateResponse) service.Execute(request);
}
private Object DeserializeBusinessEntity( string xml)
{
    XmlTextReader readerText = new XmlTextReader(new StringReader(xml));
    try
    {
    // De-serialize XML stream using .NET XmlSerializer class
    XmlRootAttribute root = new XmlRootAttribute("BusinessEntity");
    root.Namespace = "http://schemas.microsoft.com/crm/2006/WebServices"
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(BusinessEntity), root);
    BusinessEntity entity = (BusinessEntity)xmlSerializer.Deserialize(readerText);

    return entity;
    }
    catch (Exception) { throw; }
}

This code isn't entity specific so it will work for most of the entities in CRM. All the entity information is in the postImageEntityXml and is Deserialized into the DynamicEntity.

Callout Registration

<callout entity="account" event="PostCreate">
      <subscription assembly="Callout.dll" class="Callout.Migrate">
          <postvalue>@all</postvalue>
      </subscription>
</callout>

<callout entity="contact" event="PostCreate">
      <subscription assembly="Callout.Migration.dll" class="Callout.Migrate">
         <postvalue>@all</postvalue>
      </subscription>
</callout>  

There are a lot more things you can do to improve this process.   This is just a simple example.  Some other things to consider adding into your solution is Message Queueing and Exception Handling.

Hope this helps.

A big shout out to Henry who is out in Ohio this week :).

 

This customization may not be supported by Microsoft and is provided as-is with no warranty.

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://dtu11.spaces.live.com/blog/cns!D01B965C27CB1CC6!136.trak
引用此项的网络日志