Question

Is it possible to check, from within solution, if it was deployed as sandboxed or not, e.g. something like this:

if(this.IamSandboxedSolution)
    // do something
else
    // do something else
Was it helpful?

Solution

Not out-of-the-box AFAIK.

You could check it like this:

if(AppDomain.CurrentDomain.FriendlyName ==
    "Sandboxed Code Execution Partially Trusted Asp.net AppDomain") {
    // I'm playing in the sandbox!
}

OTHER TIPS

AFAIK there is no foolproof way to do this unfortunately. I know Microsoft's SharePoint Patterns & Practices team are using:

AppDomain.CurrentDomain.FriendlyName.Contains("Sandbox")

So if that's the best they've come up with, it's fair to say that's as good as it gets. Obviously some static helper method is the way to go rather than having this check littered through your code.

EDIT: IMHO this is far preferable to running some forbidden code and catching the exception, due to perf reasons.

Another equally hacky approach would be to try to do something that isn't allowed in the sandbox and catch the resulting exception. Something like this:

static bool? _isSandboxed;
static bool IsSandboxed() {
    if(!_isSandboxed.HasValue) {
        try {
            SPSecurity.RunWithElevatedPrivileges(delegate { });
            _isSandboxed = false;
        } catch (PolicyException) {
            _isSandboxed = true;
        }
    }
    return _isSandboxed.Value;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top