How to create and update entities in Dynamics CRM 2013

Lately I needed to find a way to create and update records in Dynamics CRM 2013 from external Web API.

This is actually easy to do when you use the Dynamics CRM SDK.

First, you need to add a reference to Microsoft.Xrm.SDK.dll, which can be found in DynamicsSDK\SDK\Bin\  folder.

After this I have created a helper class for the purpose of this blog post where I put 2 different methods.

For production project I recommend that you create two separate classes with meaningful names for these methods.

public class OrganizationServiceHelper
    {
        public OrganizationServiceProxy GetOrganizationService()
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = new System.Net.NetworkCredential("someUserName", "somePassword", "someDomain");
            var organizationservice = new OrganizationServiceProxy(
                new Uri("https://someCRMAdress/someOrganizationName/XRMServices/2011/Organization.svc"), null, credentials, null);
            return organizationservice;
        }

        public Entity QueryByAttribute(OrganizationServiceProxy organizationServiceProxy, string entityName, string filterColumnName, string filterValue)
        {
            var querybyattribute = new QueryByAttribute(entityName)
            {
                ColumnSet = new ColumnSet(filterColumnName)
            };

            //  Attribute to query.
            querybyattribute.Attributes.AddRange(filterColumnName);

            //  Value of queried attribute to return.
            querybyattribute.Values.AddRange(filterValue);

            EntityCollection retrieved = organizationServiceProxy.RetrieveMultiple(querybyattribute);

            return retrieved[0];
        }
    }

 

GetOrganizationService is the method used for connecting to the CRM server.

In this method we need to pass Network Credential we will use to create or update entities in Dynamics CRM.

We also need to pass Uri to Dynamics CRM server organization.

After this I have created a Repository class for Create and Update entities in Dynamics CRM 2013 which we can use for any project.

 

 public class MunicipalityCRMRepository
    {
        private readonly OrganizationServiceHelper _organizationServiceHelper = new OrganizationServiceHelper();
        private readonly OrganizationServiceProxy _organizationservice;

        public MunicipalityCRMRepository()
        {
            _organizationservice = _organizationServiceHelper.GetOrganizationService();
        }
        public void Create(Municipality municipality)
        {
	    // here we do query to get Region to pass EntityReference to Lookup field
            var region = _organizationServiceHelper.QueryByAttribute(_organizationservice, "new_region", "new_id", municipality.new_region);

            var newEntity = new Entity("new_municipality");
            newEntity["new_id"] = municipality.Mun_Id;
            newEntity["new_name"] = municipality.Mun_Name;
	    // here we populate optionset
            newEntity["new_statepart"] = new OptionSetValue(Convert.ToInt32(municipality.new_statepart));
	    // here we populate lookup field
            newEntity["new_regionid"] = new EntityReference("new_region", region.Id);

            _organizationservice.Create(newEntity);
        }

        public void Update(string id, Municipality municipality)
        {
	    // we grab entity we need to change by using passed id
            var municipalityToChange = _organizationServiceHelper.QueryByAttribute(_organizationservice, "new_municipality", "new_id", id);
	    // here we do query to get Region to pass EntityReference to Lookup field
            var region = _organizationServiceHelper.QueryByAttribute(_organizationservice, "new_region", "new_id", municipality.new_region);

            municipalityToChange["new_id"] = municipality.Mun_Id;
            municipalityToChange["new_name"] = municipality.Mun_Name;
	    // here we populate optionset
            municipalityToChange["new_statepart"] = new OptionSetValue(Convert.ToInt32(municipality.new_statepart));
	    // here we populate lookup field
            municipalityToChange["new_regionid"] = new EntityReference("new_region", region.Id);

            _organizationservice.Update(municipalityToChange);

        }
    }

 

If you want to learn more about programming CRM Dynamics 2013 I recommend a great book that I love written by real expert:

DynamicsAmazon

Microsoft Dynamics CRM 2013 Unleashed

Creating or Updating Municipality Entity inside Dynamics CRM 2013 is simple as calling Create or Update method on Repository class.

 

If you like this article don’t forget to subscribe to this blog and make sure you don’t miss new upcoming blog posts.

 

Leave a Reply