Monday, February 1, 2010

SharePoint 2007 Ate My Breadcrumbs

For a recent project my team created a process to programmatically generate new sites inside SharePoint 2007.  As it turns out this is pretty simple and can be accomplished with the method: SPWeb.Webs.Add.  You can find a good write-up of using this method here.

However, if you create a site in this manner you will notice the breadcrumbs do not show up.  This can be a major problem if you counted on breadcrumbs for site navigation.  Thankfully, this problem is easily fixed by adding the following line of code.

newSPWeb.UseShared = true;

Don’t worry if you have already created lots of sites, this setting can be applied after they have been created.  Just iterate through the sites and set this property.

Here is the full code for creating the site and ensuring that your breadcrumbs appear:

void WebsAddNoLeak(string strWebUrl)
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
using (SPWeb addedWeb = web.Webs.Add(strWebUrl))
{
addedWeb.UseShared = true;
}

} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}