Save App Web Data to Host Web List SharePoint 2013 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.

    TestApp

  5. Add a drop down and a save button to the default.aspx page as below:
    1.  <select id="drpdwnType">  
    2.           <option value="volvo">volvo</option>  
    3.           <option value="saab">saab</option>  
    4.           <option value="mercedes">mercedes</option>  
    5.           <option value="audi">audi</option>  
    6. </select>  
    7. <asp:Button runat="server" ID="btnSave" OnClientClick="btnSaveClick()" Text="Save" />  
  6. Add the below code to App.js file.
    1. 'use strict';  
    2. var appWebContext;  
    3. var listResult;  
    4. var hostweburl;  
    5.    
    6. function btnSaveClick() {  
    7.     appWebContext = new SP.ClientContext.get_current();  
    8.     hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));  
    9.     var hostwebContext = new SP.AppContextSite(appWebContext, hostweburl);  
    10.    
    11.     var list = hostwebContext.get_web().get_lists().getByTitle("TestList");  
    12.     var createItemInfo = new SP.ListItemCreationInformation();  
    13.    
    14.     var item = list.addItem(createItemInfo);  
    15.     var e= document.getElementById("drpdwnType");  
    16.     item.set_item('Title', e.options[e.selectedIndex].text);  
    17.      
    18.     item.update();  
    19.    
    20.     appWebContext.load(item);  
    21.     appWebContext.executeQueryAsync(onQuerySucceeded,onQueryFailed);  
    22.         
    23. }  
    24.    
    25. function onQuerySucceeded() {  
    26.     alert('item creataed');  
    27. }  
    28.    
    29. function onQueryFailed(sender, args) {  
    30.     alert('Request failed. ' + args.get_message() +  
    31.         '\n' + args.get_stackTrace());  
    32. }  
    33.    
    34. // jQuery plugin for fetching querystring parameters..  
    35. jQuery.extend({  
    36.     getUrlVars: function () {  
    37.         var vars = [], hash;  
    38.         var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
    39.         for (var i = 0; i < hashes.length; i++) {  
    40.             hash = hashes[i].split('=');  
    41.             vars.push(hash[0]);  
    42.             vars[hash[0]] = hash[1];  
    43.         }  
    44.         return vars;  
    45.     },  
    46.     getUrlVar: function (name) {  
    47.         return jQuery.getUrlVars()[name];  
    48.     }  
    49. });  
  7. Go to AppManifest.xml à Permissions tab. We need to give permission to the app to access the host web. Select “SiteCollection” Permission Level ”Manage”.

  8. Deploy the solution, Trust the app.

  9. Once the page opens, select some value and click on “Save” button, test the host web list, value will be getting saved in the list.

    Item List