In the last article we looked at how you can use SharePoint 2010 Timer Job framework to schedule parsing of our SharePoint site on defined schedule.
Now that we implemented basic framework of a timer job, we’ll create an actual sitemap in time job’s Execute method.
Here is how our execute method would look like:
StringBuilder writer = new StringBuilder(string.Empty);
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.RootWeb)
{
writer.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
RecursiveParse(web);
writer.Append("</urlset>");
MemoryStream stream =
new MemoryStream(Encoding.UTF8.GetBytes(writer.ToString()));
web.Files.Add("sitemap.xml", stream, true);
stream.Close();
}
}
As you can see the RecursiveParse is the method that will recurse through all of the children of the web and add them to the sitemap list. Let’s take a look at how that’s done:
private void RecursiveParse(SPWeb currentWeb)
{
PublishingPageCollection pageCollection = null;
if (PublishingWeb.IsPublishingWeb(currentWeb))
{
PublishingWeb publishingWeb =
PublishingWeb.GetPublishingWeb(currentWeb);
pageCollection = publishingWeb.GetPublishingPages();
}
textWriter.Append("<url>");
textWriter.Append("<loc>" + currentWeb.Url + "</loc>");
textWriter.Append("<lastmod>" +
currentWeb.LastItemModifiedDate.ToString() + "</lastmod>");
textWriter.Append("</url>");
foreach (SPWeb web in currentWeb.Webs)
{
using (web)
{
RecursiveParse(web);
}
}
currentWeb.Close();
}
Essentially, we’re going through the collection of publishing pages and adding them to the StringBuilder variable created earlier. The operation will recurse through all of the pages under web and it’s children. Once the parsing is complete the results will be saved as an XML file on the root of the site called sitemap.xml.
Good Luck!