SharePoint now has two major releases; they are SharePoint 2007 (with version 12.0) and SharePoint 2010 (with version 14.0). For developers, it’s important to know which version they are working on. Sometimes developers have to working on both and write different code for different version. Here are 5 ways to find SharePoint version.
Find 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 starts with “12.0”; for SharePoint 2010, the output will be string starts with “14.0”.
Find 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 get 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”.
Finding the Version by SharePoint 2010 Management Shell
SharePoint Management Shell provides another way to access SharePoint conveniently. We can use it to get SharePoint version from server. This is again applied to SharePoint 2010.
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$version = $farm.BuildVersion.ToString()
The output will start with “14.0” for SharePoint 2010.
Finding the version by HttpWebResponse object
We can send a web request to SharePoint and then get 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”.
Finding SharePoint version by reading registry
We can also get the SharePoint versions from Windows registry. If SharePoint is installed, following entry will be added 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. So 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 get 12 or 14 hive path by get the value of key “Location” under entry.
Above are different ways to get SharePoint versions, we can use them to meet different requirements.
No idea your error message, but please try this:
Reference “Microsoft.SharePoint” dll in your project, then set its property “Specific Version” to “False”.
Also please check if SharePoint 2007 server has .NET Framework 3.5
Hi,
I have a question about sharepoint features. I have sharepoint feature , which adds some commands to ribbon. Based on sharepoint version 2010 or 2013, I need to add or remove commands. How to identify this in feature XML.
Thanks