15

I have some static JavaScript being rendered to a page and would like it to behave differently if the version of SharePoint it is being displayed by is 2013. Can I tell what version of SharePoint is being used?

The reason behind this request is related to the introduction of the 15 subfolder under /_layouts.

I cannot modify the output JS from the server side as the it is included in a Custom Field Type's fldtypes*.xml.

  • 1
    Could you use the native DOM element _spPageContextInfo.webUIVersion? If it's 4 then you're on 2010, assuming 2013 is 5, and nonexistant in 2007 or 3. – Eric Alexander Aug 14 '13 at 15:47
  • @PirateEric: Good call! Although presumably it would be set to 3 if the 2007 site/list had been migrated but not upgraded, and 4 in 2013 when in "14 mode" (although that may not be disagreeable). – Stu Pegg Aug 14 '13 at 16:00
  • Ya I'm not sure exactly how it would return in an old environment and a 2013 one, don't have access to either of those at the moment. – Eric Alexander Aug 14 '13 at 17:16
  • 2
    _spPageContextInfo.webUIVersion returns "15" on a freshly-installed 2013 environment. @Eric, you should make your comment an answer. – Derek Gusoff Aug 14 '13 at 18:50
11

There is a native DOM element called _spPageContextInfo.webUIVersion that you could use. In SP 2010, it will return 4. Derek confirmed this is in 2013 and returns 15. 2007 might not return anything or throw undefined. This should allow you to determine where you need to refer to in your script.

  • 8
    You should consider that _spPageContextInfo.webUIVersion will only return the UI version currently being used. It will return 4 even if it is a SP 2013 site running in the SP 2010 UI. – Vardhaman Deshpande Aug 15 '13 at 19:37
19

You can use the Client Object Model for this:

var clientContext;
// Make sure the SharePoint script file 'sp.js' is loaded before your code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

// Create an instance of the current context.
function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
    //here you get the version
    var serverVersion = clientContext.get_serverVersion();
}

Here the reference from Microsoft

  • That's an intriguing method. It'd mean making a second call to SharePoint on each page load and some extra checks for 2007, but it's certainly an avenue I hadn't considered. – Stu Pegg Aug 14 '13 at 14:51
  • 2
    @StuartPegg maybe store the result in a cookie or local storage – user14493 Aug 15 '13 at 10:26
  • @axrwkr: That would help, but Eric's solution won me over with its simplicity. – Stu Pegg Aug 15 '13 at 12:34
  • @StuartPegg: you're right :) – mbauer Aug 15 '13 at 12:46
  • 2
    @StuartPegg I believe this is the answer which will return 100% of the time the correct server version. The other two answers will give you the current UI version. – Vardhaman Deshpande Aug 15 '13 at 19:51
5

Two great answers above... I recently (like a few days ago) put the following together:

getSPVersion = function(returnExternal) {

    var versionMap = {
                        12: '2007',
                        14: '2010',
                        15: '2013'
                },
        version     = (typeof SP !== 'undefined')
                    ?   parseInt(SP.ClientSchemaVersions.currentVersion) 
                    :   12;

    if (returnExternal) {

        version = versionMap[version] || version;

    }

    return version

}

Figure I would share.

  • 3
    SP.ClientSchemaVersions will return you the UI version being used currently. It will return 14.0.0.0 even if it is a SP 2013 site running in the SP 2010 UI – Vardhaman Deshpande Aug 15 '13 at 19:48
  • Did not know that. Thank you for commenting. I may have to change my function to one of the suggestions above. In my case, I was trying to determine the capabilities available client side, so it may still be vid for my use case. – Paul T. Aug 15 '13 at 23:57
  • That last sentence was suppose to be: "still be valid for my use case." Thanks again – Paul T. Aug 15 '13 at 23:58
  • Also, O365 might give v16. They are with the step ahead. – user656 Sep 4 '13 at 4:42
  • I tried on O365 Sharepoint (in admin center) and got 15, but get 14 for my on-premise 2010. – bgmCoder Dec 14 '15 at 5:28
2

Yet another approach how to determine SharePoint version from JavaScript:

function getVersion(webUrl,complete)
{
    $.ajax({
       url: webUrl + "/_vti_pvt/service.cnf",
       type: 'GET',
       success: function(result) {
          var lines = result.trim().split('\n');
          if(lines.length == 2) {
              var versionProperty = lines[1].split('|');
              complete(versionProperty.length == 2 ? versionProperty[1] : null);
          }
          else {
              complete(null);   
          }        
       }
    });
}

Usage

getVersion(_spPageContextInfo.webAbsoluteUrl,
   function(version){
       console.log(version); 
   });
0

This seems to work rather well, although it can be a little slow to return the value on O365. I get the right versions on both 2010 and O365. But the credit goes here, and they have several other options that also work.

var xmlHttp = null;
if (window.XMLHttpRequest) {   // If IE7, Mozilla, Safari, and so on: Use native object.    
  xmlHttp = new XMLHttpRequest();
}else{
  if (window.ActiveXObject) {
    xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ...otherwise, use the ActiveX control for IE5.x and IE6.
  }
}

xmlHttp.open('HEAD', location.href, false);
xmlHttp.send();
var headers =  xmlHttp.getAllResponseHeaders();
var SPVersion = xmlHttp.getResponseHeader("MicrosoftSharePointTeamServices");

console.log("Full Sharepoint Version ="+SPVersion);
switch(SPVersion.substring(0,2) ){
  case "12": console.log("2007"); break;
  case "14": console.log("2010"); break;
  case "16": console.log("2013"); break;  
  default: console.log(SPVersion); break;
}

I like this way Again, the credit goes here:

//return 0 if not Sharepoint, otherwise, return major version
function getSharePointMajorVersion(){
 // The following copied from: http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx
  var xmlHttp = null;
  if (window.XMLHttpRequest) {
    // If IE7, Mozilla, Safari, and so on: Use native object.
    xmlHttp = new XMLHttpRequest();
  }else{
    if (window.ActiveXObject) {
       // ...otherwise, use the ActiveX control for IE5.x and IE6.
       xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }
  }
  xmlHttp.open('HEAD', location.href, false);
  xmlHttp.send();
  var SPVersion = xmlHttp.getResponseHeader("MicrosoftSharePointTeamServices");
  if(SPVersion == null){
    return 0;
  }else{
    return  SPVersion.substring(0,2);
  }
}

alert( getSharePointMajorVersion() );

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.