Wednesday, July 15, 2009

How To Check If MOSS Or WSS Is Installed

There are many approaches to check if MOSS or WSS is installed at specific installation site. One of the approach is to check the Windows Registry key.

If MOSS is installed, there will be registry entries in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\12.0

image

If WSS is installed, there will be registry entries in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0

image

Please note that if MOSS is installed both registry entries as shown above will be exist at installation sites.

I’ve created a helper class SharePointConfig.cs which exposes method and properties which are useful to those who are interested use this approach. See code below:

public class SharePointConfig
{
    private const string MOSS_REGISTRY_PATH = @"SOFTWARE\Microsoft\Office Server\12.0";
    private const string MOSS_BUILD_VERSION = "BuildVersion";
    private const string MOSS_TEMPLATE_PATH = "TemplatePath";

    private const string WSS_REGISTRY_PATH = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0";
    private const string WSS_BUILD_VERSION = "BuildVersion";
    private const string WSS_TEMPLATE_PATH = "TemplatePath";
    private bool _ismossinstalled;
    private bool _iswssinstalled;
    private string _buildversion;
    private string _templatepath;

    /// <summary>
    /// Initializes a new instance of the <see cref="SharePointConfig"/> class.
    /// </summary>
    public SharePointConfig()
    {
        GetSharePointConfig();
    }

    /// <summary>
    /// Gets a value indicating whether this instance is MOSS installed.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance is MOSS installed; otherwise, <c>false</c>.
    /// </value>
    public bool IsMOSSInstalled
    {
        get { return _ismossinstalled; }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is WSS installed.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance is WSS installed; otherwise, <c>false</c>.
    /// </value>
    public bool IsWSSInstalled
    {
        get { return _iswssinstalled; }
    }

    /// <summary>
    /// Gets the build version.
    /// </summary>
    /// <value>The build version.</value>
    public string BuildVersion
    {
        get { return _buildversion; }
    }

    /// <summary>
    /// Gets the template path.
    /// </summary>
    /// <value>The template path.</value>
    public string TemplatePath
    {
        get { return _templatepath; }
    }

    /// <summary>
    /// Loads the share point config.
    /// </summary>
    public void GetSharePointConfig()
    {
        try
        {
            // Assume current user has full rights to registry settings
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // Get MOSS registry key
                RegistryKey theRegistry = Registry.LocalMachine.OpenSubKey(MOSS_REGISTRY_PATH);

                if (theRegistry != null)
                {
                    // Get registry values
                    _buildversion = theRegistry.GetValue(MOSS_BUILD_VERSION).ToString();
                    _templatepath = theRegistry.GetValue(MOSS_TEMPLATE_PATH).ToString();
                    _ismossinstalled = MossOrWSSIsFound();
                }
                else
                {
                    // Get WSS registry key
                    theRegistry = Registry.LocalMachine.OpenSubKey(WSS_REGISTRY_PATH);

                    if (theRegistry != null)
                    {
                        // Get registry values
                        _buildversion = theRegistry.GetValue(WSS_BUILD_VERSION).ToString();
                        _templatepath = theRegistry.GetValue(WSS_TEMPLATE_PATH).ToString();
                        _iswssinstalled = MossOrWSSIsFound();
                    }
                }

            });
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Mosses the or WSS is found.
    /// </summary>
    /// <returns></returns>
    private bool MossOrWSSIsFound()
    {
        bool retVal = false;
        try
        {
            if (!string.IsNullOrEmpty(_buildversion) && !string.IsNullOrEmpty(_templatepath))
            {
                Version buildVersion = new Version(_buildversion);
                if (buildVersion.Major == 12)
                    retVal = true;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return retVal;
    }
}

The following tables list the members exposed by the SharePointMalaya.SSOHelper.SSOConfiguration type.

Public method SharePointConfig - Initializes a new instance of the SharePointConfig class, get registry key for either MOSS or WSS.

Public method IsMOSSInstalled - Gets a value indicating whether MOSS instance is installed.

Public method IsWSSInstalled - Gets a value indicating whether WSS instance is installed.

Public method BuildVersion - Gets either MOSS or WSS build version.

Public method TemplatePath - Gets either MOSS or WSS template path.

Get source code here:

Sample Web Part

image

1 comment:

NLV said...

Good sample. Thanks.