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