How To Get All Site Columns Using REST API In SharePoint Online And Office 365


Welcome to an article on “How to get all Site Columns using REST API in SharePoint Online and Office 365” where we will see the steps of creating an app using Napa Tool which will help us to view all the site columns on our site using REST API.

  • Open the “NAPA” Office 365 Development Tools through your SharePoint store.
  • Click on Add New Project.
  • It will ask you, what type of app do you want to build?build
  • Select SharePoint Add-in and provide a name to your app and click on Create.type
  • You will see the screen below on the app,content
  • Click on Default.aspx and paste the code below under the,
    1. “<asp:ContentContentPlaceHolderID=“PlaceHolderMain” runat=“server”>”.

    Code:

    1.         All Site Columns
    2.         “height:300px; width:250px” multiple=“multiple” id=“getallsitecolumns”>
  • Now on the navigation click on the App.js file and paste the code below removing the previous code completely.Code:

    1. ‘use strict’;
    2. var hostweblink;
    3. var applink;
    4. // Load the links on app load
    5. $(document).ready(function()
    6. {
    7.     hostweblink = decodeURIComponent(
    8.         getQueryStringParameter(“SPHostUrl”));
    9.     applink = decodeURIComponent(
    10.         getQueryStringParameter(“SPAppWebUrl”));
    11.     varscriptlink = hostweblink + “/_layouts/15/”;
    12.     $.getScript(scriptlink + “SP.RequestExecutor.js”, loadPage);
    13. });
    14. //function to retrieve values 
    15. function getQueryStringParameter(paramval)
    16. {
    17.         var paramvalue = document.URL.split(“?”)[1].split(“&”);
    18.         for (var i = 0; i < paramvalue.length; i = i + 1)
    19.         {
    20.             var Paramval1 = paramvalue[i].split(“=”);
    21.             if (Paramval1[0] == paramval) return Paramval1[1];
    22.         }
    23.     }
    24.     // load function of the app 
    25. function loadPage()
    26. {
    27.     getallSiteColumns();
    28. }
    29. //Retrieve all of the site columns 
    30. function getallSiteColumns()
    31. {
    32.     var play;
    33.     play = new SP.RequestExecutor(applink);
    34.     play.executeAsync
    35.     ({
    36.         url: applink + “/_api/SP.AppContextSite(@target)/web/Fields?@target='” + hostweblink + “‘”,
    37.         method: “GET”,
    38.         headers:
    39.         {
    40.             “Accept”“application/json; odata=verbose”
    41.         },
    42.         success: getcolumnssuccessful,
    43.         error: getcolumnsfail
    44.     });
    45. }
    46. //shows all Columns.
    47. function getcolumnssuccessful(data)
    48. {
    49.     var jsonObjectval = JSON.parse(data.body);
    50.     var selectallColumns = document.getElementById(“getallsitecolumns”);
    51.     if (selectallColumns.hasChildNodes())
    52.     {
    53.         while (selectallColumns.childNodes.length >= 1)
    54.         {
    55.             selectallColumns.removeChild(selectallColumns.firstChild);
    56.         }
    57.     }
    58.     var result = jsonObjectval.d.results;
    59.     for (var i = 0; i < result.length; i++)
    60.     {
    61.         varselectOption = document.createElement(“option”);
    62.         selectOption.value = result[i].Title;
    63.         selectOption.innerText = result[i].Title;
    64.         selectallColumns.appendChild(selectOption);
    65.     }
    66. }
    67. functiongetcolumnsfail(data, errorCode, errorMessage)
    68. {
    69.     alert(“Could not retrieve site columns, view he error “ + errorMessage);
    70. }
  • Click on the settings icon on the tool on the left.settings
  • Under the properties, select Permissions and provide full control to the app on the Site Collection level.Permissions
  • Click on the deploy button on the left and run the project.deploy
  • Click on the launch button.launch
  • Accept the trust and click on ‘Trust It’.trust
  • Your app will be deployed and open for you as per the following screenshot:open
  • Your entire site columns will load on your app on the page load of the app.

Here we saw today how to get all site columns using REST API in SharePoint Online and Office 365. You will love your app. Keep reading and keep learning!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s