Create an App in Office 365 and SharePoint 2013: Part 3


Here in this article we will see a different app that you will be able to use in your daily job profiles and it’s actually going to help you a lot.

As per my last statement “When we all see some new apps, the first thing of a techie guy that comes in mind is “Even I want to make an app” but people either get carried away or are unable to decide how to start.”.

So, let’s go ahead and determine how to implement our new app.

  • Open your site in Office 365 or on On-Premise.
  • Click on the Site settings and click on Add an app.

    Site settings

  • Add “Napa” Office 365 Development Tools from the SharePoint Store.

    Once added click on it

  • Once you have added the tool, it will be available in your site contents. This app is a tool to start building apps for Office and SharePoint.
  • Once added, click on it.

    click

  • It will ask you what kind of app you want to build, for us it will be App for SharePoint.
  • Provide it a name and click on Create.
  • In this article we will create an app where on a button click you will be able to delete a list form a selection of drop down.
  • Here we will have a drop down associated to a button. The drop down will reflect all the lists in the site and on the selection of it a user can delete a its directly by clicking on a button next to it.
  • Let’s see how we will do it.
  • Now when your app has been created, you will come on the default.aspx page of your app displaying the code behind.
  • Paste this code under PlaceHolder Main.

    <asp:Content ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

    Code

    1. – 
      “starter”>
    2. – 
    3.    – Lists
    4. – 
    5. – “selectlistbox” >”deletelistbutton”>Delete Selected List

– Here we are creating a button and a drop down box where we will generate an event.

  • Now we need to write a function for the button to call so click on App.js.

  • Here in App.js, copy the following variables on the top.
    1. var context = SP.ClientContext.get_current();
    2. var user = context.get_web().get_currentUser();
    3. var web = context.get_web();
    4. var lists = web.get_lists();
    5. var listItemCollection;

    – Here we assign the variables to be used in functions.

  • Under the document.ready function, paste the following lines of code to call the function.
    1. $(document).ready(function () {
    2.    $(“#deletelistbutton”).click(function (event) {
    3.       deletelist();
    4.       event.preventDefault();
    5.    });
    6.    displayLists();
    7. });

    Here under the document.ready function we are calling our function on page load.

  • Then paste the following function where we will display all the lists in a drop down that a user can select to delete:
    1. function displayLists() {
    2.       // Get all the SharePoint lists and add them to context.load
    3.       lists = web.get_lists();
    4.       context.load(lists);
    5.       context.executeQueryAsync(onGetLists, onGetListsFail);
    6.   }
    7.   function onGetLists (sender, args) {
    8.       // Get the makes and set the elements to the list box.
    9.       var listEnumerator = lists.getEnumerator();
    10.       var selectListBox = document.getElementById(“selectlistbox”);
    11.       if (selectListBox.hasChildNodes()) {
    12.           while (selectListBox.childNodes.length >= 1) {
    13.               selectListBox.removeChild(selectListBox.firstChild);
    14.           }
    15.       }
    16.       while (listEnumerator.moveNext()) {
    17.           var selectOption = document.createElement(“option”);
    18.           selectOption.value = listEnumerator.get_current().get_title();
    19.           selectOption.innerHTML = listEnumerator.get_current().get_title();
    20.           selectListBox.appendChild(selectOption);
    21.       }
    22.   }
    23.   function onGetListsFail(sender, args) {
    24.       // Display the error
    25.       alert(‘Failed to fetch lists. Error: ‘ + args.get_message());
    26.   }
  • Now here we will get the main function of deleting it.
    1. function deletelist() {
    2.         // Delete the list that the user selects.
    3.         var selectListBox = document.getElementById(“selectlistbox”);
    4.         var selectedListTitle = selectListBox.value;
    5.         var selectedList = web.get_lists().getByTitle(selectedListTitle);
    6.         selectedList.deleteObject();
    7.         context.executeQueryAsync(onDeleteList, onDeleteFail);
    8.     }
    9.     function onDeleteList () {
    10.         displayLists();
    11.     }
    12.     function onDeleteFail(sender, args) {
    13.         alert(‘Failed to delete the list. ‘ + args.get_message());
    14.     }

    – Now once clicked on delete, the display list will not show the deleted list.

    – Now after this, let’s deploy the code.

    deploy the code

  • Once you make all necessary updates to you code, click on the Play icon button that states “Run Project”.
  • It will first prepare then deploy then launch.

    launch

  • Here we have our app deployed with a button stating “Delete Selected List” and a drop down showing all the list name.
  • Select the list form the drop down you want to delete and click on the button.

    Select the list form the drop down

  • Your list will be deleted.
  • Here it was our third app showing how to delete lists on selection. Soon we will see many new apps on our way. Until then, keep learning.

    Cheers!