Programmatically Publish Customizations in Dynamics CRM

By | October 26, 2016

Introduction:

In Dynamics CRM, to make the entity customization changes visible it is important to “Publish” the customization. The customization could be changes to the entity metadata or form design or adding buttons to the ribbon bar. You can also import solutions that update the customizations and publish them.

Scenario:

In one of the processes that we were designing it required updating of data to the XML web resource as part of the solution configuration process. We did not want the user to manually manipulate the XML resource as it could be error-prone and therefore a suitable UI provided for data entry which would then programmatically need to be updated to the XML web resource.

While it was easy to update the content in the XML web resource. We noticed that the changes didn’t take effect until the web resource was published. Here we discuss how we can programmatically publish the customizations.

Disclaimer:

One must take a note that, publishing customizations causes the CRM application to freeze for the moment, (i.e. till the customizations get published) and thus should be used thoughtfully and executed from within operations that are performed during non-business hours.

To publish data programmatically CRM SDK has provided two messages.

  1. PublishXmlRequest – Publishes a single component.
  2. PublishAllXmlRequest – Publishes all the customizations.

Example:

We will publish an XML Webresource in our example using the PublishXMLRequest.

Kindly refer PublishXmlRequest.ParameterXml property to refer to the xml, required to be passed to publish different solution components (like Entity, Ribbons, Dashboards, etc.…).

In our case since it is web resource we would be providing the following xml to the publish message.

string webResctag = “<webresource>” + webResourceId + “</webresource>”;

string webrescXml = “<importexportxml><webresources>” + webResctag + “</webresources></importexportxml>”;

Here we update the content of the web resource:

Entity webSrc = new Entity("webresource");

//Set the id of the webresource
webSrc.Id = webResourceId;

//Set the content of the webresource
webSrc.Attributes["content"] = content;

UpdateRequest updaterequest = new UpdateRequest{Target = webSrc};

Following is the code to publish the updated web resource:

string webResctag = "<webresource>" + webResourceId + "</webresource>";

string webrescXml = "<importexportxml><webresources>" + webResctag +    "</webresources></importexportxml>";

  PublishXmlRequest publishxmlrequest = new PublishXmlRequest

  {

     ParameterXml = String.Format(webrescXml)

  };

Following is the code to execute both the requests sequentially:   

ExecuteMultipleRequest executemultiplerequest = new ExecuteMultipleRequest();

  executemultiplerequest.Settings = new ExecuteMultipleSettings();

  executemultiplerequest.Settings.ContinueOnError = false;

  executemultiplerequest.Settings.ReturnResponses = true;

  executemultiplerequest.Requests = new OrganizationRequestCollection();

  executemultiplerequest.Requests.Add(updaterequest);

  executemultiplerequest.Requests.Add(publishxmlrequest);

//Below is the execute method of IOrganizationServiceFactory interface which will execute the ExecuteMultipleRequest

// organizationservice is the object of the IOrganizationServiceFactory interface.

organizationservice.Execute(executemultiplerequest);

Now if one wants to publish all the customizations at a go, then ‘PublishAllXmlRequest’ is what is needed.

Following is the code to publish all the customisations of an organisation:

PublishAllXmlRequest publishallxmlrequest = new PublishAllXmlRequest();

NOTE:

  • One can either use the ‘ExecuteMultipleRequest’ to execute the ‘PublishAllXmlRequest’ or the ‘PublishXmlRequest’ as shown in the above scenario.
  • Or, one can directly use the execute method of IOrganizationServiceFactory interface to execute the ‘PublishAllXmlRequest’ or the ‘PublishXmlRequest’ as shown below.

            // organizationservice is the object of the IOrganizationServiceFactory interface.

organizationservice.Execute(publishallxmlrequest);

Conclusion:

By the use of ‘PublishAllXmlRequest’ or the ‘PublishXmlRequest’ one can avoid the added step of manually publishing the customizations after updating any data programmatically.

Try our one click solution to export Dynamics CRM reports – Click2Export!