SharePoint Add-ins are self-contained extensions of SharePoint websites that you create, and that run without custom code on the SharePoint server.

In SharePoint hosted apps, you can use almost all the SharePoint components like list, content type, pages etc.…

All business logic in a SharePoint-hosted add-in, use JavaScript, either directly on a custom page or as a JavaScript file that is referenced from a custom page. A JavaScript version of the SharePoint Object Model (JSOM) is available to make it simple for the add-in to perform create, read, update, and delete (CRUD) operations on SharePoint data.

Over View of Example

  1. Create List in Office 365 Site
  2. Create Project in Visual Studio (Selecting SharePoint Hosted App)
  3. Provide User Interface
  4. Business logic under .js file (Insert Data)
  5. Deploy the Solution to office 365 Site.

Let’s take an example of SharePoint hosted app.

Step - 1 (Create List in Office 365 Site)

Open your office 365 Share Point site and create one list. Below find the steps and screen shots to create list with columns.

Now, we need to create some columns for this list.















We have done creation of List with columns. Now, we have to insert the data to the list, using User Interface and business logic.

Step - 2 (Create Project in Visual Studio (Selecting SharePoint Hosted App)

Below, find the Screenshots for creating the SharePoint Hosted App through Visual Studio, using SharePoint hosted app.













As of now, we have done creation of project through Visual studio. After creation of project, Project contains different hierarchy like pages, Scripts, AppManifest.xml and etc.

Below find the screen shot for AppManifest.xml file.



Step - 3 (Provide User Interface)

We are going to provide some text boxes, labels, and buttons for Inserting and displaying the data.

After writing the above code, User Interface will look like the below. You will not get a result immediately. Don’t bother about this. I have given this screenshot just for reference.



Step - 4 (Business logic under .js file (Insert and Display Data))

After completion of user Interface, we need to write the business logic for Insertion and Display operations. You can write business logic in App.js file or same page (default.aspx), or you can take separate .js file.

For this example, I am taking App.js file to write business logic. Please follow the below steps to write the code under App.js file.

The following method is used for retrieving data from the list and display in a table.

  1. //Get List items of Employee List
  2. function DisplayEmployeeDetails() {
  3. var contxt = new SP.ClientContext(appWebUrl);
  4. var appContxt = new SP.AppContextSite(contxt, hostWebUrl);
  5. var web = appContxt.get_web(); //Get the Web
  6. var list = web.get_lists().getByTitle("Employees"); //Get the List
  7. var empQuery = new SP.CamlQuery();
  8. empQuery.set_viewXml('<View><RowLimit></RowLimit>50</View>');
  9. var items = list.getItems(empQuery);
  10. contxt.load(list);
  11. contxt.load(items);
  12. var table = $("#tblEmployees");
  13. var html = "<thead><tr><th>EmployeeId</<th><th>Name</th><th>Salary</th><th>Address</th></tr></thead>";
  14. //Execute the Query Asynchronously
  15. contxt.executeQueryAsync(
  16. Function.createDelegate(this, function () {
  17. var itemInfo = '';
  18. var enumerator = items.getEnumerator();
  19. while (enumerator.moveNext()) {
  20. var currentEmpItem = enumerator.get_current();
  21. html += "<tr><td>" + currentEmpItem.get_item('ID') + "</td><td>" + currentEmpItem.get_item('Title') + "</td><td>" + currentEmpItem.get_item('Salary') + "</td><td>" + currentEmpItem.get_item('Address') + "</td></tr>";
  22. }
  23. table.html(html);
  24. }),
  25. Function.createDelegate(this, onQueryFailedGet)
  26. );
  27. }
  28. function onQueryFailedGet() {
  29. $("#dvMessage").text("Display failed " + arguments[1].get_message());
  30. }
The following method is used for inserting the data to Employee List.
  1. //Insert data into Employee List.
  2. function createEmployee() {
  3. var contxt = new SP.ClientContext(appWebUrl);
  4. var appContxt = new SP.AppContextSite(contxt, hostWebUrl);
  5. var web = appContxt.get_web(); //Get the Web
  6. var list = web.get_lists().getByTitle("Employees"); //Get the List
  7. var listCreationInformation = new SP.ListItemCreationInformation();
  8. var listItem = list.addItem(listCreationInformation);
  9. listItem.set_item("Title", $("#empName").val());
  10. listItem.set_item("Salary", $("#empSalary").val());
  11. listItem.set_item("Address", $("#empAddress").val());
  12. listItem.update(); //Update the list Item.
  13. contxt.load(listItem);
  14. //Execute the batch Asynchronously
  15. contxt.executeQueryAsync(
  16. Function.createDelegate(this, onQuerySucceededCreate),
  17. Function.createDelegate(this, onQueryFailedCreate));
  18. }
  19. function onQuerySucceededCreate() {
  20. $("#dvMessage").text("Employee Information Submitted Successfully");
  21. }
  22. function onQueryFailedCreate() {
  23. $("#dvMessage").text("Submission failed " + arguments[1].get_message());
  24. }
And, I have used CSS for table as well as headers.

Open the Content-> App.css file and paste the below css.
  1. /* Place custom styles below */
  2. table.mytable {
  3. border-collapse: collapse;
  4. width: 100%;
  5. }
  6. th, td {
  7. text-align: left;
  8. padding: 8px;
  9. }
  10. tr:nth-child(even){background-color: #f2f2f2}
  11. th {
  12. background-color: #4CAF50;
  13. color: white;
  14. }
Finally, App.js file looks like below and copy paste the below code under App.js.
  1. //-------------------------------------------------------------------------------//
  2. 'use strict';
  3. var hostWebUrl;
  4. var appWebUrl;
  5. var context = SP.ClientContext.get_current();
  6. var user = context.get_web().get_currentUser();
  7. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  8. $(document).ready(function () {
  9. hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  10. appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
  11. DisplayEmployeeDetails();
  12. $("#btnCreate").on('click', function () {
  13. createEmployee();
  14. DisplayEmployeeDetails();
  15. });
  16. $("#btnClear").on('click', function () {
  17. $(".csValue").val('');
  18. });
  19. });
  20. function manageQueryStringParameter(paramToRetrieve) {
  21. var params =
  22. document.URL.split("?")[1].split("&");
  23. var strParams = "";
  24. for (var i = 0; i < params.length; i = i + 1) {
  25. var singleParam = params[i].split("=");
  26. if (singleParam[0] == paramToRetrieve) {
  27. return singleParam[1];
  28. }
  29. }
  30. }
  31. //Get List items of Employee List
  32. function DisplayEmployeeDetails() {
  33. var contxt = new SP.ClientContext(appWebUrl);
  34. var appContxt = new SP.AppContextSite(contxt, hostWebUrl);
  35. var web = appContxt.get_web(); //Get the Web
  36. var list = web.get_lists().getByTitle("Employees"); //Get the List
  37. var empQuery = new SP.CamlQuery();
  38. empQuery.set_viewXml('<View><RowLimit></RowLimit>50</View>');
  39. var items = list.getItems(empQuery);
  40. contxt.load(list);
  41. contxt.load(items);
  42. var table = $("#tblEmployees");
  43. var html = "<thead><tr><th>EmployeeId</<th><th>Name</th><th>Salary</th><th>Address</th></tr></thead>";
  44. //Execute the Query Asynchronously
  45. contxt.executeQueryAsync(
  46. Function.createDelegate(this, function () {
  47. var itemInfo = '';
  48. var enumerator = items.getEnumerator();
  49. while (enumerator.moveNext()) {
  50. var currentEmpItem = enumerator.get_current();
  51. html += "<tr><td>" + currentEmpItem.get_item('ID') + "</td><td>" + currentEmpItem.get_item('Title') + "</td><td>" + currentEmpItem.get_item('Salary') + "</td><td>" + currentEmpItem.get_item('Address') + "</td></tr>";
  52. }
  53. table.html(html);
  54. }),
  55. Function.createDelegate(this, onQueryFailedGet)
  56. );
  57. }
  58. function onQueryFailedGet() {
  59. $("#dvMessage").text("Display failed " + arguments[1].get_message());
  60. }
  61. //Insert data into Employee List.
  62. function createEmployee() {
  63. var contxt = new SP.ClientContext(appWebUrl);
  64. var appContxt = new SP.AppContextSite(contxt, hostWebUrl);
  65. var web = appContxt.get_web(); //Get the Web
  66. var list = web.get_lists().getByTitle("Employees"); //Get the List
  67. var listCreationInformation = new SP.ListItemCreationInformation();
  68. var listItem = list.addItem(listCreationInformation);
  69. listItem.set_item("Title", $("#empName").val());
  70. listItem.set_item("Salary", $("#empSalary").val());
  71. listItem.set_item("Address", $("#empAddress").val());
  72. listItem.update(); //Update the list Item.
  73. contxt.load(listItem);
  74. //Execute the batch Asynchronously
  75. contxt.executeQueryAsync(
  76. Function.createDelegate(this, onQuerySucceededCreate),
  77. Function.createDelegate(this, onQueryFailedCreate));
  78. }
  79. function onQuerySucceededCreate() {
  80. $("#dvMessage").text("Employee Information Submitted Successfully");
  81. }
  82. function onQueryFailedCreate() {
  83. $("#dvMessage").text("Submission failed " + arguments[1].get_message());
  84. }
  85. // This function prepares, loads, and then executes a SharePoint query to get the current users information
  86. function getUserName() {
  87. context.load(user);
  88. context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  89. }
  90. // This function is executed if the above call is successful
  91. // It replaces the contents of the 'message' element with the user name
  92. function onGetUserNameSuccess() {
  93. $('#message').text('Hello ' + user.get_title());
  94. }
  95. // This function is executed if the above call fails
  96. function onGetUserNameFail(sender, args) {
  97. alert('Failed to get user name. Error:' + args.get_message());
  98. }
  99. //------------------------------------------------------------//
Step- 5 (Deploy the Solution to Office 365 Site)

Deploy the solution to Office 365 site. Follow these steps to deploy the solution.







We have done the Share point hosted app basic operations, like Insertion and Display the data. Please let me know if you have any queries.

References used- https://msdn.microsoft.com/en-us/library/office/fp179930.aspx