How to programmatically detect the version of SharePoint 2007 and 2010

Quite often when developing code for DocRead we need to be able to discover what versions of SharePoint are on installed in the target platform. So being the kind souls we are – we have decided to share this helper library. The class is a singleton, so only once instance can be used and is cached after the first hit – so there no perf hits. We have added LOTS of boolean’s to make it really easy to find out if a product SKU is installed.

To use the code – do something like this :

if (SPEnvironment.Instance.IsSharePointServerInstalled){
   // either SharePoint server for 2010 or 2007 is installed.
}

Here’s the complete listing :

 namespace Collaboris.SharePoint
{
    using System;
    using System.Collections.Generic;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.Win32;

    public class SPEnvironment
    {
        #region Constants and Fields

        private const string Foundation2010 = 
            "BEED1F75-C398-4447-AEF1-E66E1F0DF91E";

        private const string OfficeWebCompanions2010 = 
            "926E4E17-087B-47D1-8BD7-91A394BC6196";

        private const string ProjectServer2010 = "ED21638F-97FF-4A65-AD9B-6889B93065E2";

        private const string ProjectServerTria2010 = 
            "84902853-59F6-4B20-BC7C-DE4F419FEFAD";

        private const string SearchServer2010 = 
            "08460AA2-A176-442C-BDCA-26928704D80B";

        private const string SearchServerExpress2010 = 
            "1328E89E-7EC8-4F7E-809E-7E945796E511";

        private const string SearchServerTrial2010 = 
            "BC4C1C97-9013-4033-A0DD-9DC9E6D6C887";

        private const String SharePointProductsRegistry2010Path = 
            @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\WSS\InstalledProducts\";

        private const string SharePointServerEnterprise2010 = 
            "D5595F62-449B-4061-B0B2-0CBAD410BB51";

        private const string SharePointServerEnterpriseTrial2010 = 
            "88BED06D-8C6B-4E62-AB01-546D6005FE97";

        private const string SharePointServerStandard2010 = 
            "3FDFBCC8-B3E4-4482-91FA-122C6432805C";

        private const string SharePointServerStandardTrial2010 = 
            "B2C0B444-3914-4ACB-A0B8-7CF50A8F7AA0";

        private static readonly object SyncRoot = new Object();

        private readonly bool is2007Installed;

        private readonly bool is2010Installed;

        private static volatile SPEnvironment instance;

        private bool isFoundation2010Installed;

        private bool isMoss2007EnterpriseInstalled;

        private bool isMoss2007Installed;

        private bool isOfficeWebCompanions2010Installed;

        private bool isProjectServer2010Installed;

        private bool isProjectServerTria2010Installed;

        private bool isSearchServer2010Installed;

        private bool isSearchServerExpress2010Installed;

        private bool isSearchServerTrial2010Installed;

        private bool isSharePointServerEnterprise2010Installed;

        private bool isSharePointServerEnterpriseTrial2010Installed;

        private bool isSharePointServerStandard2010Installed;

        private bool isSharePointServerStandardTrial2010Installed;

        private bool isWss2007Installed;

        #endregion

        #region Constructors and Destructors

        private SPEnvironment()
        {
            try
            {
                int version = SPFarm.Local.BuildVersion.Major;
                if (version == 12)
                {
                    // SharePoint 2007     
                    this.is2007Installed = true;
                    this.Init2007();
                }

                if (version == 14)
                {
                    // SharePoint 2010     
                    this.is2010Installed = true;
                    this.Init2010();
                }
            }
            catch (Exception)
            {
            }
        }

        #endregion

        #region Properties

        public static SPEnvironment Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (instance == null)
                        {
                            // needs to be the app pool to get to the registry key
                            SPSecurity.RunWithElevatedPrivileges(
                                () => instance = new SPEnvironment()
                                                                );
                        }
                    }
                }

                return instance;
            }
        }

        public bool Is2007Installed
        {
            get
            {
                return this.is2007Installed;
            }
        }

        public bool Is2010Installed
        {
            get
            {
                return this.is2010Installed;
            }
        }

        public bool IsFoundation2010Installed
        {
            get
            {
                return this.isFoundation2010Installed;
            }
        }

        public bool IsMoss2007Installed
        {
            get
            {
                return this.isMoss2007Installed;
            }
        }

        public bool IsOfficeWebCompanions2010Installed
        {
            get
            {
                return this.isOfficeWebCompanions2010Installed;
            }
        }

        public bool IsProjectServer2010Installed
        {
            get
            {
                return this.isProjectServer2010Installed;
            }
        }

        public bool IsProjectServerTria2010Installed
        {
            get
            {
                return this.isProjectServerTria2010Installed;
            }
        }

        public bool IsSearchServer2010Installed
        {
            get
            {
                return this.isSearchServer2010Installed;
            }
        }

        public bool IsSearchServerExpress2010Installed
        {
            get
            {
                return this.isSearchServerExpress2010Installed;
            }
        }

        public bool IsSearchServerTrial2010Installed
        {
            get
            {
                return this.isSearchServerTrial2010Installed;
            }
        }

        public bool IsSharePointServer2010Installed
        {
            get
            {
                if (this.IsSharePointServerEnterprise2010Installed || 
                    this.isSharePointServerEnterpriseTrial2010Installed || 
                    this.isSharePointServerStandard2010Installed || 
                    this.IsSharePointServerStandardTrial2010Installed)
                {
                    return true;
                }
                return false;
            }
        }

        public bool IsSharePointServerEnterprise2010Installed
        {
            get
            {
                return this.isSharePointServerEnterprise2010Installed;
            }
        }

        public bool IsSharePointServerEnterpriseTrial2010Installed
        {
            get
            {
                return this.isSharePointServerEnterpriseTrial2010Installed;
            }
        }

        public bool IsSharePointServerInstalled
        {
            get
            {
                if (this.Is2007Installed)
                {
                    // if moss isnt installed and Wss is installed.
                    if (this.IsMoss2007Installed)
                    {
                        return true;
                    }

                    return false;
                }

                if (this.is2010Installed)
                {
                    if (this.IsSharePointServer2010Installed)
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        public bool IsSharePointServerStandard2010Installed
        {
            get
            {
                return this.isSharePointServerStandard2010Installed;
            }
        }

        public bool IsSharePointServerStandardTrial2010Installed
        {
            get
            {
                return this.isSharePointServerStandardTrial2010Installed;
            }
        }

        public bool IsWss2007Installed
        {
            get
            {
                return this.isWss2007Installed;
            }
        }

        #endregion

        #region Helper Methods

        private static bool IsMoss2007()
        {
            SPFeatureDefinitionCollection features = 
                SPContext.Current.Site.WebApplication.Farm.FeatureDefinitions;

            if (features["OssNavigation"] != null && features["Publishing"] != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Method to find out if Wss is installed.
        /// </summary>
        /// <returns></returns>
        private static bool IsWss2007()
        {
            SPFeatureDefinitionCollection features = 
                SPContext.Current.Site.WebApplication.Farm.FeatureDefinitions;

            if (features["WebPageLibrary"] != null)
            {
                return true;
            }

            return false;
        }

        private bool GetValue(RegistryKey key, 
                              IEnumerable<string> valueNames, string productId)
        {
            if (valueNames == null)
            {
                return false;
            }

            foreach (string name in valueNames)
            {
                if ((string)key.GetValue(name) == productId)
                {
                    return true;
                }
            }
            return false;
        }

        private void Init2007()
        {
            this.isMoss2007Installed = IsMoss2007();
            this.isWss2007Installed = IsWss2007();
        }

        private void Init2010()
        {
            //Open the registry key in read-only mode. using (RegistryKey key = 
                Registry.LocalMachine.OpenSubKey(SharePointProductsRegistry2010Path, false))
            {
                if (key != null)
                {
                    string[] valueNames = key.GetValueNames();

                    this.isFoundation2010Installed = 
                        this.GetValue(key, valueNames, Foundation2010);

                    this.isSearchServerExpress2010Installed = 
                        this.GetValue(key, valueNames, SearchServerExpress2010);

                    this.isSharePointServerStandardTrial2010Installed = 
                        this.GetValue(key, valueNames, SharePointServerStandardTrial2010);

                    this.isSharePointServerStandard2010Installed = 
                        this.GetValue(key, valueNames, SharePointServerStandard2010);

                    this.isSharePointServerEnterpriseTrial2010Installed = 
                        this.GetValue(key, valueNames, SharePointServerEnterpriseTrial2010);

                    this.isSharePointServerEnterprise2010Installed = 
                        this.GetValue(key, valueNames, SharePointServerEnterprise2010);

                    this.isSearchServerTrial2010Installed = 
                        this.GetValue(key, valueNames, SearchServerTrial2010);

                    this.isSearchServer2010Installed = 
                        this.GetValue(key, valueNames, SearchServer2010);

                    this.isProjectServerTria2010Installed = 
                        this.GetValue(key, valueNames, ProjectServerTria2010);

                    this.isProjectServer2010Installed = 
                        this.GetValue(key, valueNames, ProjectServer2010);

                    this.isOfficeWebCompanions2010Installed = 
                        this.GetValue(key, valueNames, OfficeWebCompanions2010);
                }
            }
        }

        #endregion
    }
}

Leave a Reply 0 comments

Leave a Reply: