11

How could you programatically determine if code is running in an on-premise Sandbox model or SharePoint Online sandbox?

7
  • Interesting Question. I am just trying to think of a scenario where you would require such a distinction to be made. SharePoint Online is just SharePoint Server hosted on the cloud so your solution should behave the same on both. Are you facing any differences? Oct 25, 2012 at 15:33
  • 2
    There are a few differences in what you can do - e.g. you can't use pretty much anything in Assembly namespace in Online - they have some extra validation/restriction over and above sandbox on premise. For my particular use case its about restrictions on different version for licensing.
    – Ryan
    Oct 25, 2012 at 16:50
  • 1
    Could you just check if the current request URL contains "SharePoint.com"? Jun 9, 2013 at 6:59
  • 1
    Okay sorry about that.. I misread coming back to the question. I will keep thinking!
    – Hugh Wood
    Nov 18, 2013 at 12:55
  • 1
    But for JSOM in page, if you want to know if it's 365 you can check _v_dictSod["sp.js"].url to see if it's coming from the CDN. I will keep thinking SSOM.
    – Hugh Wood
    Nov 18, 2013 at 13:00

4 Answers 4

5

This is how to check for sandbox:

if(System.AppDomain.CurrentDomain.FriendlyName.Contains("Sandbox"))
{
    // I'm in a SandPit
}

This is how to check for SPO:

if(SPContext.Current.Web.siteClientTag.contains("$$16"))
{
    // I'm in the clouds
}

Mystery solved!?!

5
  • interested if there is a client side / js equivalent? Nov 18, 2013 at 11:32
  • CSOM/Apps/JSOM run in a different AppDomain to your SandBox code, I think you would have to pass Client Side constants for components to inform that they are running in SandBox. If you want to know if it's 365 you can check _v_dictSod["sp.js"].url to see if it's coming from the CDN perhaps.
    – Hugh Wood
    Nov 18, 2013 at 12:19
  • 1
    Not what I asked - this only tells you if you're running in Sandbox mode, not tell if its on-premise or on-line. Mystery not solved ;)
    – Ryan
    Nov 18, 2013 at 12:50
  • 1
    Edited the question for SPO Wave 15, its version number or at least client version number is 16 not 15 like on premises. Of course you can do the same check using JSOM _spPageContextInfo.siteClientTags
    – Hugh Wood
    Nov 18, 2013 at 13:59
  • Hey Ryan it is what you asked now ;) nudge
    – Hugh Wood
    Dec 6, 2013 at 9:59
3

For client side stuff I came up with the following check:

if (typeof O365 === "undefined"){
  // I'm executing in the context of SharePoint on-prem.
}
else{
  // I'm executing in the context of SharePoint Online.
}
1
  • typeof O365 gives "object" in 2019 on-prem.
    – Gennady G
    Feb 5, 2020 at 22:02
2

How about checking the current page script files .. a whole bunch of them come from cdn.sharepointonline.com.

e.g. https://cdn.sharepointonline.com/7085/_layouts/15/init.js?rev=T%2B51qvNtDMdb8GaqPbzXag%3D%3D https://cdn.sharepointonline.com/7085/_layouts/15/1033/initstrings.js?rev=hxd8ClaHhdv0iS7vbodfQg%3D%3D https://cdn.sharepointonline.com/7085/_layouts/15/1033/strings.js?rev=23IMQCq1f6scE85dxNy2Xg%3D%3D

I'm sure there is a way of checking this on the current page in JavaScript?

1
  • Yes you can check this in JavaScript after render, but not in C# before render. Despite Atwoods Law not everything is written in javascript yet. +1 anyway.
    – Ryan
    Nov 18, 2013 at 12:52
0

A good comparison of them is here:

http://www.helpmeonsharepoint.com/2012/06/differences-between-sharepoint.html

As for checking the environment I would look for the _layouts/QuickShare.aspx

Since it is a SharePoint 365 only feature.

[update] Something like:

XslCompiledTransform.Load("https://<site>/_layouts/QuickShare.aspx", settings, resolver); 

Should work right maybe not with outbound requests blocked? Okay it's messy but I can't think of another way that you can load a remote file on 365 at the moment.

But you could do something to throw it at the client side to update a local list?

[note] When the new version comes out, you will be able to do things like this much easier!

2
  • How would you look for _layouts/QuickShare.aspx in the sandbox model on SharePoint Online?
    – Ryan
    Nov 2, 2012 at 17:09
  • 1
    Updated for an idea, I didn't have time to test it. Failing that JavaScript and write the result to a list, I guess there are a few ways I know outbound requests are a pain though.
    – Hugh Wood
    Nov 2, 2012 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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