Create a File in a Document Library in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to create a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

  1. Ensure you have access to the Office 365 online.
  2. Ensure the Napa Tool is available in your site.

Use the following procedure

  1. Create an app for SharePoint using Office 365 Tools. If you have missed out on how to create an app in SharePoint 2013, then Click here.
  2. Create a Docment Library and name it “MyDocumentLibrary”. Refer to my article if you want to learn how to create Document Library in SP2013. Click Here
  3. Click on the Default.aspx page.

    aspx page

  4. Add 2 TextBoxes to capture the file name and file content details respectively.
  5. Add a button to create a file in the Document Library and place it inside the "PlaceHolderMain" tag.
     

    Enter File Name: <input type="text" id="txtFileName" name="txtFileName"></input>

    Enter File Content: <input type="text" id="txtFileContent" name="txtFileContent"/>

    <button id="btnDeleteFolder" onclick="createFile()">Create File</button>
     

  6. Click on the App.js file.

    js file

  7. Globally declare the content, web and list objects as shown below.
     

        var context = SP.ClientContext.get_current(); //gets the current context

        var web = context.get_web(); //gets the web object

        var list = web.get_lists(); //gets the list collection object

        var targetList;

        var fileCreation;

        var fileContent;
     

  8. Now write the function to create a file in a Document Library.
        

        function createFile() {

            targetList = list.getByTitle("MyDocumentLibrary");

            fileCreation = new SP.FileCreationInformation();

            var fileName = document.getElementById("txtFileName").value; //gets the file name

            fileName = fileName + ".txt";

            fileCreation.set_url(fileName);

            fileCreation.set_content(new SP.Base64EncodedByteArray());

            fileContent = document.getElementById("txtFileContent").value;

            //gets the file contents.

            for (var content = 0; content < fileContent.length; content++) {

     

                fileCreation.get_content().append(fileContent.charCodeAt(content)); //add the text content to the file.

            }

            //create a new file instance

            var newFile = targetList.get_rootFolder().get_files().add(fileCreation);

            context.load(newFile);

            context.executeQueryAsync(onFileCreationSuccess, onFileCreationFail);

        }
     

  9. Here in this example we are creating a file in a Document Library.
  10. We are using a “FileCreationInformation” object to create the file in a Document Library.
  11. Set the URL and content attribute using the FileCreationInformation object and then add this object to the Document Library using the add method.
  12. Load the new file object and execute the code by calling executeQueryAsync ().
     

        function onFileCreationSuccess() {

            alert("file Created");

        }

        function onFileCreationFail(sender, args) {

            alert('Failed to create a file. Error:' + args.get_message());

        }
     

  13. That's it!! Now let's start testing.

Testing

  1. Now to run the app click on the "Play" button available towards the left-most corner.

    Play button

  2. The app is packaged, deployed and installed on your Office 365 Site.

    app

  3. Now you will be able to see the following page.

    page

  4. Enter the file name as “MyTextFile” and file content as “This is my custom content added through code.” in the respective text boxes and click on "Create File" button.

    Create File

  5. Now to check the file that was created recently, let's navigate to the following URL.

    Syntax: https://yoursite/DocumentLibraryName
    Example: https://mysite/MyDocumentLibrary
     
    url

  6. You will be able to see the text file that you created recently.
  7. Click on "…" and click on "Edit".

    Edit

  8. Now let's go and check the content that we added.

    content added

Summary

Thus in this article you saw how to create a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.