0

Currently trying to Create a basic SharePoint-hosted app for SharePoint by using Napa Office 365 Development Tools

While reading Items From list getting errorenter image description here

Created User Custom List on https://xyz.sharepoint.com/sites/AppDev Site Using napa Tool created App in office 365 i'm Observed that it Create New Domain for App, Searching list in App Domain But I have Created List On SharePoint Domain may be it happen because of Cross Domain List ,Please help Me

My Code

'use strict';

var context = SP.ClientContext.get_current();
 var user = context.get_web().get_currentUser();
var web = context.get_web();
var lists = web.get_lists();
var listItemCollection;
(function () {

// This code runs when the DOM is ready and creates a context object which      is 
 // needed to use the SharePoint object model
  $(document).ready(function () {
   getListItemCollection();
  });

 function getListItemCollection() {
 var selectedListTitle = web.get_lists().getByTitle('User');    

var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><ViewFields>" +
        "<FieldRef Name='ID' />" +
        "<FieldRef Name='Title' />" +
        "</ViewFields></View>')");
        listItemCollection = selectedListTitle.getItems(camlQuery);
        context.load(listItemCollection, "Include(Title, ID)");
        context.executeQueryAsync(onGetItemsSuccess, onGetItemsFail);

}

    function onGetItemsSuccess(sender, args) {
    // The list items were retrieved.
    // Show all child nodes.
    var listItemEnumerator = listItemCollection.getEnumerator();
    var selectItemBox = document.getElementById("selectitembox");
    if (selectItemBox.hasChildNodes()) {
        while (selectItemBox.childNodes.length >= 1) {
        selectItemBox.removeChild(selectItemBox.firstChild);
        }
    }
    while (listItemEnumerator.moveNext()) {
        var selectOption = document.createElement("option");
        selectOption.value = listItemEnumerator.get_current().get_item('ID');
        selectOption.innerHTML = listItemEnumerator.get_current().get_item('Title');
        selectItemBox.appendChild(selectOption);            
    }
}

function onGetItemsFail(sender, args) {
    // The list items couldn’t be retrieved - display an error message.
    alert('Failed to get items. Error: ' + args.get_message());
}

 })(); 
1
  • Have you tried CSOM for the same purpose ? If you are trying to enter in SharePoint online than you have must credentials for the same. and you can pass that credentials(Username , Password) as Secure String. You can find out regarding security String on google. get back to me if you have any query or question . Happy Coding :)
    – Dipen Shah
    Jul 6, 2015 at 13:16

1 Answer 1

0

After lots pf research found Artical SharePoint App accessing data from host web

Its Working For Me

SharePoint App accessing data from host web The SharePoint app can access the host web by using the standard provided SharePoint web services. When the App first launches, SharePoint provides three values in the query string named: SPHostUrl, SPAppWebUrl and SPLanguage.

By using the first two we can use a JavaScript to access the host web services. The only thing to get this working is to ask the app installer to provide the permissions.

These App permissions can be set in the AppManifest.xml, using the provided editor in Visual Studio 2012. When the permissions are set, please do only ask for permissions you really need.

sharepoint-app-permission-requests.png

After a user installed the App, the user will be asked to trust the App. If the user decide not to trust the App, the App will still be installed and users can launch the App. This results in a permissions issue which must be dealt with when the App wants to access the resources.

​When the correct permission requests are set, we can use the SPHostUrl and SPAppWebUrl to connect to the SharePoint web services. This code snipit below gets the query parameters and loads the necessary SharePoint JavaScript files.

      var web;
      var hostweburl;
      var appweburl;

       function sharePointReady() {
     hostweburl =
       decodeURIComponent(
        getQueryStringParameter('SPHostUrl')
        );
         appweburl =
     decodeURIComponent(
       getQueryStringParameter('SPAppWebUrl')
    );

   var scriptbase = hostweburl + '/_layouts/15/';

   $.getScript(scriptbase + 'SP.Runtime.js',
    function () {
        $.getScript(scriptbase + 'SP.js',
            function () { $.getScript(scriptbase + 'SP.RequestExecutor.js',      printAllListNamesFromHostWeb); }
         );
     }
    );
     }

   function getQueryStringParameter(param) {
  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] == param) {
        return singleParam[1];
    }
   }
  }

After the SharePoint JavaScript files are loaded, the appweburl is needed to create a context. With this context we can create a appContextSite to get to the host web. After this its all oldschool JSOM.

function printAllListNamesFromHostWeb() {
   var context;
var factory;
var appContextSite;
var collList;

context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);

this.web = appContextSite.get_web();
collList = this.web.get_lists();
context.load(collList);

context.executeQueryAsync(
    Function.createDelegate(this, successHandler),
    Function.createDelegate(this, errorHandler)
);

function successHandler() {
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += '<li>' + oList.get_title() + '</li>';
    }

    document.getElementById("message").innerHTML = 'Lists found:<ul>' +        listInfo + '</ul>';
  }

  function errorHandler(sender, args) {
    document.getElementById("message").innerText =
        "Could not complete cross-domain call: " + args.get_message();
   }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.