Tuesday, March 13, 2012

Disassociate an Entity Record From Another Entity Record in Microsoft Dynamics CRM 2011 Using C# in Silverlight

This tutorial will show you how to disassociate an entity record from 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#:

OrganizationRequest request = new OrganizationRequest() { RequestName = "Disassociate" };

//Target is the entity that you are associating your entities to.
EntityReference entrefTarget = new EntityReference();
entrefTarget.Id = new Guid("06AA44B1-336B-E111-B3CA-1CC1DEF1B5FF");
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);


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.

-

1 comment:

  1. One thing I've come to do is create MyClass first and then create a EFMyClass that inherits from MyClass. EFMyClass is the one I associate with EF.

    After fetching my entities I convert them to MyClass using a constructor in MyClass that has a EFMyClass parameter.

    public class MyClass
    {
    public MyClass() { }
    public MyClass(EFMyClass efMyClass)
    {
    *copy members*
    }
    }

    public class EFMyClass : MyClass
    {

    }

    EFMyClass can then implement specific properties to match the database without influencing MyClass which is written to fit the application.

    ReplyDelete