Tuesday, June 01, 2010

Dynamically loading SharePoint 2010 or 2007 master pages for your legacy layouts

So you have a layouts page you used in your SharePoint 2007 product and you want to use it in SharePoint 2010. The functionality did not change much, however when you open the page it looks ugly and it uses an older SharePoint 2007 master page. What can I do to fix this, so that I have one code base that covers both platforms?

One option is to dynamically change the master page on the PreInit event. Keep the aspx page as it is and do not change the master page entry. In the page class add the snippet below. In this particular example I use reflection to find out the version of SharePoint, but you can use any other method.

protected override void OnPreInit(EventArgs e)
{
    bool isSP2010 = false;
    try
    {
        Assembly assembly = Assembly.LoadWithPartialName("Microsoft.Sharepoint");
        var assemblyName = assembly.GetName();
        isSP2010 = assemblyName.Version.Major > 13;
    }
    catch (Exception)
    {
        isSP2010 = false;
    }
    if (isSP2010)
    {
        this.Page.MasterPageFile = "/_layouts/applicationv4.master";
    }          
}

Layouts work on both SharePoint 2007 and 2010!

Dovizhdane!

Credit goes also to Dan D. and Ivan P., thanks!

HTTP Error 503. The service is unavailable. in SharePoint 2010 Central Administration

In order to install a web application running only in 32-bit mode, I had to change the default IIS application pool settings to allow 32-bit applications. This worked fine for my application, however later I installed SharePoint 2010 on the same machine and when I opened the Central Administration I got the wonderful:

HTTP Error 503. The service is unavailable.

No IIS logs, no ULS logs, not much clues… The SharePoint Central Administration application pool was always shutting down for some reason.

6-1-2010 5-00-26 PM

It turns out that if Enable for 32-bit Applications is enabled SharePoint will not load and turn down the application pool. I changed this setting back to false and I was able to get to the Central Admin.