Get Host Web List data in SharePoint-Hosted App

Please follow the below steps:

Steps:

  1. Create a list in the main site (Host web) as below:

    test list

  2. Open Visual Studio à Create a new SharePoint App Project.

    App for SharePoint

  3. Select the Host for the app as “SharePoint-Hosted”.

  4. Once the project is created, you will find the below structure.

    test app

  5. Add the below code to the App.js file.
    1. var appWebContext;  
    2. var listResult;  
    3. var hostweburl;  
    4.    
    5. $(document).ready(function () {  
    6.     getListData();  
    7. });  
    8.    
    9. function getListData() {  
    10.     appWebContext = new SP.ClientContext.get_current();  
    11.     hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));  
    12.     var hostwebContext = new SP.AppContextSite(appWebContext, hostweburl);  
    13.      
    14.     var list = hostwebContext.get_web().get_lists().getByTitle('TestList');  
    15.     alert(list);  
    16.     var query = new SP.CamlQuery();  
    17.     query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Test</Value></Contains></Where></Query></View>");  
    18.     listResult = list.getItems(query);  
    19.     appWebContext.load(listResult);  
    20.     appWebContext.executeQueryAsync(onSucceded, onFailed);  
    21. }  
    22.    
    23. function onSucceded(sender, args) {  
    24.     var enumerator = listResult.getEnumerator();  
    25.     while (enumerator.moveNext()) {  
    26.         $('#message').append(" " + enumerator.get_current().get_item("Name"));  
    27.     }  
    28. }  
    29.    
    30. function onFailed(sender, args) {  
    31.     alert('Failed to get host web: ' + args.get_errorDetails());  
    32. }  
    33. // jQuery plugin for fetching querystring parameters  
    34. jQuery.extend({  
    35.     getUrlVars: function () {  
    36.         var vars = [], hash;  
    37.         var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
    38.         for (var i = 0; i < hashes.length; i++) {  
    39.             hash = hashes[i].split('=');  
    40.             vars.push(hash[0]);  
    41.             vars[hash[0]] = hash[1];  
    42.         }  
    43.         return vars;  
    44.     },  
    45.     getUrlVar: function (name) {  
    46.         return jQuery.getUrlVars()[name];  
    47.     }  
    48. });  
  1. Go to AppManifest.xml à Permissions tab. We need to give permission to the app to access the host web. Select “SiteCollection” Permission Level ”Manage” (I have given Manage, since I am planning to edit the data as well, which I will do later). Read permission can also be given.

  2. Deploy the solution, Trust  the app.

  3. You will the data from the “Name” column of the host web list “TestList” on the App page. [The formatting of the data shown in App default page is not done presently].
Ebook Download
View all
Learn
View all