Sharepoint API – Nodejs

SharePoint API is a set of RESTful web services in Microsoft SharePoint that provides programmatic access to content, metadata, and functionality stored in a SharePoint site. The API enables developers to interact with SharePoint sites and data in a variety of ways, such as reading, creating, and updating data, managing permissions and access to content, and performing searches.

With the SharePoint API, you can build custom solutions and integrations for SharePoint using your preferred programming language, including .NET, JavaScript, and Python. You can also use the API to integrate SharePoint with other systems and services, such as external data sources, line-of-business applications, and cloud-based services.

The SharePoint API supports both basic authentication and OAuth 2.0 authentication, making it easy to connect and authenticate to SharePoint from various platforms and applications.

To get started with the SharePoint API, you’ll need a SharePoint site and a basic understanding of RESTful web services. You can find more information, including code samples and tutorials, on the Microsoft Developer Network (MSDN) website.

Retrive Access token from Sharepoint

				
					async authenticate() {

        try {
         
            let response=await axios({
                url: {{auth url}},
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                data: this.formUrlEncoded({
                    client_id: {{your client id}},
                    client_secret: {{your client sec}},
                    grant_type: 'client_credentials',
                    resource: {{your resource }},
                })
            });
            if (response.status == 200) {
                return {
                    token:response.data.access_token,
                    _meta:'live'
                };
            } else {
                
                throw new Error("Authentication failed");
            }
        } catch (ex) {
           
            console.log(ex);
            return ex;
        }
    }
				
			

Uploading a file to Sharepoint

				
					//the file is the req.files[0] value 
async uploadFile(file:any, subPath:string,fileName:string) {
        console.log("***************************");
        console.log("***********share point service uploadFile****************");
        console.log("***************************");
        const form = new FormData();
        let token=await this.authenticate();
        let headers = {
            Authorization: "Bearer " + token.token,
            Accept: "application/json;odata=verbose",
            "Content-Type":"application/json;odata=verbose"
        };
        //form.append('file', file.data);
        console.log(headers)
        let requestBody = file.data;
        console.log(requestBody);
        let url={{your domain and folder path}}/_api/Web/GetFolderByServerRelativeUrl('{{sub directory path||}')/Files/add(url='${fileName}',overwrite=true);
        console.log(url);
        try {

            let response = await axios({
                method: 'post',
                url: url,
                headers: headers,
                data: requestBody
            });

            //console.log(response);
            if (response.status == 200) {
                console.log("uploadFile 200");
                console.log(response);
                return response.data;
            } else {
                console.log("uploadFile error");
                throw new Error("Authentication failed");
            }
        } catch (ex) {
            console.log("uploadFile error");
            console.log(ex);
            throw ex;
        }
    }
				
			

If you want to look in to jquery usage of the API can refer to this post by microsoft

Sharepoint API – Nodejs
Scroll to top