Creating List and Columns Using SharePoint Hosted Add-ins
Softree Technology > Blogs > Office 365 > Creating List and Columns Using SharePoint Hosted Add-ins

Creating List and Columns Using SharePoint Hosted Add-ins

  • Posted by: Softree Technology

Introduction :

In this blog, I have described step by step how to create a list and column using SharePoint-self hosted Add-ins. To create the List and List Columns using SharePoint Hosted Apps, you have to follow the below steps.

Step 1- Create a SharePoint Hosted App project.
Step 2- Write HTML code under Default.aspx.
Step 3- Add JavaScript code under App.js file.
Step 4- Deploy the Project.

  1. Create a SharePoint Hosted App project :
  • Open visual studio to create a SharePoint hosted Add-In. First, click the file menu option then click the new item. After that we can see the option for Project item, now click on the same option.
  • Expand the office/SharePoint tab and select SharePoint Add-in. Enter the project name & location of the project then hit the OK button.
  • Select the SharePoint site URL & Hosted add-in as shown below and then click on the Next button.
  • Now it will ask for your credentials. Enter the user Credentials (Username & Password) and Click the “Sign-in” Option.
  • Once the SharePoint hosted App is created, then we can add our code.

2. Write HTML code under Default.aspx :

Go to Default.aspx page. Create a label and a button inside the div class as shown below.

<tr>
       <td><label id="btnCreate" style="font-size:large;font-style:normal">Enter The List Name You Want To Give</label></td> 
       <td><input type="text" id="txtListName" name="txtListName" /></td>
       <td><input type="button" id="crtBtn" value="CreateList" /></td>
</tr>

3. Add Javascript code under App.js file :

Go to App.js . Add your coding.

function initializePage() {
    var context = SP.ClientContext.get_current();
    var user = context.get_web().get_currentUser();
    
    $(document)x.ready(function () {  $("#crtBtn").on('click', function () {
            CreateList();
        });
function getURLParameters(param)
    {
        var params = document.URL.split('?')[1].split('&');
        var strParams = '';
        var plength = params.length;
        for (var i = 0; i < plength; i = i + 1) {
            var singleParam = params[i].split('=');
            if (singleParam[0] == param) 
               {
                 return singleParam[1];
               }
           }
        }

             
   function CreateList() 
    {
      var hostUrl = decodeURIComponent(getURLParameters("SPHostUrl"));
      var clientContext = new SP.ClientContext.get_current();
      var hostContext = new SP.AppContextSite(clientContext, hostUrl);
      var oWeb = hostcontext.get_web();
      var value =document.getElementById("txtListname").value.toLowerCase();
      var listCreation = new SP.ListCreationInformation();
      listCreation.set_title(value);
      listCreation.set_templateType(SP.ListTemplateType.genericList);
      var mySpList = oWeb.get_lists().add(listCreationInfo);
      var fieldColl = mySpList.get_fields();
      var spTxtField = fieldColl.addFieldAsXml('<Field Type="Text" DisplayName="Employee Name" Name="Employee Name" />', true, SP.AddFieldOptions.addToDefaultContentType);
     spTxtField.set_title("Employee Name");
     var spDescripField = fieldCol.addFieldAsXml('<Field Type="Note" DisplayName="Address" Name="Address" />', true, SP.AddFieldOptions.addToDefaultContentType);
    spDescripField.set_title("Address");
    mySpList.update();
    clientContext.executeQueryAsync(function () {
        alert("successfully Created The List and List Field");
    },
       function (sender, args) {
           alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
       });
   }
}

4. Deploy the Project :

Go to the “Solution Explorer”, Right-click on the project and click on Deploy.

  • Once you deploy it, enter your office365 credentials.
  • Now you can see your App page.
  • Enter the list name and click on the CreateList butto
  • Now you can see the list in your sitecollection.
  • And the NewList contains two columns as shown below.

Keywords:

• Using SharePoint hosted app how to create list and columns.
• Creation of list using SharePoint hosted adding.
• Programmatically creating list and columns using SharePoint hosted app.
• Create custom list and column programmatically using js.

Author: Softree Technology

Leave a Reply