Creating a Document Library in SharePoint hosted APP using SharePoint 2013 REST API

I used the below code to create a document library using SharePoint 2013 Hosted App

Before you do this just copy the below code and paste it into APP.js file

<script type="text/javascript">
             'use strict'
             var hostweburl;  
             var appweburl;  
  
             // 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 () {           
                 //Get the URI decoded URLs.  
                 hostweburl =  
                     decodeURIComponent(  
                         getQueryStringParameter("SPHostUrl"));  
                 appweburl =  
                     decodeURIComponent(  
                         getQueryStringParameter("SPAppWebUrl"));  
                 // Resources are in URLs in the form: 
                 // web_url/_layouts/15/resource 
                 var scriptbase = hostweburl + "/_layouts/15/";   
  
                 // Load the js file and continue to load the page with information about the list top level folders. 
                 // SP.RequestExecutor.js to make cross-domain requests 
         
                 // Load the js files and continue to the successHandler 
                 $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); 
             });               
             // Function to prepare and issue the request to get 
             //  SharePoint data 
             function execCrossDomainRequest() { 
                 // executor: The RequestExecutor object 
                 // Initialize the RequestExecutor with the app web URL. 
                 var executor = new SP.RequestExecutor(appweburl);            
                 var listName="New Document Library"
 
                 // Issue the call against the app web. 
                 // To get the title using REST we can hit the endpoint: 
 
                 //      appweburl/_api/web/lists/getbytitle('listname')/items 
                 // The response formats the data in the JSON format. 
 
                 // The functions successHandler and errorHandler attend the 
                 //      sucess and error events respectively. 
                 executor.executeAsync( 
                     { 
                         url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists?@target='" + hostweburl + "'"
                         method: "POST"
                         body: "{ '__metadata': { 'type': 'SP.List' }, 'BaseTemplate': 101,'Description': '" + listName + "', 'Title':'" + listName + "'}"
                         headers: {"content-type": "application/json; odata=verbose"}, 
                         success: contextSuccessHandler, 
                         error: contextErrorHandler 
                     } 
                 ); 
             
             }                       
             // This function prepares, loads, and then executes a SharePoint query to get  
             // the current users information       
             //Utilities     
             // 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("&");    
                 for (var i = 0; i < params.length; i = i + 1) {  
                     var singleParam = params[i].split("=");  
                     if (singleParam[0] == paramToRetrieve)  
                         return singleParam[1];  
                 }  
             }     
             //Retrieve the form digest value.      
             //Store the value of the form digest.  
             function contextSuccessHandler(data) {  
                 alert("New List Has Been Created Successfully"); 

      </script>

Comments

  1. Code didnt work and getting 403 FORBIDDEN.

    ReplyDelete
    Replies
    1. Hi,

      Just now i have checked the code. Its working for me. Pl add the below method in the app.js and check again. also i hope u have set the correct permission for ur app.

      function contextErrorHandler (data) {
      alert("New List is not Created Successfully");
      }

      Permission

      List - Full control
      Web - Full control
      Tenant- Full control
      Site Collection - Full control

      Let me know if its ok

      Delete
    2. if you have still problem then let me know i will send u the sample working solution

      Delete

Post a Comment

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell