SharePoint world of ECM and Information Management

SharePoint Tip #16. Do you know “how to get the SharePoint version programmatically”?

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.

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: }

 

Current "SharePoint Tips and Tricks" series has been moved to its own "SharePoint SandBox" site, to leave the place for others SharePoint posts on this blog

Comments

fzchen said:

 I really want to know "how to get the SharePoint Farm server's version programmatically",I try to get it user sql script but I got nothing.

# April 13, 2009 3:17 AM

Michael said:

What code did you use?! SQL or API?!

# April 13, 2009 3:29 AM

Jay said:

fzchen: It sounds like you didn't select the correct database. Try the following:

Replace with your ID (do exec sp_databases to figure out the name)

use "SharePoint_AdminContent_c2150d26-7bd4-4a06-9437-fde2496ccb02";

select Version,UserName from Versions where VersionID='00000000-0000-0000-0000-000000000000' order by id desc;

# April 15, 2009 3:04 PM

Dzeee said:

Interesting post on how to find the versions of SharePoint installed in the farm.

home.dzeee.net/.../Post.aspx

# April 21, 2009 8:29 PM

Jen said:

or something like this (when I'm checking if SP2 is installed), which is a bit smaller:

Version version = SPFarm.Local.BuildVersion;

bool sp2 = (version.Major >= 12 && version.MinorRevision >= 6421);

# August 5, 2009 4:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)