Tip #16. Get the SharePoint version programmatically

Rate this item
(0 votes)

The administrative way of getting SharePoint version is navigating to “Central Administration > Operations > Servers in Farm”. But what if you need to get version programmatically?!

It might be that your feature relies on the specific version, lets say SharePoint SP1 + Infrastructure Update installed, and you need to check version of the SharePoint when feature installs. The way to get version programmatically is use the following SQL script

   1: SELECT Version, UserName  
   2: FROM Versions 
   3: WHERE VersionId = '00000000-0000-0000-0000-000000000000' 
   4: ORDER BY Id DESC

This script should be run against the Central Administration content database, and it returns the value which is show in “Service In Farm”. Refer the following post to find which Update/Patch corresponds to SharePoint version number.

Take into account that it's officially unsupported and not recommended way to get any information directly from SharePoint database, the right way to this is via API.

Use the following code-snippet to find the content database of the Central Administration site, to be used in the SQL script

   1: using (SPSite startSite = new SPSite("<Central Admin url>"))
   2: {
   3:     SPFarm farm = startSite.WebApplication.Farm;
   4:     SPWebService service = farm.Services.GetValue<SPWebService>("");
   5:  
   6:     foreach (SPWebApplication webApplication in service.WebApplications)
   7:     {
   8:         foreach (SPSite site in webApplication.Sites)
   9:         {
  10:             Console.WriteLine(string.Format("{0} - {1}", site.Url, site.ContentDatabase.Name));
  11:         }
  12:     }
  13: }

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.