Monday, March 12, 2012

Associate an Entity Record with Another in Microsoft Dynamics CRM 2011 Using C# in Silverlight

This tutorial will show you how to associate an entity record to another entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

First things first.  You have to set up your Silverlight app to make a web services connection to CRM.   The best tutorial I have found for this is located here in the MSDN:
http://msdn.microsoft.com/en-us/library/gg594452.aspx

Here is the call in C#:

private void AssociateAccount(Guid guidResult)
{
    OrganizationRequest request = new OrganizationRequest() { RequestName = "Associate" };

    //Target is the entity that you are associating your entities to.
    EntityReference entrefTarget = new EntityReference();
    entrefTarget.Id = guidResult;
    entrefTarget.LogicalName = "account";
    request["Target"] = entrefTarget;
     
    //RelatedEntities are the entities you are associating to your target (can be more than 1)
    EntityReferenceCollection entrefcolCollection = new EntityReferenceCollection();
    EntityReference entrefRelatedEntity1 = new EntityReference();
    entrefRelatedEntity1.Id = new Guid("32B365C9-5F0C-E111-BF0B-1CC1DEE89AA8");
    entrefRelatedEntity1.LogicalName = "contact";
    entrefcolCollection.Add(entrefRelatedEntity1);
    request["RelatedEntities"] = entrefcolCollection;
    

    //The relationship schema name in CRM you are using to associate the entities. 
    //found in settings - customization - entity - relationships
    Relationship relRelationship = new Relationship();
    relRelationship.SchemaName = "contact_customer_accounts";
    request["Relationship"] = relRelationship;

    //execute the request
    IOrganizationService service = SilverlightUtility.GetSoapService();

    //depending on how you do things your calls will most likely be asynchronous
    service.BeginExecute(request, new AsyncCallback(Associate_Callback), service);
}

 private void Associate_Callback(IAsyncResult result)
{
    try
    {
        OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
        

        

    }
    catch (Exception ex)
    {
        ReportError(ex);
    }
}

You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

Really though once you understand the differences in working with the two concepts it really isn't a tough nut to crack.

3 comments:

  1. Superb understanding made about associate entry record. Much helpful stuff.


    Invoice Template

    ReplyDelete
  2. CrmSdk.OrganizationRequest request = new CrmSdk.OrganizationRequest() { RequestName = "Associate" };

    EntityReference targetEntityReference = new EntityReference();
    targetEntityReference.LogicalName = "pr_customer";
    targetEntityReference.Id = recordid;


    request["Target"] = targetEntityReference ; //// This statement is not working (compiler error)

    ReplyDelete
  3. I have had trouble using Silverlight 5 with CRM. Make sure you are using 4 and try it again.

    ReplyDelete