Last active
December 16, 2015 03:19
-
-
Save stevebosworth/5368664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool publishPage(int _cmsID, string _parent, string _title, string _publicUrl) | |
{ | |
string path = System.Web.HttpContext.Current.Server.MapPath("~/web.sitemap"); | |
XmlDocument doc = new XmlDocument(); | |
doc.Load(path); | |
XmlNode newSiteMapNode = createSiteMapNode(doc, _title, _publicUrl); | |
//I had an issue here finding siteMapNodes under the root siteMap because of the xml namespace .net uses. You can either remove it or try something like: /siteMap[@xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"/siteMapNode/siteMapNode[@title='" + _parent + "'] | |
//I haven't actually tried that yet but it's worth a shot now that I look at the code again. | |
XmlNode parentNode = doc.SelectSingleNode("/siteMap/siteMapNode/siteMapNode[@title='" + _parent + "']"); | |
//this if is supposed to prevent posting the same node twice but it doesn't work properly. | |
if(!parentNode.InnerXml.Contains(newSiteMapNode.ToString())) | |
{ | |
parentNode.AppendChild(newSiteMapNode); | |
doc.Save(path); | |
using (objPagesDC) | |
{ | |
var objUpdPublished = objPagesDC.pages.Single(x => x.cms_id == _cmsID); | |
objUpdPublished.published = 1; | |
objPagesDC.SubmitChanges(); | |
} | |
return true; | |
} | |
return false; | |
} | |
protected XmlNode createSiteMapNode(XmlDocument doc, string _title, string _publicUrl) | |
{ | |
// create new siteMapNode | |
XmlNode siteMapNode = doc.CreateElement("siteMapNode"); | |
XmlAttribute attrUrl = doc.CreateAttribute("url"); | |
attrUrl.Value = "~/page_sb.aspx?page=" + _publicUrl; | |
XmlAttribute attrTitle = doc.CreateAttribute("title"); | |
attrTitle.Value = _title; | |
XmlAttribute attrDesc = doc.CreateAttribute("description"); | |
attrDesc.Value = _title; | |
//append attributes to siteMapNode | |
siteMapNode.Attributes.Append(attrUrl); | |
siteMapNode.Attributes.Append(attrTitle); | |
siteMapNode.Attributes.Append(attrDesc); | |
return siteMapNode; | |
} | |
public bool unPublishPage(int _cmsID, string _parent, string _title) | |
{ | |
string path = System.Web.HttpContext.Current.Server.MapPath("../../web.sitemap"); | |
XmlDocument doc = new XmlDocument(); | |
doc.Load(path); | |
XmlNode deleteNode = doc.SelectSingleNode("/siteMap/siteMapNode/siteMapNode[@title='" + _parent + "']/siteMapNode[@title='" + _title + "']"); | |
deleteNode.RemoveAll(); | |
doc.Save(path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment