Wednesday, March 10, 2010

Using Recursion in SharePoint

Using Microsoft's SharePoint as a Content Management System can present some challenges. One of the issues I once had with a client was that of accessing data that was only available on the individual content pages themselves. Unlike other types of web applications that store data in custom back end databases, when the information that you are looking for lives on a particular content page, it can be difficult to get at that information if you want to aggregate it in any way.

For this particular client, the information we were looking for was the latitude and longitude of a location that was stored on the content page. We were using Google Maps to present a filterable map of all of the companies' locations, since the locations, and in fact all of the data existed only on the content page itself -- we had to arrive at some way to get that data and store it so that is could be displayed efficiently when a user wanted to look at the map.

Our solution looked like this:

1. A timer job would run every 10 minutes and iterate through all of the levels of the site (the site had a user-defined, ragged hierarchy, we we used recursion to iterate through all of the sub-sites of a given sub-site.)
2. The timer job then took the results of the search and dropped them into an XML document that was stored in a standard SharePoint document library.
3. The map page then read from this XML document to produce a map with the locations and the appropriate meta-data for each location.



protected void findProperties(SPWeb w, XElement category)
{
if (ListExists(w, "Pages"))
{
foreach (SPListItem i in w.Lists["Pages"].Items)
{
if (i.ContentType.Name == "propertyPageType")
{
try
{

// create xml document node
// add it to xml document
}
catch (Exception ex)
{

TraceProvider.WriteTrace(TraceProvider.TagFromString("MapRefreshError"),
TraceProvider.TraceSeverity.Monitorable,
Guid.NewGuid(),
"MapRefreshJob",
"BCCG.MapRefresh",
"MapRefreshJob - Find Properties / Create Property Node",
"Bad Property Data: " + i["Title"].ToString() + ex.ToString());
badProperties.Add(new XElement("property",
new XAttribute("title", i["Title"] != null ? i["Title"].ToString() : "No Title"),
new XAttribute("url", i.Web.ServerRelativeUrl + "/" + i.Url)
));

}
}
}
}
foreach (SPWeb wChild in w.Webs)
{
findProperties(wChild, category);
}
}

}
}

0 comments:

Post a Comment