Is there any way to programmatically determine which version of SharePoint (2007 or 2010) is currently running, using the API? I have a web part that needs to emit different blocks of Javacript based on the version of SharePoint.

  • I'd love to figure out a way to do this reliably with script and/or the Web Services as well! – Marc D Anderson May 25 '10 at 11:32

You can get the Version of the SPFarm object. There is no need to elevate privileges.

Listed below is a code sample from my series on SharePoint 2007 - 2010 migration.

int spVersion = SPFarm.Local.BuildVersion.Major;
if (spVersion == 12)
    // ** SharePoint 2007
    sp2010AdditionalStyles.Disabled = true;
    // ** SharePoint 2010
    sp2010AdditionalStyles.Disabled = false;

Note that you may want to check if the SP2010 site is running in SP2007 visual mode. You can find a great article on this topic on Microsoft's SharePoint Blog: 'Making Your SharePoint 2010 Customizations Visual Upgrade Aware'.

| improve this answer | |
  • Do you know what the permissions requirements are to access that property? The web part itself only requires permissions to view list items. Will I need to elevate privileges? – jason.venema May 24 '10 at 14:23
  • I don't think so, The SDK doesn't state any CAS requirements for SPFarm... msdn.microsoft.com/en-us/library/… - Anyone else got any info? – James Love May 24 '10 at 20:27
  • To access that property you would need farm admin rights. If you configured your farm for security, elevation in the usual sense (run with elevated privileges) wouldnt help you, as the application pool account for your web application should not be able to access farm level classes in the api. Another way would be to either check for visual upgrade (as suggested above) or other new methods only available in SP2010 -consider that a hack though. You could also expose the version as a root web property or similar. – Anders Rask May 24 '10 at 21:12
  • 1
    I just had a chat with Anders, and double checked as well, but there is no need to elevate privileges or worry about the app pool account not having the appropriate rights. The code sample as presented in this answer is correct. – Jeroen Ritmeijer - Muhimbi May 25 '10 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.