Introduction

In this article, we will learn how to create a folder in the Sharepoint document library using SharePoint-hosted SharePoint Add-in with Napa Office 365 development tools. Sharepoint's basic operations are used by JavaScript Object Model here. If there are any changes, post your comments here.

Develop the project using the method given below in NAPA tool

Prerequisite

Important things we need to know

Sp.ClientContext

Reference the required libraries

Before using the JSOM, you must reference the following libraries, in exact order.

  1. ASP.NET Ajax library
  2. sp.runtime.js file
  3. sp.js file

Creating, updating, and deleting the lists through Client Object Model works similar to the Server Object Model using .NET Framework (CSOM,SSOM). The operations do not complete until and unless you call the executeQueryAsync(succeededCallback, failedCallback) function.

SharePoint 2013 introduces a NAPA tool service, which is comparable to the existing SharePoint Client Object Model. NAPA Tools can be used to perform Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint.

The add-in that you’ll create includes the controls and the code to manage the lists and list items.

Choose the kind of add-in you want to create, name the project, and then press the "Create" button.

Common steps for all NAPA tool deployment


NAPA is a tool, which you can use to create SharePoint-hosted SharePoint add-ins. We can't create a PHA (Provider Hosted App), using NAPA Tools.

Click "Create" button.


Default .aspx display is like the pic, shown above and App.Js file looks, as shown.

You can add more .js files and add the code to it instead of it to the existing file.


Now, publish your app.


Choose the "Click here to launch your add-in in a new window" link.


Click "Trust it " option to deploy your app here. This action will be followed by a login popup window which will come and ask for your credentials.

Enter your credentials and then click "OK".




In the App.js, declare variables to store the Host Web and App.

  1. var hostWebUrl;
  2. var appWebUrl;
Get the Host Web and App Web Url in document.ready,

  1. hostWebUrl= decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  2. appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
In the above code, SPHostUrl represents the full URL of the host site and SPAppWebUrl represents the full URL of the app web.
  1. <script type="text/javascript">
  2. var hostweburl;
  3. // Load the required SharePoint libraries.
  4. $(document).ready(function () {
  5. // Get the URI decoded URLs.
  6. hostweburl =decodeURIComponent( getQueryStringParameter("SPHostUrl") );
  7. // The js files are in a URL in the form:
  8. // web_url/_layouts/15/resource_file
  9. var scriptbase = hostweburl + "/_layouts/15/";
  10. // Load the js files and continue to
  11. $.getScript(scriptbase + "SP.Runtime.js",
  12. function () {
  13. $.getScript(scriptbase + "SP.js", createFolder);
  14. }
  15. );
  16. });
  17. function createFolder() {
  18. var clientContext;
  19. var _Web;
  20. var _List;
  21. var _itemCreateInfo;
  22. clientContext = new SP.ClientContext.get_current();
  23. _Web= clientContext.get_web();
  24. _List= oWebsite.get_lists().getByTitle("Tutotrialdocuments");
  25. _itemCreateInfo= new SP.ListItemCreationInformation();
  26. _itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
  27. _itemCreateInfo.set_leafName("Customfolder");
  28. this._ListItem = oList.addItem(itemCreateInfo);
  29. this._ListItem.set_item("Title", "Customfolder!");
  30. this._ListItem.update();
  31. clientContext.load(this._ListItem);
  32. clientContext.executeQueryAsync(
  33. Function.createDelegate(this, success),
  34. Function.createDelegate(this, error)
  35. );
  36. function success() {
  37. alert(“New folder Created successfully”);
  38. }
  39. function error(sender,args) {
  40. alert( "Request failed: " + arguments[1].get_message();
  41. }
  42. }
  43. // Function to retrieve a query string value.
  44. // For production purposes you may want to use
  45. // a library to handle the query string.
  46. function getQueryStringParameter(paramToRetrieve) {
  47. var params =
  48. document.URL.split("?")[1].split("&");
  49. var strParams = "";
  50. for (var i = 0; i < params.length; i = i + 1) {
  51. var singleParam = params[i].split("=");
  52. if (singleParam[0] == paramToRetrieve)
  53. return singleParam[1];
  54. }
  55. }
  56. <script>

Once the code is executed, go back to SharePoint site and check if the list operations executed successfully or not.

That's it. Thanks for reading the article.