Here are the steps,
Step 1: Create SharePoint Hosted App with Office 365 account credentials. Open Visual Studio and select New Project as in the following,

Step 2
: Specify Site URL & app type to proceed next.



Step 3: Provide Office365 credentials to continue further.


Step 4
: Project structure is as in the following,



Step 5: We first need to set permission for App, so that we can communicate with site properly.


Step 6
: Deploy the app into Office 365 environment.

Step 7: User Interface creation code

In this part, we are going to perform simple operation so UI is also very simple as in the following example,

Source Code
  1. <%@ Page Language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2. <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  3. <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  4. <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  5. <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
  6. <SharePoint:ScriptLink Name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
  7. <script src="../Scripts/jquery-1.8.2.js" type="text/javascript"></script>
  8. <script src="../Scripts/JSLibrary.js" type="text/javascript"></script>
  9. </asp:Content>
  10. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
  11. <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="full" Title="loc:full" />
  12. <hr />
  13. <div>
  14. <h2><b>Office 365 Host Web Details</b></h2>
  15. <br />
  16. <div id="HostwebDetails"></div>
  17. </div>
  18. <hr />
  19. <div>
  20. <h3><b>Folder Creation</b></h3>
  21. <input type="text" id="txtFolderName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWCreateFolder()">Create Folder</a> </div>
  22. <hr />
  23. <div>
  24. <h3><b>List Creation</b></h3>
  25. <input type="text" id="txtListName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWCreateList()">Create List</a> </div>
  26. <hr />
  27. <div>
  28. <h3><b>List Deletion</b></h3>
  29. <input type="text" id="txtDeletionListName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWDeleteList()">Delete List</a> </div>
  30. <hr/> </asp:Content>
Step 8: Fetch current Web Site details on page load. The following code is used to perform this operation.
  1. var clientContext;
  2. var factory;
  3. var appContextSite;
  4. var web;
  5. var collList;
  6. var itemCreateInfo;
  7. var param1, param2, param3;
  8. var oList, targetList, listFields, oListItem, oField, itemId;
  9. var ExsitingListNameVal;
  10. window.Wingtip = window.Wingtip ||
  11. {}
  12. Wingtip.JSOMHostWebCollections = function()
  13. {
  14. LoadlayoutsJS = function()
  15. {
  16. //debugger
  17. hostweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPHostUrl"));
  18. appweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPAppWebUrl"));
  19. var Sessionhostweburl = sessionStorage.getItem("hostweburl");
  20. var Sessionappweburl = sessionStorage.getItem("appweburl");
  21. if (Sessionhostweburl == null || Sessionappweburl == null)
  22. {
  23. sessionStorage.setItem("hostweburl", hostweburl);
  24. sessionStorage.setItem("appweburl", appweburl);
  25. }
  26. if (hostweburl == null || appweburl == null || hostweburl == 'undefined' || appweburl == 'undefined')
  27. {
  28. hostweburl = sessionStorage.getItem("hostweburl");
  29. appweburl = sessionStorage.getItem("appweburl");
  30. }
  31. var scriptbase = hostweburl + "/_layouts/15/";
  32. $.getScript(scriptbase + "SP.Runtime.js", function()
  33. {
  34. $.getScript(scriptbase + "SP.js", function()
  35. {
  36. $.getScript(scriptbase + "SP.RequestExecutor.js", Wingtip.JSOMHostWebCollections.HWLoadClientContext);
  37. });
  38. });
  39. }
  40. getQueryStringParameter = function(paramToRetrieve)
  41. {
  42. //debugger
  43. try
  44. {
  45. var params = document.URL.split("?")[1].split("&");
  46. var strParams = "";
  47. for (var i = 0; i < params.length; i = i + 1)
  48. {
  49. var singleParam = params[i].split("=");
  50. if (singleParam[0] == paramToRetrieve) return singleParam[1];
  51. }
  52. }
  53. catch (ex)
  54. {}
  55. }
  56. LoadClientContext = function()
  57. {
  58. //debugger
  59. clientContext = new SP.ClientContext(appweburl);
  60. factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  61. clientContext.set_webRequestExecutorFactory(factory);
  62. appContextSite = new SP.AppContextSite(clientContext, hostweburl);
  63. web = appContextSite.get_web();
  64. clientContext.load(web);
  65. Wingtip.JSOMHostWebCollections.HWGetTitle();
  66. //Wingtip.JSOMHostWebCollections.HWCreateFolder("Rupali");
  67. //Wingtip.JSOMHostWebCollections.HWCreateList("Book");
  68. //Wingtip.JSOMHostWebCollections.HWDeleteList("Book");
  69. }
  70. GetTitle = function()
  71. {
  72. //debugger
  73. web.set_title('Office365 Host Web');
  74. web.update();
  75. clientContext.load(web);
  76. clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebDetailsSuccessHandler), Function.createDelegate(this, SPHostWebDetailsErrorHandler));
  77. function SPHostWebDetailsSuccessHandler()
  78. {
  79. var Result = "";
  80. Result = Result + "<b>Title :</b> " + web.get_title() + "<br/>";
  81. Result = Result + "<b> ID :</b> " + web.get_id() + "</br>";
  82. Result = Result + "<b> Created :</b> " + web.get_created() + "</br>";
  83. document.getElementById("HostwebDetails").innerHTML = Result;
  84. }
  85. function SPHostWebDetailsErrorHandler(data, errorCode, errorMessage)
  86. {
  87. alert("fail" + errorMessage);
  88. document.getElementById("HostwebTitle").innerText = "Could not complete cross-domain call: " + errorMessage;
  89. }
  90. }
  91. //public interface
  92. return {
  93. HWLoadlayoutsJS: LoadlayoutsJS,
  94. QSParameter: getQueryStringParameter,
  95. HWLoadClientContext: LoadClientContext,
  96. HWGetTitle: GetTitle,
  97. HWCreateFolder: CreateFolder,
  98. HWCreateList: CreateList,
  99. HWDeleteList: DeleteList,
  100. HWEmpty: Empty
  101. }
  102. }();
  103. $(document).ready(function()
  104. {
  105. //Load Out Of Box JS file for List Operation
  106. Wingtip.JSOMHostWebCollections.HWLoadlayoutsJS();
  107. });
Output


Step 9:
Create folder at site, we can use the following code,
  1. CreateFolder = function()
  2. {
  3. //debugger
  4. oList = web.get_lists().getByTitle("Documents");
  5. itemCreateInfo = new SP.ListItemCreationInformation();
  6. itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
  7. var FolderNameVal = $(txtFolderName).val();
  8. console.log(FolderNameVal);
  9. itemCreateInfo.set_leafName(FolderNameVal);
  10. //itemCreateInfo.set_leafName("My new folder!!!");
  11. oListItem = oList.addItem(itemCreateInfo);
  12. oListItem.update();
  13. clientContext.load(oListItem);
  14. clientContext.executeQueryAsync(Function.createDelegate(this, CreateFolderSuccessHandler), Function.createDelegate(this, CreateFolderErrorHandler));
  15. function CreateFolderSuccessHandler()
  16. {
  17. alert("Go to Site Content ->Documents to see your new folder.");
  18. }
  19. function CreateFolderErrorHandler()
  20. {
  21. alert("Request failed: " + arguments[1].get_message());
  22. }
  23. }
Output


New folder is getting created inside Document folder at root level.


Step 10:
Create List at host web using the following code,
  1. CreateList = function()
  2. {
  3. //debugger
  4. var listCreationInfo = new SP.ListCreationInformation();
  5. var ListNameVal = $(txtListName).val();
  6. //var ListNameVal = ListName;
  7. listCreationInfo.set_title(ListNameVal);
  8. listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
  9. oList = web.get_lists().add(listCreationInfo);
  10. clientContext.load(oList);
  11. oList.set_description(ListNameVal + 'created via JSOM Model ...');
  12. oList.update();
  13. clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebCreateListSuccessHandler), Function.createDelegate(this, SPHostWebCreateListErrorHandler));
  14. function SPHostWebCreateListSuccessHandler()
  15. {
  16. var result = oList.get_title() + ' List created successfully.';
  17. alert(result);
  18. }
  19. function SPHostWebCreateListErrorHandler(sender, args)
  20. {
  21. alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  22. }
  23. }
Output


Finally list is created at host web.


Step 11:
Delete list at host web using the following code,
  1. DeleteList = function()
  2. {
  3. // debugger
  4. var DeleteListNameVal = $(txtDeletionListName).val();
  5. oList = web.get_lists().getByTitle(DeleteListNameVal);
  6. oList.deleteObject();
  7. clientContext.executeQueryAsync(Function.createDelegate(this, DeleteListHostWebSucceeded), Function.createDelegate(this, DeleteListHostWebFailed));
  8. function DeleteListHostWebSucceeded()
  9. {
  10. alert("Delete List done Successfully ...");
  11. }
  12. function DeleteListHostWebFailed(sender, args)
  13. {
  14. alert('Request failed. ' + args.get_message() + '\n StackTrace : ' + args.get_stackTrace());
  15. }
  16. }
Output


Full Source Code
  1. var clientContext;
  2. var factory;
  3. var appContextSite;
  4. var web;
  5. var collList;
  6. var itemCreateInfo;
  7. var param1, param2, param3;
  8. var oList, targetList, listFields, oListItem, oField, itemId;
  9. var ExsitingListNameVal;
  10. window.Wingtip = window.Wingtip ||
  11. {}
  12. Wingtip.JSOMHostWebCollections = function()
  13. {
  14. LoadlayoutsJS = function()
  15. {
  16. //debugger
  17. hostweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPHostUrl"));
  18. appweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPAppWebUrl"));
  19. var Sessionhostweburl = sessionStorage.getItem("hostweburl");
  20. var Sessionappweburl = sessionStorage.getItem("appweburl");
  21. if (Sessionhostweburl == null || Sessionappweburl == null)
  22. {
  23. sessionStorage.setItem("hostweburl", hostweburl);
  24. sessionStorage.setItem("appweburl", appweburl);
  25. }
  26. if (hostweburl == null || appweburl == null || hostweburl == 'undefined' || appweburl == 'undefined')
  27. {
  28. hostweburl = sessionStorage.getItem("hostweburl");
  29. appweburl = sessionStorage.getItem("appweburl");
  30. }
  31. var scriptbase = hostweburl + "/_layouts/15/";
  32. $.getScript(scriptbase + "SP.Runtime.js", function()
  33. {
  34. $.getScript(scriptbase + "SP.js", function()
  35. {
  36. $.getScript(scriptbase + "SP.RequestExecutor.js", Wingtip.JSOMHostWebCollections.HWLoadClientContext);
  37. });
  38. });
  39. }
  40. getQueryStringParameter = function(paramToRetrieve)
  41. {
  42. //debugger
  43. try
  44. {
  45. var params = document.URL.split("?")[1].split("&");
  46. var strParams = "";
  47. for (var i = 0; i < params.length; i = i + 1)
  48. {
  49. var singleParam = params[i].split("=");
  50. if (singleParam[0] == paramToRetrieve) return singleParam[1];
  51. }
  52. }
  53. catch (ex)
  54. {}
  55. }
  56. LoadClientContext = function()
  57. {
  58. //debugger
  59. clientContext = new SP.ClientContext(appweburl);
  60. factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  61. clientContext.set_webRequestExecutorFactory(factory);
  62. appContextSite = new SP.AppContextSite(clientContext, hostweburl);
  63. web = appContextSite.get_web();
  64. clientContext.load(web);
  65. Wingtip.JSOMHostWebCollections.HWGetTitle();
  66. //Wingtip.JSOMHostWebCollections.HWCreateFolder("Rupali");
  67. //Wingtip.JSOMHostWebCollections.HWCreateList("Book");
  68. //Wingtip.JSOMHostWebCollections.HWDeleteList("Book");
  69. }
  70. GetTitle = function()
  71. {
  72. //debugger
  73. web.set_title('Office365 Host Web');
  74. web.update();
  75. clientContext.load(web);
  76. clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebDetailsSuccessHandler), Function.createDelegate(this, SPHostWebDetailsErrorHandler));
  77. function SPHostWebDetailsSuccessHandler()
  78. {
  79. var Result = "";
  80. Result = Result + "<b>Title :</b> " + web.get_title() + "<br/>";
  81. Result = Result + "<b> ID :</b> " + web.get_id() + "</br>";
  82. Result = Result + "<b> Created :</b> " + web.get_created() + "</br>";
  83. document.getElementById("HostwebDetails").innerHTML = Result;
  84. }
  85. function SPHostWebDetailsErrorHandler(data, errorCode, errorMessage)
  86. {
  87. alert("fail" + errorMessage);
  88. document.getElementById("HostwebTitle").innerText = "Could not complete cross-domain call: " + errorMessage;
  89. }
  90. }
  91. CreateFolder = function()
  92. {
  93. //debugger
  94. oList = web.get_lists().getByTitle("Documents");
  95. itemCreateInfo = new SP.ListItemCreationInformation();
  96. itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
  97. var FolderNameVal = $(txtFolderName).val();
  98. console.log(FolderNameVal);
  99. itemCreateInfo.set_leafName(FolderNameVal);
  100. //itemCreateInfo.set_leafName("My new folder!!!");
  101. oListItem = oList.addItem(itemCreateInfo);
  102. oListItem.update();
  103. clientContext.load(oListItem);
  104. clientContext.executeQueryAsync(Function.createDelegate(this, CreateFolderSuccessHandler), Function.createDelegate(this, CreateFolderErrorHandler));
  105. function CreateFolderSuccessHandler()
  106. {
  107. alert("Go to Site Content ->Documents to see your new folder.");
  108. }
  109. function CreateFolderErrorHandler()
  110. {
  111. alert("Request failed: " + arguments[1].get_message());
  112. }
  113. }
  114. CreateList = function()
  115. {
  116. //debugger
  117. var listCreationInfo = new SP.ListCreationInformation();
  118. var ListNameVal = $(txtListName).val();
  119. //var ListNameVal = ListName;
  120. listCreationInfo.set_title(ListNameVal);
  121. listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
  122. oList = web.get_lists().add(listCreationInfo);
  123. clientContext.load(oList);
  124. oList.set_description(ListNameVal + 'created via JSOM Model ...');
  125. oList.update();
  126. clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebCreateListSuccessHandler), Function.createDelegate(this, SPHostWebCreateListErrorHandler));
  127. function SPHostWebCreateListSuccessHandler()
  128. {
  129. var result = oList.get_title() + ' List created successfully.';
  130. alert(result);
  131. }
  132. function SPHostWebCreateListErrorHandler(sender, args)
  133. {
  134. alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  135. }
  136. }
  137. DeleteList = function()
  138. {
  139. // debugger
  140. var DeleteListNameVal = $(txtDeletionListName).val();
  141. oList = web.get_lists().getByTitle(DeleteListNameVal);
  142. oList.deleteObject();
  143. clientContext.executeQueryAsync(Function.createDelegate(this, DeleteListHostWebSucceeded), Function.createDelegate(this, DeleteListHostWebFailed));
  144. function DeleteListHostWebSucceeded()
  145. {
  146. alert("Delete List done Successfully ...");
  147. }
  148. function DeleteListHostWebFailed(sender, args)
  149. {
  150. alert('Request failed. ' + args.get_message() + '\n StackTrace : ' + args.get_stackTrace());
  151. }
  152. }
  153. function Empty()
  154. {
  155. console('Hello');
  156. }
  157. //public interface
  158. return {
  159. HWLoadlayoutsJS: LoadlayoutsJS,
  160. QSParameter: getQueryStringParameter,
  161. HWLoadClientContext: LoadClientContext,
  162. HWGetTitle: GetTitle,
  163. HWCreateFolder: CreateFolder,
  164. HWCreateList: CreateList,
  165. HWDeleteList: DeleteList,
  166. HWEmpty: Empty
  167. }
  168. }();
  169. $(document).ready(function()
  170. {
  171. //Load Out Of Box JS file for List Operation
  172. Wingtip.JSOMHostWebCollections.HWLoadlayoutsJS();
  173. });