{"id":3787,"date":"2016-10-26T16:47:40","date_gmt":"2016-10-26T11:17:40","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=3787"},"modified":"2016-10-26T16:47:40","modified_gmt":"2016-10-26T11:17:40","slug":"programmatically-publish-customizations-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/10\/programmatically-publish-customizations-in-dynamics-crm\/","title":{"rendered":"Programmatically Publish Customizations in Dynamics CRM"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>In Dynamics CRM, to make the entity customization changes visible it is important to \u201cPublish\u201d 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.<\/p>\n<p><strong>Scenario:<\/strong><\/p>\n<p>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.<\/p>\n<p>While it was easy to update the content in the XML web resource. We noticed that the changes didn\u2019t take effect until the web resource was published. Here we discuss how we can programmatically publish the customizations.<\/p>\n<p><strong>Disclaimer:<\/strong><\/p>\n<p><strong>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.<\/strong><\/p>\n<p>To publish data programmatically <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/gg334390.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">CRM SDK<\/a> has provided two messages.<\/p>\n<ol>\n<li><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.crm.sdk.messages.publishxmlrequest.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">PublishXmlRequest<\/a> &#8211; Publishes a single component.<\/li>\n<li><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.crm.sdk.messages.publishallxmlrequest.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">PublishAllXmlRequest<\/a> &#8211; Publishes all the customizations.<\/li>\n<\/ol>\n<p><strong>Example:<\/strong><\/p>\n<p>We will publish an XML Webresource in our example using the PublishXMLRequest.<\/p>\n<p>Kindly refer <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.crm.sdk.messages.publishxmlrequest.parameterxml.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">PublishXmlRequest.ParameterXml<\/a> property to refer to the xml, required to be passed to publish different solution components (like Entity, Ribbons, Dashboards, etc.\u2026).<\/p>\n<p>In our case since it is web resource we would be providing the following xml to the publish message.<\/p>\n<p>string webResctag = &#8220;&lt;webresource&gt;&#8221; + webResourceId + &#8220;&lt;\/webresource&gt;&#8221;;<\/p>\n<p>string webrescXml = &#8220;&lt;importexportxml&gt;&lt;webresources&gt;&#8221; + webResctag + &#8220;&lt;\/webresources&gt;&lt;\/importexportxml&gt;&#8221;;<\/p>\n<p><strong>Here we update the content of the web resource:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">Entity webSrc = new Entity(\"webresource\");\n\n\/\/Set the id of the webresource\nwebSrc.Id = webResourceId;\n\n\/\/Set the content of the webresource\nwebSrc.Attributes[\"content\"] = content;\n\nUpdateRequest updaterequest = new UpdateRequest{Target = webSrc};<\/pre>\n<p><strong>Following is the code to publish the updated web resource:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">string webResctag = \"&lt;webresource&gt;\" + webResourceId + \"&lt;\/webresource&gt;\";\n\nstring webrescXml = \"&lt;importexportxml&gt;&lt;webresources&gt;\" + webResctag + \u00a0\u00a0\u00a0\"&lt;\/webresources&gt;&lt;\/importexportxml&gt;\";\n\n\u00a0 PublishXmlRequest publishxmlrequest = new PublishXmlRequest\n\n\u00a0 {\n\n\u00a0\u00a0\u00a0\u00a0 ParameterXml = String.Format(webrescXml)\n\n\u00a0 };<\/pre>\n<p><strong>Following is the code to execute both the requests sequentially: \u00a0\u00a0<\/strong><\/p>\n<pre class=\"lang:default decode:true\">ExecuteMultipleRequest executemultiplerequest = new ExecuteMultipleRequest();\n\n\u00a0 executemultiplerequest.Settings = new ExecuteMultipleSettings();\n\n\u00a0 executemultiplerequest.Settings.ContinueOnError = false;\n\n\u00a0 executemultiplerequest.Settings.ReturnResponses = true;\n\n\u00a0 executemultiplerequest.Requests = new OrganizationRequestCollection();\n\n\u00a0 executemultiplerequest.Requests.Add(updaterequest);\n\n\u00a0 executemultiplerequest.Requests.Add(publishxmlrequest);<\/pre>\n<p>\/\/Below is the execute method of <strong>IOrganizationServiceFactory <\/strong>interface which will execute the <strong>ExecuteMultipleRequest<\/strong><\/p>\n<p><strong>\/\/<\/strong> organizationservice is the object of the <strong>IOrganizationServiceFactory <\/strong>interface.<\/p>\n<p>organizationservice.Execute(executemultiplerequest);<\/p>\n<p>Now if one wants to publish all the customizations at a go, then <strong>\u2018PublishAllXmlRequest\u2019<\/strong> is what is needed.<\/p>\n<p><strong>Following is the code to publish all the customisations of an organisation:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">PublishAllXmlRequest publishallxmlrequest = new PublishAllXmlRequest();<\/pre>\n<p><strong>NOTE:<\/strong><\/p>\n<ul>\n<li>One can either use the \u2018ExecuteMultipleRequest\u2019 to execute the \u2018PublishAllXmlRequest\u2019 or the \u2018PublishXmlRequest\u2019 as shown in the above scenario.<\/li>\n<\/ul>\n<ul>\n<li>Or, one can directly use the execute method of <strong>IOrganizationServiceFactory <\/strong>interface to execute the \u2018PublishAllXmlRequest\u2019 or the \u2018PublishXmlRequest\u2019 as shown below.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/<\/strong> organizationservice is the object of the <strong>IOrganizationServiceFactory <\/strong>interface.<\/p>\n<p>organizationservice.Execute(publishallxmlrequest);<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>By the use of \u2018PublishAllXmlRequest\u2019 or the \u2018PublishXmlRequest\u2019 one can avoid the added step of manually publishing the customizations after updating any data programmatically.<\/p>\n<p>Try our one click solution to export Dynamics CRM reports &#8211; <a href=\"http:\/\/www.inogic.com\/product\/productivity-pack\/click-2-export-microsoft-dynamics-crm-reports\" target=\"_blank\" rel=\"noopener noreferrer\">Click2Export<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In Dynamics CRM, to make the entity customization changes visible it is important to \u201cPublish\u201d 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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/10\/programmatically-publish-customizations-in-dynamics-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13,15,19,66],"tags":[1375,1383,1384,1385],"class_list":["post-3787","post","type-post","status-publish","format-standard","hentry","category-customizations","category-development","category-dynamics-crm","category-web-resources-2","tag-programmatically-publish-customizations","tag-publishallxmlrequest","tag-publishing-customizations","tag-publishxmlrequest"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=3787"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3787\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=3787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=3787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=3787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}