Get ClientContext in SharePoint App

Add the following function

    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, " "));
    }

Then you can use this to get ClientContext

    siteUrl = getParameterByName("SPAppWebUrl");

    var clientContext = new SP.ClientContext(siteUrl);

Leave a Reply

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