What I'm trying to do is build a dual purpose JavaScript that might run inside or outside of SharePoint, so I need something like this:

if(SharePoint CSOM Exists)
//Do a bunch of stuff involving the SharePoint CSOM as well
//We can run outside of SharePoint as well

Is there a standard for doing this? My hunch says it's just if(SP){} but wanted to double check.


I think the style of script you're writing is probably fairly unique in the wild (how do you handle authentication if you're in the non-SP case and you use something like a REST request, for instance?) so you may not find a genuine 'standard' for doing this -- but checking for the SP object being initialized would be sufficient in most cases.

If you wanted to be a bit more specific, I would actually check for SP and SP.ClientContext since practically everything in the Javascript Client Object Model relies on the ClientContext.

  • The JavaScript in question does not really need authentication. Most of the SharePoint stuff is adapting styling and using SharePoint modals.
    – tekiegreg
    Sep 3 '14 at 20:02

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.