Saturday, September 23, 2017

Access SharePoint list across domain using CSOM SharePoint 2013

var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var web;

 
(function () {

$(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
    });

   function getUrl () {
        hostweburl = getQueryStringParameter("SPHostUrl");
        appweburl = getQueryStringParameter("SPAppWebUrl");
        hostweburl = decodeURIComponent(hostweburl);
        appweburl = decodeURIComponent(appweburl);

alert("hostweburl: " + hostweburl);
alert("appweburl: " + appweburl);

    var scriptbase = hostweburl + "/_layouts/15/";
                $.getScript(scriptbase + "SP.Runtime.js",
                    function () {
                        $.getScript(scriptbase + "SP.js",
                        function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
                        );
                    }
                );
            event.preventDefault();


function execOperation() {
        context = new SP.ClientContext(appweburl);
        var factory =
            new SP.ProxyWebRequestExecutorFactory(
                appweburl
            );
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);
        web = appContextSite.get_web();
        context.load(web);

        list = web.get_lists().getByTitle("TestList");
        context.load(list);
        context.executeQueryAsync(onSuccess, onFail);
        }
        function onSuccess() {
              alert("List loaded Successfully");
        }

        // This function is executed if the above call fails
        function onFail(sender, args) {
               alert( args.get_message());
        }
    }
function getQueryStringParameter(paramToRetrieve) {
                var params =
                    document.URL.split("?")[1].split("&");
                for (var i = 0; i < params.length; i = i + 1) {
                    var singleParam = params[i].split("=");
                    if (singleParam[0] == paramToRetrieve)
                        return singleParam[1];
                }
            }
})();

No comments:

Post a Comment