SharePoint now has two major releases: SharePoint 2007 (with version 12.0) and SharePoint 2010 (with version 14.0). For developers, it's important to know which version they're working on. Sometimes developers need to working on both and write different code for different version.
Here are several ways to get SharePoint versions.
1. Get the version by SharePoint Object Model
This is easy for all developers, using Microsoft.SharePoint.Administration namespace and gets the version information by following code.
string version = SPFarm.Local.BuildVersion;
Console.WriteLine(version);
For SharePoint 2007, the output will be string begins with "12.0"; for SharePoint 2010, the output will be string starts with "14.0".
2. Finding the version by HttpWebResponse object
We can send a web request to SharePoint and then obtain a host header. There is one host header MicrosoftSharePointTeamServices that will provide us with the SharePoint version. Following function will send request and return version.
string GetSharePointVersion(string url, ICredentials credentials)
{
string version = string.Empty;
try
{
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AllowAutoRedirect = false;
request.Credentials = credentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
version = response.Headers["MicrosoftSharePointTeamServices"].ToString();
}
catch (Exception)
{
}
return version;
}
For SharePoint 2007, the return value will start with "12.0"; for SharePoint 2010, the return value will start with "14.0".
3. Get the version by SharePoint Client Object Model
This is only applied to SharePoint 2010 as SharePoint 2007 does not support this way.using Microsoft.SharePoint.Client name space and acquire version by following code.
string url = @"http://yourweburl";
ClientContext context = new ClientContext(url);
context.ExecuteQuery();
Console.WriteLine(context.ServerVersion.Major.ToString());
The output will be string "14".
4. Finding the Version by SharePoint 2010 Management Shell
SharePoint Management Shell provides another way to access SharePoint conveniently. We are able to use it to acquire SharePoint version from server. This is again used on SharePoint 2010.
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$version = $farm.BuildVersion.ToString()
The output will begin with "14.0" for SharePoint 2010.
5. Finding SharePoint version by reading registry
We can get the SharePoint versions from Windows registry. If SharePoint is installed, following entry will be included in the registry (HKEY_LOCAL_MACHINE).
For SharePoint 2007: "SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0"
For SharePoint 2010: "SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0"
And one key under entry is "Version" which stores the SharePoint version. Therefore we can read it in registry by code:
string GetSharePointVerions()
{
string SharePoint2007InstallEntry = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0";
string SharePoint2010InstallEntry = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0";
string version = string.Empty;
RegistryKey key2007 = Registry.LocalMachine.OpenSubKey(SharePoint2007InstallEntry);
RegistryKey key2010 = Registry.LocalMachine.OpenSubKey(SharePoint2010InstallEntry)
if(key2007 != null)
{
object oValue = key2007.GetValue("Version");
if (null != oValue)
{
version = oValue.ToString();
}
}
else if(key2010 != null)
{
object oValue = key2010.GetValue("Version");
if (null != oValue)
{
version = oValue.ToString();
}
}
else{}
return version;
}
For SharePoint 2007, the return value will be "12.0"; for SharePoint 2010, the return value will be "14.0".

Besides, we can use the same way to obtain 12 or 14 hive path by get the value of key "Location" under entry.
Above are different ways to get SharePoint versions.
No comments:
Post a Comment