Introduction to sp-request
The sp-request
module is a powerful tool for making HTTP requests to SharePoint. It simplifies the API call process and provides a straightforward, promise-based interface to handle SharePoint services. This guide will introduce you to the basics of sp-request
and offer examples to demonstrate its usage, ensuring you can get the most out of it.
Authentication
Before making requests to SharePoint, you’ll need to authenticate. Here’s an example of how to set up authentication using sp-request
:
const sprequest = require('sp-request');
const creds = {
username: 'yourusername@domain.com',
password: 'yourpassword'
};
const spr = sprequest.create(creds);
GET Request Example
Here’s an example of making a GET request to retrieve list items:
const listUrl = 'https://yoursharepointsite.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items';
spr.get(listUrl)
.then(response => {
console.log(response.body.d.results);
})
.catch(error => {
console.log(error);
});
POST Request Example
To create a new list item, you can use a POST request like so:
const postUrl = 'https://yoursharepointsite.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items';
const newItem = {
'__metadata': { 'type': 'SP.Data.YourListListItem' },
'Title': 'New Item'
};
spr.post(postUrl, { body: newItem })
.then(response => {
console.log('Item created with ID: ', response.body.d.ID);
})
.catch(error => {
console.log(error);
});
Update Request Example
Updating an existing list item can be done with a MERGE request:
const updateUrl = 'https://yoursharepointsite.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items(1)';
const updatedItem = {
'__metadata': { 'type': 'SP.Data.YourListListItem' },
'Title': 'Updated Item'
};
spr.requestDigest('https://yoursharepointsite.com/sites/yoursite')
.then(digest => {
return spr.request({
url: updateUrl,
method: 'MERGE',
headers: {
'X-RequestDigest': digest,
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: updatedItem
});
})
.then(() => {
console.log('Item updated successfully');
})
.catch(error => {
console.log(error);
});
DELETE Request Example
To delete an existing list item, use a DELETE request:
const deleteUrl = 'https://yoursharepointsite.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items(1)';
spr.requestDigest('https://yoursharepointsite.com/sites/yoursite')
.then(digest => {
return spr.request({
url: deleteUrl,
method: 'DELETE',
headers: {
'X-RequestDigest': digest,
'IF-MATCH': '*'
}
});
})
.then(() => {
console.log('Item deleted successfully');
})
.catch(error => {
console.log(error);
});
Application Example
Here is an example of a small Node.js application using the sp-request
module to interact with a SharePoint list:
const sprequest = require('sp-request');
const creds = {
username: 'yourusername@domain.com',
password: 'yourpassword'
};
const spr = sprequest.create(creds);
const listUrl = 'https://yoursharepointsite.com/sites/yoursite/_api/web/lists/getbytitle('YourList')/items';
const newItem = {
'__metadata': { 'type': 'SP.Data.YourListListItem' },
'Title': 'New Item'
};
async function manageSharePointList() {
try {
// Create new item
const createResponse = await spr.post(listUrl, { body: newItem });
console.log('Item created with ID: ', createResponse.body.d.ID);
// Update item
const updateUrl = `${listUrl}(${createResponse.body.d.ID})`;
const updatedItem = {
'__metadata': { 'type': 'SP.Data.YourListListItem' },
'Title': 'Updated Item'
};
await spr.requestDigest('https://yoursharepointsite.com/sites/yoursite')
.then(digest => {
return spr.request({
url: updateUrl,
method: 'MERGE',
headers: {
'X-RequestDigest': digest,
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: updatedItem
});
});
console.log('Item updated successfully');
// Delete item
await spr.requestDigest('https://yoursharepointsite.com/sites/yoursite')
.then(digest => {
return spr.request({
url: updateUrl,
method: 'DELETE',
headers: {
'X-RequestDigest': digest,
'IF-MATCH': '*'
}
});
});
console.log('Item deleted successfully');
} catch (error) {
console.log(error);
}
}
manageSharePointList();
Hash: 84409e49807b5edc678ec81aa95972ec9fd661c01edca1119d411476236de537