SharePoint online office 365 how to create document using Rest API

SharePoint online office 365 how to create document using Rest API

Here we will discuss how we can create a document using Rest API in SharePoint Online Office 365. The sane rest API code will also work in SharePoint 2016 and SharePoint 2013.

Here in this particular example let us take a textbox, multiple text boxes as well as a button. Here user will give a name for the file and the user can put the file content in the multiline textbox. Then the user can submit in the button which will create a text file.

Here we will write both the html code as well as in the JavaScript object model (jsom) inside a script editor web part which will be inside a web part page. The html code will looks like below:

You can also read few useful articles below:
SharePoint 2016 List View Auto Indexing Automatic Index Management

Microsoft Flow Tutorials and Save my email attachments to a SharePoint document library Flow Sample

Create folder inside document library using Rest API in SharePoint Online Office 365

HTML Code:

<div id=”CreateFile”>

<div>

<strong>Enter Document Name:</strong>

<br />

<input type=”text” id=”txtDocumentTitle” />

</div>

<div>

<strong>Enter Document Content:</strong>

<br />

<textarea cols=”20″ id=”txtDocumentContent”></textarea>

</div>

<br />

<input type=”button” id=”btnSubmit” value=”Submit” />

</div>

<div id=”divResults”></div>

Rest API Code:

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

createDocument();

});

}

function createDocument() {

var docTitle = $(“#txtDocumentTitle”).val() + “.txt”;

var docContent = $(“#txtDocumentContent”).val();

var fullUrl = _spPageContextInfo.webAbsoluteUrl+ “/_api/web/GetFolderByServerRelativeUrl(‘Shared Documents’)/Files/add(url='” + docTitle +”‘,overwrite=true)”;

$.ajax({

url: fullUrl,

type: “POST”,

data: docContent,

headers: {

“accept”: “application/json;odata=verbose”,

“content-type”: “application/json;odata=verbose”,

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()

},

success: onQuerySucceeded,

error: onQueryFailed

});

}

function onQuerySucceeded() {

$(“#divResults”).html(“Document successfully created!”);

}

function onQueryFailed() {

alert(‘Error!’);

}

</script>

Once we will Save the page you can see a page like below where user can give a title for the file, then user can put content in the content textbox and then user can click on the submit button which will create a file inside the document library like below:

create document using rest api sharepoint 2013
create document using rest api sharepoint 2013

Now you can check in the document library where you can see the file got created in the document library.

SharePoint online office 365 how to create document using Rest API
SharePoint online office 365 how to create document using Rest API

Here we have discussed how we can create a document using Rest API in SharePoint Online Office 365.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →