Friday, September 28, 2007

Workaround - "Error" in navigation when creating a publishing site from code

Since I started working with sharepoint 2007 I was puzzled by this bug, and recently I asked more people how said they have that too. The problem happens when you use the API to create a site out of a template that has the publishing feature activated in it. The site is created ok, but the first user to open the site will see a very odd tab and quick-launch:






So far, I have no explanation, but I have a workaround. Make a call to the site in the code. I know - this adds process time and resources, but it beats having your users ask you about the "error tab".
In your code, add the following function, and call it just after the site creation code:



public static void OpenUrl(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
request.UseDefaultCredentials = true;
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
}
catch(Exception ex) {
//do something with the error
}
}


Here is an example how to use this function:



public static void CreateSite(SPWeb parentWeb)
{
try
{
//get the template for the site using the GetWebTemplate function I wrote
SPWebTemplate template =
SharePointFunctions.GetWebTemplate(parentWeb,
this.SiteTemplateName,
ConfigVariables.SitesLCID);
//create the site using the class properties
newWeb = parentWeb.Webs.Add(this.ShortName,
this.SiteTitle,
this.SiteDescription,
ConfigVariables.SitesLCID,
template,
false,
false);
OpenSiteHomePage(newWeb.Url);
}
catch(Exception ex) {
//do something with the error
}
}









2 comments:

Puneet said...

Which publishing feature is causing the problem for you? Can you post the guid for the feature?

Leon Zandman said...

We also encountered this error. We think it happened to us while changing a site's URL (removing upper capital characters from it), but we aren't quite sure. We were able to fix it by simply moving the site to a different location and moving it back.