3

I've a SharePoint Hosted App and its deployed to Root site. Using that App, I need to read items of specific list from all sub sites of that root site. My current Code:

var hostsuburl = "Subsite URL goes here";
    var context = new SP.ClientContext(hostsuburl);
    var oList = context.get_web().get_lists().getByTitle("ResourceDetails");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='monthnumber'/><Value Type='Text'>" + CurrentMonth + "</Value></Eq></Where></Query></View>");
    this.collListItem = oList.getItems(camlQuery);
    context.load(collListItem, 'Include(ActualHours,AvailableHours,CostPerHour)');
    context.executeQueryAsync(function onQuerySucceeded(sender, args) {
    alert('Inside OnQuery succeeded');
    }
 , function onQueryFailed(sender, args) {
      alert('Error onQuery Failed');
  });

But I can't get into either onQuerySucceeded function & onQueryFailed function. Any help would be appreciated.

EDIT: I can get into subsite, but I can't read list items, My code:


context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostsuburl);
this.web = appContextSite.get_web();
var olist = this.web.get_lists().getByTitle('ResourceDetails');
var camlQuery = new SP.CamlQuery(); 
camlQuery.set_viewXml("some query goes here");
this.oListCollitems = oList.getItems(camlQuery);
context.load(oListCollitems, 'Include(ActualHours,AvailableHours,CostPerHour)');
context.ExecuteQueryAsync(Function.createDelegate(this, successHandler1), Function.createDelegate(this, errorHandler1));
 function successHandler1() {
        alert('Inside Successhandler1');
     }
function errorHandler1() {
     alert('Inside Error Handler1');
     }


I'm still not get into either successhandler1() or errorhandler1() How to load list items and Enumerate to get all list items? Pls correct me, if i'm using wrong method.

  • Any console error? – Atish Dipongkor - MVP Jan 21 '16 at 11:36
  • @AtishDipongkor I'm getting following error Error: Sys.InvalidOperationException: The object is used in the context different from the one associated with the object. Error.create() msajaxbundle.js:1 Error.invalidOperation() msajaxbundle.js:1....... – Arun Jan 21 '16 at 11:43
  • Can you run your code inside SP.SOD.executeFunc("sp.js", 'SP.ClientContext', function () { // here goes your code }); – Atish Dipongkor - MVP Jan 21 '16 at 11:46
  • @AtishDipongkor I tried, Now I'm in OnQueryFailed function. – Arun Jan 21 '16 at 11:51
  • You don't need to load the web unless you're getting web properties. If you just need list items, then remove the context.load(this.web);, and the context.executeQueryAsync() after it. Go straight to getting the list items. – wjervis Jan 22 '16 at 15:36
0

According to the comment of OP, It seems SP.SOD.executeFunc is missing. So it should be

SP.SOD.executeFunc("sp.js", 'SP.ClientContext', function() {
    var hostsuburl = "Subsite URL goes here";
    var context = new SP.ClientContext(hostsuburl);
    var oList = context.get_web().get_lists().getByTitle("ResourceDetails");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='monthnumber'/><Value Type='Text'>" + CurrentMonth + "</Value></Eq></Where></Query></View>");
    this.collListItem = oList.getItems(camlQuery);
    context.load(collListItem, 'Include(ActualHours,AvailableHours,CostPerHour)');
    context.executeQueryAsync(function onQuerySucceeded(sender, args) {
        alert('Inside OnQuery succeeded');
    }, function onQueryFailed(sender, args) {
        alert('Error onQuery Failed');
    });
});
  • I've tried it. Now I can see alert from OnQueryFailed function. – Arun Jan 21 '16 at 11:55
  • That mean you have problem in your code. Let me know the error like alert(args.get_message()); in OnQueryFailed. Your problem is solved that you were unable to get into either onQuerySucceeded function & onQueryFailed – Atish Dipongkor - MVP Jan 21 '16 at 11:56
  • I'm getting Error:Unexpected response data from server error – Arun Jan 21 '16 at 11:59
  • That means you dont have permission in site you are accessing – Atish Dipongkor - MVP Jan 21 '16 at 12:01
  • Thanks for your effort, But I'm still not clear that what kind of error is it. I can use the same code to get the lists from root site itself. But I can't get into any subsite contents. Also I'm administrator & In app level, I've provided permission on AppManifest too. – Arun Jan 21 '16 at 12:08
0

If you're attempting to access a list that is not on the app web, you must use the cross domain library. Here is an example of getting a Web object using JSOM:

var crossDomainRequest = function() {
    var context = new SP.ClientContext(appweburl);
    var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);
    var appContextSite = new SP.AppContextSite(context, siteUrl);
    var web = appContextSite.get_web();
    context.load(web);
    context.executeQueryAsync(success, error);
};
  • appweburl is the url for the app that is installed.
  • hostUrl is the url for the web that the app is installed on.
  • siteUrl is the url of the web you want to access.

Example, you install your app at https://mytenant.sharepoint.com/sites/somesite. That url will be the hostUrl. Your appweburl will be https://mytenant-61a62e04fdcd25.sharepoint.com/sites/somesite/myapp/. The hex string is inserted by SharePoint and is random. If you want to access a list on https://bhets.sharepoint.com/sites/somesite/subsite, then set siteUrl to its url.

You will need to reference SP.RequestExecutor.js in order to execute the function:

var scriptbase = hostUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function() {
    $.getScript(scriptbase + "SP.js", function () {
        $.getScript(scriptbase + "SP.RequestExecutor.js", crossDomainRequest);
    });
});
  • As I'm new to this, I've confusion that what is AppWeb & HostWeb. Could you pls explain? – Arun Jan 21 '16 at 13:26
  • I've Site Collection and several subsites (/RootSite, /RootSite/SubSite1, /RootSite/SubSite2). I'm creating this app to calculate some item values from subsites list and store it into list on root site. So I've to deploy this app part on Root Site only. – Arun Jan 21 '16 at 13:32
  • @Arun see my update – wjervis Jan 21 '16 at 13:42
  • Thanks for clarification. Consider HostURL is https://testsite.sharepoint.com/sites/root and apparently appwebURL like https://testsite-61a62e04fdcd25.sharepoint.com/sites/root I want to read list from https://testsite.sharepoint.com/sites/root /subsite1 – Arun Jan 21 '16 at 14:59
  • Then use the url, https://testsite.sharepoint.com/sites/root/subsite1/, in place of siteUrl. – wjervis Jan 21 '16 at 15:01

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.