Thursday, July 26, 2012

Change the sitemap content programmatically in CRM 2011 (MSCRM)

We can change the content of the sitemap by using the CRM 2011 Webservice.
To accomplish this follow these steps

Step 1: Retrieve the sitemap



QueryExpression query = new QueryExpression();
query.EntityName = "sitemap";
query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true);

EntityCollection col = _service.RetrieveMultiple(query);

Entity sitemap = null;
if (col != null && col.Entities.Count > 0)
   sitemap  = col.Entities[0];

Step 2: Parse the sitemap entity to an XDocument object




string sitemapcontent = sitemap["sitemapxml"].ToString();
XDocument sitemapxml = XDocument.Parse(sitemapcontent);


You can now change the sitemap by modifying the sitemapxml object. After you made your changes, it's time to save these changes.

Step 3: Update the sitemap


sitemap["sitemapxml"] = sitemapxml.ToString();
_service.Update(sitemap);

Step 4: Publish the sitemap

Now you need to publish the sitemap.


PublishXmlRequest request = new PublishXmlRequest();
request.ParameterXml = "<importexportxml><sitemaps><sitemap></sitemap></sitemaps></importexportxml>";
_service.Execute(request);

Helpful links
http://msdn.microsoft.com/en-us/library/gg309259.aspx