0

My objective is to read and extract data from list created in SharePoint site and put it in another list of the same site.

Firstly my problem is that an error message appears where I deploy my application and add my app

enter image description here

function retrieveListItems() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('List Test');

    var camlQuery = new SP.CamlQuery();
    var q = '<View><Query><Where><Eq><FieldRef Name=\'email\' /><Value Type=\'Text\'>' + user.get_email() + '</Value></Eq></Where></Query></View>';
    camlQuery.set_viewXml(q);
    collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);

    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

}

function onQuerySucceeded(sender, args) {
    alert("Suceeded");
    ![var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        alert("ok");][1]
    }
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
3

It seems this error occurs because of the way the SP.ClientContext was created. And to get the contents of the host web, you probably need to use an SP.AppContextSite

var hostweburl, appweburl;

$(document).ready(function () {

    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var scriptbase = hostweburl + "/_layouts/15/";

    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", retrieveListItems);
        }
    );
});

// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

function retrieveListItems() { 
  var context = new SP.ClientContext(appweburl); 
  var appContextSite = new SP.AppContextSite(clientContext, hostweburl);
  var website = appContextSite.get_web();

  //the remaining code is omitted for clarity  
}
  • thanks. But I work with sharepoint online. Is it the same way or not ? because I think that SP.AppContextSite is for Access data from the host web section. – user32312 Aug 8 '14 at 11:29
  • 1
    This is a common pattern to Access SharePoint 2013/SPO data from apps. – Vadim Gremyachev Aug 8 '14 at 11:49
  • Interesting that queries are still in current context: "context.load(website);" , but not "appContextSite.load(website);" – Gennady G Feb 2 '17 at 16:43

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.