Monday, October 3, 2016

Create Many to Many Records in CRM using c#

I have written an article of how this can be done in 2011. The class that was used to do this has been deprecated, so putting down below how the new class would work.

The code is taken from the below link from Microsoft. It contains the both associate as well as disassociates functionalities.

Although this has been done for account and contact, the below code can be used for any entities which are having many to many relationship. There are 3 accounts being associated with one contact record. It can be a single account record and the code would still work.

// Associate the accounts to the contact record.

// Create a collection of the entities that will be
// associated to the contact.
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account1Id));
relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account2Id));
relatedEntities.Add(new EntityReference(Account.EntityLogicalName, _account3Id));

// Create an object that defines the relationship between the contact and account.
Relationship relationship = new Relationship("account_primary_contact");

//Associate the contact with the 3 accounts.
_service.Associate(Contact.EntityLogicalName, _contactId, relationship,

Console.WriteLine("The entities have been associated.");

//Disassociate the records.
_service.Disassociate(Contact.EntityLogicalName, _contactId, relationship,

 Also the below code works fine as well. Again this was taken form a Microsoft link.

AssociateRequest teamToProfile = new AssociateRequest
    Target = new EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId),
    RelatedEntities = new EntityReferenceCollection
        new EntityReference(Team.EntityLogicalName, _teamId)
    Relationship = new Relationship("teamprofiles_association")
// Execute the request.

Hiding a pane from the social pane

With CRM 2013, we have got a social pane in which we get posts, activities and notes for all the entities that have been marked for activities and notes. You can disregard the whole pane by removing it from the entity.

However, most of the users would love to see this pane. They would want to remove one tab sometime. This is possible using javascripts. There is a good article on how to do this below.

For case, we get 4 tabs. This includes the KB articles as well. This can be removed as well. However, for this you don’t have to write javascript. It is just a click on the properties of the pane. We need to un tick the knowledge base search control check box.

Also you can decide which tab to load when the form is loaded. This can be done by selecting the tab name from the default tab section. 

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...