Get ClientContext in SharePoint App

Add the following function

[sourcecode language="javascript"]
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

[/sourcecode]

Then you can use this to get ClientContext

[sourcecode language="javascript"]
siteUrl = getParameterByName("SPAppWebUrl");

var clientContext = new SP.ClientContext(siteUrl);
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.