Introduction:
As we all know we can edit Sitemap by using Microsoft Dynamic Site Map Designer or SiteMap Editor using XrmToolBox. But, we can’t see the updated changes on the Site Map until we save and publish that sitemap.
Whereas we can also update SiteMap programmatically, after update we need to publish the sitemap XML programmatically
In our recent blog we discussed about how to retrieve SiteMap XML programatically.
And in this blog, we will discuss about how to update and Publish Sitemap XML programmatically.
Step 1:- Update Sitemap Programmatically
Suppose we have multiple SiteMap XML collection,
EntityCollection sitemaps = _service.RetrieveMultiple(query1); foreach (Entity entSiteMap in sitemaps.Entities) { Entity updateSiteMap = null; XElement subAreaElement; // XDocument document = XDocument.Parse(entSiteMap["sitemapxml"].ToString()); //Specify Area from default site map XElement area = document.Descendants("Area").Where(ele => appArea.Contains((string)ele.Attribute("Id"))).FirstOrDefault(); //To get specific area in site map by area id XElement area1 = document.Descendants("Area").Where(ele => (string)ele.Attribute("Id") == “Area ID”).FirstOrDefault(); //Specify the Group of default sitemap. XElement group = document.Descendants("Group").Where(ele => appGroup.Contains((string)ele.Attribute("Id"))).FirstOrDefault(); //Specify the Group of sitemap by group Id. XElement group1 = document.Descendants("Group").Where(ele => (string)ele.Attribute("Id") == “Group ID”).FirstOrDefault(); //Add new SubArea subAreaElement.Add(new XElement("Titles", new XElement("Title", new XAttribute("LCID", ENGLISH), new XAttribute("Title", “Display Name”)) )); //Add subarea into group. group.Add(subAreaElement); //Updating Sitemap updateSiteMap = new Entity(“sitemap”); updateSiteMap.Id = entSiteMap.Id; updateSiteMap["sitemapxml"] = document.ToString(SaveOptions.DisableFormatting); //Update site map service.Update(updateSiteMap); }
Step 2:- Publish Sitemap Programmatically
string siteMap = string.Empty; string parameterXML = string.Empty; //Object of PublishXmlRequest PublishXmlRequest publishXmlRequest = null; //SiteMap is not null if (sitemaps != null) { foreach (Entity appSiteMapEntity in sitemaps.Entities) { //Add multiple sitemap into appSiteMap string siteMap = siteMap + "<sitemap>" + appSiteMapEntity.Id + "</sitemap>"; } } //Add appsitemap string into <importexportxml> parameterXML = "<importexportxml><sitemaps>" + appSiteMap + "</sitemaps></importexportxml>"; //Add parameterXML string PublishXmlRequest publishXmlRequest = new PublishXmlRequest { ParameterXml = parameterXML }; //Execute publishXmlRequest to Publish sitemap Service.Execute(publishXmlRequest);
Conclusion:
We can publish updated sitemap xml using PublishXmlRequest in Dynamics 365 using the code given above.