Introduction
To begin this blog, let's better understand the Sharepoint Rest API and its application in facilitating the Check-In and Check-Out of documents stored in the Document Library for our Custom solutions. In this Blog, we will demonstrate how we can Check-In and Check-Out the Document Stored in the Library of the SharePoint Site using SharePoint Rest API.
What is the SharePoint Rest API?
SharePoint Rest API allows developers to communicate with SharePoint Server using HTTP request and response and allows developers to work with SharePoint Lists, Document Libraries, and sites.
By adjusting the EndPoint used in the SharePoint REST API, we can carry out numerous operations beyond Create, Read, Update, and Delete (CRUD).
Why Do We Need to Check-In and Check-Out the Document?
Firstly checking-in and checking-out documents are necessary to manage the Version History so that if there happens to be a conflict between two users editing the same document at the same time.
When the user has performed the Editing, then the user can Check-Out the document so that others can view the Changes or can modify the Changes.
Overall, checking in and checking out documents in SharePoint helps ensure that edits are managed appropriately, and document versions are properly tracked and maintained.
Let's see how we can Perform the Check-In Procedure for a Document using SharePoint Rest API.
Check-In Document Using Rest API
Firstly we have a SitePages Library in SharePoint Site, and in that, we have a SitePage as Out Document we will be Checking-In that Using the Rest API
Here in the Below Figure, we can see that Contact-Us Page is Currently Checked-Out for the Edit.

Now Let's Build a Rest API to Check this In.
We will be using GetFileByServerRelativeUrl in our Rest API in order to retrieve Our file from a document library based on the file's server-relative URL.
And then, we will use CheckIn() EndPoint to CheckIn the Document.
Remember, while using the CheckIn(), we need to Pass two things in the Parameter i.e.
- Comment- we add while we CheckIn the Document that Represents the Changes we made to the File.
- Checkintype- i.e., which type of Version do we need to CheckIn as we can CheckIn the Minor Version or Major Version of the Document?
If we need to CheckIn the Minor Version, we can set the checkintype = 1 in the Parameter. Else, we can set the checkintype = 2 for Major CheckIn.
So, Our Rest API with EndPoint will look something Like this.
<<your SharePoint Site URL>>/_api/web/getfilebyserverrelativeurl('/sites/AmanSite/SitePages/Contact-Us.aspx')/CheckIn(comment='',checkintype=0)
Note. ServerRelativeURL of your File Differs in your case.
Now in Order to Let this Rest API Call, we will need to Execute a POST Request with the FormDigest Token.
Generally, FormDigest is a hidden form field that contains a unique value that is generated by SharePoint for each session. This value is used to verify that the user making the request has permission to modify the data and that the request is not a forgery or a malicious attack.
In order to Get the FormDigest for the Particular Site, we can Execute the REST API with the ContextInfo EndPoint.
Here is the Example of the FetchRequest for Getting the Form Digest.
fetch(`<<your SharePoint Site URL>>/_api/contextinfo`, {
method: "POST",
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose"
}
}).then(response => response.json())
.then(result => {
console.log("FormDigest",result.d.GetContextWebInformation.FormDigestValue)
});
Now we will Execute the Post Request with this Form Digest and with CheckIn EndPoint in the Rest API using Fetch.





Join the conversation! Your thoughts help the community grow.