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- <%@ Page Language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
- <SharePoint:ScriptLink Name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
- <script src="../Scripts/jquery-1.8.2.js" type="text/javascript"></script>
- <script src="../Scripts/JSLibrary.js" type="text/javascript"></script>
- </asp:Content>
- <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
- <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="full" Title="loc:full" />
- <hr />
- <div>
- <h2><b>Office 365 Host Web Details</b></h2>
- <br />
- <div id="HostwebDetails"></div>
- </div>
- <hr />
- <div>
- <h3><b>Folder Creation</b></h3>
- <input type="text" id="txtFolderName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWCreateFolder()">Create Folder</a> </div>
- <hr />
- <div>
- <h3><b>List Creation</b></h3>
- <input type="text" id="txtListName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWCreateList()">Create List</a> </div>
- <hr />
- <div>
- <h3><b>List Deletion</b></h3>
- <input type="text" id="txtDeletionListName" /> <a href="#" onclick="Wingtip.JSOMHostWebCollections.HWDeleteList()">Delete List</a> </div>
- <hr/> </asp:Content>
Step 8: Fetch current Web Site details on page load. The following code is used to perform this operation.
- var clientContext;
- var factory;
- var appContextSite;
- var web;
- var collList;
- var itemCreateInfo;
- var param1, param2, param3;
- var oList, targetList, listFields, oListItem, oField, itemId;
- var ExsitingListNameVal;
- window.Wingtip = window.Wingtip ||
- {}
- Wingtip.JSOMHostWebCollections = function()
- {
- LoadlayoutsJS = function()
- {
-
- hostweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPHostUrl"));
- appweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPAppWebUrl"));
- var Sessionhostweburl = sessionStorage.getItem("hostweburl");
- var Sessionappweburl = sessionStorage.getItem("appweburl");
- if (Sessionhostweburl == null || Sessionappweburl == null)
- {
- sessionStorage.setItem("hostweburl", hostweburl);
- sessionStorage.setItem("appweburl", appweburl);
- }
- if (hostweburl == null || appweburl == null || hostweburl == 'undefined' || appweburl == 'undefined')
- {
- hostweburl = sessionStorage.getItem("hostweburl");
- appweburl = sessionStorage.getItem("appweburl");
- }
- var scriptbase = hostweburl + "/_layouts/15/";
- $.getScript(scriptbase + "SP.Runtime.js", function()
- {
- $.getScript(scriptbase + "SP.js", function()
- {
- $.getScript(scriptbase + "SP.RequestExecutor.js", Wingtip.JSOMHostWebCollections.HWLoadClientContext);
- });
- });
- }
- getQueryStringParameter = function(paramToRetrieve)
- {
-
- try
- {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1)
- {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) return singleParam[1];
- }
- }
- catch (ex)
- {}
- }
- LoadClientContext = function()
- {
-
- clientContext = new SP.ClientContext(appweburl);
- factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
- clientContext.set_webRequestExecutorFactory(factory);
- appContextSite = new SP.AppContextSite(clientContext, hostweburl);
- web = appContextSite.get_web();
- clientContext.load(web);
- Wingtip.JSOMHostWebCollections.HWGetTitle();
-
-
-
- }
- GetTitle = function()
- {
-
- web.set_title('Office365 Host Web');
- web.update();
- clientContext.load(web);
- clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebDetailsSuccessHandler), Function.createDelegate(this, SPHostWebDetailsErrorHandler));
-
- function SPHostWebDetailsSuccessHandler()
- {
- var Result = "";
- Result = Result + "<b>Title :</b> " + web.get_title() + "<br/>";
- Result = Result + "<b> ID :</b> " + web.get_id() + "</br>";
- Result = Result + "<b> Created :</b> " + web.get_created() + "</br>";
- document.getElementById("HostwebDetails").innerHTML = Result;
- }
-
- function SPHostWebDetailsErrorHandler(data, errorCode, errorMessage)
- {
- alert("fail" + errorMessage);
- document.getElementById("HostwebTitle").innerText = "Could not complete cross-domain call: " + errorMessage;
- }
- }
-
- return {
- HWLoadlayoutsJS: LoadlayoutsJS,
- QSParameter: getQueryStringParameter,
- HWLoadClientContext: LoadClientContext,
- HWGetTitle: GetTitle,
- HWCreateFolder: CreateFolder,
- HWCreateList: CreateList,
- HWDeleteList: DeleteList,
- HWEmpty: Empty
- }
- }();
- $(document).ready(function()
- {
-
- Wingtip.JSOMHostWebCollections.HWLoadlayoutsJS();
- });
Step 9: Create folder at site, we can use the following code,
- CreateFolder = function()
- {
-
- oList = web.get_lists().getByTitle("Documents");
- itemCreateInfo = new SP.ListItemCreationInformation();
- itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
- var FolderNameVal = $(txtFolderName).val();
- console.log(FolderNameVal);
- itemCreateInfo.set_leafName(FolderNameVal);
-
- oListItem = oList.addItem(itemCreateInfo);
- oListItem.update();
- clientContext.load(oListItem);
- clientContext.executeQueryAsync(Function.createDelegate(this, CreateFolderSuccessHandler), Function.createDelegate(this, CreateFolderErrorHandler));
-
- function CreateFolderSuccessHandler()
- {
- alert("Go to Site Content ->Documents to see your new folder.");
- }
-
- function CreateFolderErrorHandler()
- {
- alert("Request failed: " + arguments[1].get_message());
- }
- }
Output
New folder is getting created inside Document folder at root level.
Step 10: Create List at host web using the following code,
- CreateList = function()
- {
-
- var listCreationInfo = new SP.ListCreationInformation();
- var ListNameVal = $(txtListName).val();
-
- listCreationInfo.set_title(ListNameVal);
- listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
- oList = web.get_lists().add(listCreationInfo);
- clientContext.load(oList);
- oList.set_description(ListNameVal + 'created via JSOM Model ...');
- oList.update();
- clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebCreateListSuccessHandler), Function.createDelegate(this, SPHostWebCreateListErrorHandler));
-
- function SPHostWebCreateListSuccessHandler()
- {
- var result = oList.get_title() + ' List created successfully.';
- alert(result);
- }
-
- function SPHostWebCreateListErrorHandler(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
- }
Output
Finally list is created at host web.
Step 11: Delete list at host web using the following code,
- DeleteList = function()
- {
-
- var DeleteListNameVal = $(txtDeletionListName).val();
- oList = web.get_lists().getByTitle(DeleteListNameVal);
- oList.deleteObject();
- clientContext.executeQueryAsync(Function.createDelegate(this, DeleteListHostWebSucceeded), Function.createDelegate(this, DeleteListHostWebFailed));
-
- function DeleteListHostWebSucceeded()
- {
- alert("Delete List done Successfully ...");
- }
-
- function DeleteListHostWebFailed(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n StackTrace : ' + args.get_stackTrace());
- }
- }
Output
Full Source Code- var clientContext;
- var factory;
- var appContextSite;
- var web;
- var collList;
- var itemCreateInfo;
- var param1, param2, param3;
- var oList, targetList, listFields, oListItem, oField, itemId;
- var ExsitingListNameVal;
- window.Wingtip = window.Wingtip ||
- {}
- Wingtip.JSOMHostWebCollections = function()
- {
- LoadlayoutsJS = function()
- {
-
- hostweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPHostUrl"));
- appweburl = decodeURIComponent(Wingtip.JSOMHostWebCollections.QSParameter("SPAppWebUrl"));
- var Sessionhostweburl = sessionStorage.getItem("hostweburl");
- var Sessionappweburl = sessionStorage.getItem("appweburl");
- if (Sessionhostweburl == null || Sessionappweburl == null)
- {
- sessionStorage.setItem("hostweburl", hostweburl);
- sessionStorage.setItem("appweburl", appweburl);
- }
- if (hostweburl == null || appweburl == null || hostweburl == 'undefined' || appweburl == 'undefined')
- {
- hostweburl = sessionStorage.getItem("hostweburl");
- appweburl = sessionStorage.getItem("appweburl");
- }
- var scriptbase = hostweburl + "/_layouts/15/";
- $.getScript(scriptbase + "SP.Runtime.js", function()
- {
- $.getScript(scriptbase + "SP.js", function()
- {
- $.getScript(scriptbase + "SP.RequestExecutor.js", Wingtip.JSOMHostWebCollections.HWLoadClientContext);
- });
- });
- }
- getQueryStringParameter = function(paramToRetrieve)
- {
-
- try
- {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1)
- {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) return singleParam[1];
- }
- }
- catch (ex)
- {}
- }
- LoadClientContext = function()
- {
-
- clientContext = new SP.ClientContext(appweburl);
- factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
- clientContext.set_webRequestExecutorFactory(factory);
- appContextSite = new SP.AppContextSite(clientContext, hostweburl);
- web = appContextSite.get_web();
- clientContext.load(web);
- Wingtip.JSOMHostWebCollections.HWGetTitle();
-
-
-
- }
- GetTitle = function()
- {
-
- web.set_title('Office365 Host Web');
- web.update();
- clientContext.load(web);
- clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebDetailsSuccessHandler), Function.createDelegate(this, SPHostWebDetailsErrorHandler));
-
- function SPHostWebDetailsSuccessHandler()
- {
- var Result = "";
- Result = Result + "<b>Title :</b> " + web.get_title() + "<br/>";
- Result = Result + "<b> ID :</b> " + web.get_id() + "</br>";
- Result = Result + "<b> Created :</b> " + web.get_created() + "</br>";
- document.getElementById("HostwebDetails").innerHTML = Result;
- }
-
- function SPHostWebDetailsErrorHandler(data, errorCode, errorMessage)
- {
- alert("fail" + errorMessage);
- document.getElementById("HostwebTitle").innerText = "Could not complete cross-domain call: " + errorMessage;
- }
- }
- CreateFolder = function()
- {
-
- oList = web.get_lists().getByTitle("Documents");
- itemCreateInfo = new SP.ListItemCreationInformation();
- itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
- var FolderNameVal = $(txtFolderName).val();
- console.log(FolderNameVal);
- itemCreateInfo.set_leafName(FolderNameVal);
-
- oListItem = oList.addItem(itemCreateInfo);
- oListItem.update();
- clientContext.load(oListItem);
- clientContext.executeQueryAsync(Function.createDelegate(this, CreateFolderSuccessHandler), Function.createDelegate(this, CreateFolderErrorHandler));
-
- function CreateFolderSuccessHandler()
- {
- alert("Go to Site Content ->Documents to see your new folder.");
- }
-
- function CreateFolderErrorHandler()
- {
- alert("Request failed: " + arguments[1].get_message());
- }
- }
- CreateList = function()
- {
-
- var listCreationInfo = new SP.ListCreationInformation();
- var ListNameVal = $(txtListName).val();
-
- listCreationInfo.set_title(ListNameVal);
- listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
- oList = web.get_lists().add(listCreationInfo);
- clientContext.load(oList);
- oList.set_description(ListNameVal + 'created via JSOM Model ...');
- oList.update();
- clientContext.executeQueryAsync(Function.createDelegate(this, SPHostWebCreateListSuccessHandler), Function.createDelegate(this, SPHostWebCreateListErrorHandler));
-
- function SPHostWebCreateListSuccessHandler()
- {
- var result = oList.get_title() + ' List created successfully.';
- alert(result);
- }
-
- function SPHostWebCreateListErrorHandler(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
- }
- DeleteList = function()
- {
-
- var DeleteListNameVal = $(txtDeletionListName).val();
- oList = web.get_lists().getByTitle(DeleteListNameVal);
- oList.deleteObject();
- clientContext.executeQueryAsync(Function.createDelegate(this, DeleteListHostWebSucceeded), Function.createDelegate(this, DeleteListHostWebFailed));
-
- function DeleteListHostWebSucceeded()
- {
- alert("Delete List done Successfully ...");
- }
-
- function DeleteListHostWebFailed(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n StackTrace : ' + args.get_stackTrace());
- }
- }
-
- function Empty()
- {
- console('Hello');
- }
-
- return {
- HWLoadlayoutsJS: LoadlayoutsJS,
- QSParameter: getQueryStringParameter,
- HWLoadClientContext: LoadClientContext,
- HWGetTitle: GetTitle,
- HWCreateFolder: CreateFolder,
- HWCreateList: CreateList,
- HWDeleteList: DeleteList,
- HWEmpty: Empty
- }
- }();
- $(document).ready(function()
- {
-
- Wingtip.JSOMHostWebCollections.HWLoadlayoutsJS();
- });