Dynamically Switching UI Versions in SharePoint 2010

Table of Contents

When you have performed a clean installation of SharePoint 2010 the Visual Upgrade features are disabled by default.  Now this is fine for users starting from scratch who don't have a V3 masterpage already customised. 

To easily switch the UI back to V3 and use your existing masterpages you just need to run the following code.

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite spSite = new SPSite(url))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        //save the current allowunsafeupdates values
                        bool bSiteAllowUnsafeUpdates = spSite.AllowUnsafeUpdates;
                        bool bWebAllowUnsafeUpdates = spWeb.AllowUnsafeUpdates;

                        //allow unsafe updates on site and web
                        spSite.AllowUnsafeUpdates = true;
                        spWeb.AllowUnsafeUpdates = true;

                        //if the user interace is SharePoint 2007
                        if (spWeb.UIVersion == 3)
                        {
                            spWeb.UIVersionConfigurationEnabled = false;
                            spWeb.UIVersion = 4;
                        }
                        //if the user interace is SharePoint 2010
                        else if (spWeb.UIVersion == 4)
                        {
                            spWeb.UIVersion = 3;
                            spWeb.UIVersionConfigurationEnabled = true;
                        }

                        //update the web
                        spWeb.Update();

                        //retain the original allowunsafeupdates values
                        spWeb.AllowUnsafeUpdates = bWebAllowUnsafeUpdates;
                        spSite.AllowUnsafeUpdates = bSiteAllowUnsafeUpdates;
                    }
                }
            });

What this does is switch the rendering back to 2007 mode, and also enables the Visual Upgrade features where you can then use to switch back and forth via the Site Settings link.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 10

    Chakkaradeep Chandran says:

    Hi Chris, Unless you AllowUnSafeUpdates, your code will not work. I released th...

    Hi Chris,

    Unless you AllowUnSafeUpdates, your code will not work. I released the SPToggleUI project to codeplex last week which switches between the new SharePoint 2010 & MOSS 2007 user interface. Here is the method which does that:

    public void Toggle(string url)
            {           
                //run elevated
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite spSite = new SPSite(url))
                    {
                        using (SPWeb spWeb = spSite.OpenWeb())
                        {
                            //save the current allowunsafeupdates values
                            bool bSiteAllowUnsafeUpdates = spSite.AllowUnsafeUpdates;
                            bool bWebAllowUnsafeUpdates = spWeb.AllowUnsafeUpdates;
    
                            //allow unsafe updates on site and web
                            spSite.AllowUnsafeUpdates = true;
                            spWeb.AllowUnsafeUpdates = true;
    
                            //if the user interace is SharePoint 2007
                            if (spWeb.UIVersion == 3)
                            {
                                spWeb.UIVersionConfigurationEnabled = false;
                                spWeb.UIVersion = 4;
                            }
                            //if the user interace is SharePoint 2010
                            else if (spWeb.UIVersion == 4)
                            {
                                spWeb.UIVersion = 3;
                                spWeb.UIVersionConfigurationEnabled = true;
                            }
    
                            //update the web
                            spWeb.Update();
    
                            //retain the original allowunsafeupdates values
                            spWeb.AllowUnsafeUpdates = bWebAllowUnsafeUpdates;
                            spSite.AllowUnsafeUpdates = bSiteAllowUnsafeUpdates;
                        }
                    }
                });
            }

    It would be good if you can update your code :)

    Thanks,

    Chaks

    1. Mar 10

      Chris Walsh says:

      Chaks, AllowUnSafeUpdates isn't required if you are running a Console app on th...

      Chaks,

      AllowUnSafeUpdates isn't required if you are running a Console app on the same machine.  The code executes fine on Beta 2 & RC without setting those properties.

      Cheers

      Chris

    2. Mar 10

      Jeremy Thake says:

      Chris, as this is a wiki you can actually edit the page and modify the code cont...

      Chris, as this is a wiki you can actually edit the page and modify the code content to correct the issue you've raised. The problem with blog posts is often people don't read all of the comments for the solution, part of the reason on starting SPDevWIki was to allow the content to mature through community contributions Thanks in advance!


Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare