tnw
tnw

Reputation: 13887

Get SP Configuration Database Version in C#

I'm looking to grab the SharePoint Configuration Database version via C# and found the SPConfigDatabase class under Microsoft.SharePoint.Administration which has a Version member. This is exactly what I'm looking for, but this is now obsolete.

What is the best method for doing this? Ideally I'd like to grab the Cumulative Update date as well, though this is essentially just another name for the version number.

Thanks

Upvotes: 3

Views: 1041

Answers (3)

Goyuix
Goyuix

Reputation: 24370

The way to get it using C# is to use the Microsoft.SharePoint.Administration.SPFarm class and the property you are looking for is BuildVersion:

var v = SPFarm.Local.BuildVersion;

As that returns a System.Version object, you may want to concatenate the values together to make the string appear as it does in the central admin:

var friendly = v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision;

Also, please note that to access the SPFarm.Local property, you will need to be running the code in 64-bit mode as well as have an authorized account. In fact, with the UAC enabled on my VM I needed to run my code by right-clicking on it and choosing "Run as Administrator" as well.

Upvotes: 3

tnw
tnw

Reputation: 13887

I found a tentative solution by executing a powershell script. Here's my solution that prints out the result to the console from the powershell script:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
.
.
.
MAIN CODE:
Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();


PowerShell ps = PowerShell.Create();
ps.AddScript("Add-PsSnapin Microsoft.SharePoint.Powershell");
ps.AddScript("get-spfarm | select BuildVersion");
ps.AddCommand("Out-String");

Collection <PSObject> results = ps.Invoke();
runspace.Close();

foreach (PSObject obj in results)
{
    Console.WriteLine(obj.ToString());
}

Credit to http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C and http://social.msdn.microsoft.com/Forums/hu/sharepoint2010general/thread/88b11fe3-c218-49a3-ac4b-d1a04939980c

Upvotes: 1

Mahmoud Fayez
Mahmoud Fayez

Reputation: 3459

just connect to the database and execute this query

SELECT SERVERPROPERTY ('productversion'), 
       SERVERPROPERTY ('productlevel'), 
       SERVERPROPERTY ('edition')

references: http://support.microsoft.com/kb/321185

I hope that helps

Upvotes: 0

Related Questions