2

I have a javascript code that will run on different SharePoint platforms ( Office365, SharePoint 2013 & SharePoint 2016 )

I would like to detect the environment my code is running on, so I can add some CSS classes to control page behvaior based on the environment that I am running on.

How can I detect all those environment, note that code should still work if page is accessed anonymously ( SP.ClientContext is not an option here )

something like:

if ( isSharePoint2013() ) {
  document.documentElement.className += ' sharepoint-2013';
} else if ( isSharePoint2016() ) {
  document.documentElement.className += ' sharepoint-2016';
} else if ( isOffice365() ) {
  document.documentElement.className += ' office-365';
}

so what could be the logic behind those detection methods, in order to detect the right platform type ?

2

This code will work anonymously if you uncheck Remote Interfaces permission

 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
    console.log(clientContext.get_serverVersion());
}
function onRequestFailed(){
    console.log("Failed!");   
}
  • 1
    Having to wait for an async response to complete, will affect the overall user experience, as you can see I need to add classes as soon as possible. – Anas Nakawa Jan 4 '17 at 17:54
  • for o365, you could say if(window.location.href.indexOf("sharepoint.com") > 0){ //its o365} – Mike Jan 4 '17 at 17:59
  • thanks, but it won't work if client has registered a custom domain – Anas Nakawa Jan 4 '17 at 18:34
  • theres also a _spPageContextInfo.tenantAppVersion will be "0" if it's not hosted in the cloud. – Mike Jan 4 '17 at 19:14
  • 2
    @AnasNakawa SharePoint online will always have sharepoint.com in it's URL no matter wheather you registered your domain or not – Denis Molodtsov Jan 4 '17 at 20:24
2

JSOM requires SP.js, so you can not check for version info early on in the Browser.

This is a snippet of the REST call I use to monitor SPOnline progress

fetch('/_vti_pvt/buildversion.cnf', {
    headers: new Headers({"content-type": "text/plain"})
}).then(function (r) {
    return r.text();
}).then(function (buildtext) {
    var build = buildtext.split('vti_buildversion:SR|')[1].split('\n')[0];
    console.info('SharePoint build:',build);
}).catch(function(e){
    console.error(,e);
});

This outputs 16.0.6008.1203 on one of my SPOnline tenants today
note: This changes weekly, sometimes hourly, and I even see Microsoft retract versions

On-Prem this version will only change on updates.
BUT read this: https://blogs.technet.microsoft.com/stefan_gossner/2016/08/23/sharepoint-does-not-have-a-build-version-full-stop/

Since I only monitor SPOnlines deltas I don't care about what the Buildversion is.

The fetch API (http://caniuse.com/#feat=fetch) is standard in Chrome,
for IE you need the PolyFill: https://github.com/github/fetch

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.