Hey there, I'm using pnp/nodejs-commonjs with Azure Functions to add list items using a sharepoint Add-in, but I'm a bit stuck on how to catch exceptions because neither pnp-sp nor Microsoft's sharepoint REST API documentations are helpful here, or I haven't been searching very well
I came up with a hacky way on how to get 400 error messages :
async create(listTitle: string, properties: object): Promise<IItemAddResult> {
try {
const item = await sp.web.lists
.getByTitle(listTitle)
.items.add(properties);
return item;
} catch (error) {
const message = this.getErrorMessage(error);
throw new Error(message);
}
}
private getErrorMessage(error: any): string {
const mixedJson = error.message;
const index = mixedJson.indexOf('{');
return JSON.parse(mixedJson.substring(index))['odata.error'].message.value;
}
The error object itself returns this
{
"response": {
"size": 0,
"timeout": 0
},
"status": 400,
"statusText": "Bad Request",
"isHttpRequestError": true
}
and error.message returns this
Error making HttpClient request in queryable [400] Bad Request ::> {"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The property 'Name' does not exist on type 'SP.Data.MattersListItem'. Make sure to only use property names that are defined by the type."}}}
You can easily get all that information in one call. Site Pages is a document library, similar as a list, all items can be requested with the metadata, author is one of the columns.
E.g.
const list = sp.web.lists.getByTitle('Site Pages');
list.items
.select('*,Author/Title,OData__AuthorByline/Title')
.expand('OData__AuthorByline,Author')
.get()
.then(console.log);
Good day, @s-KaiNet . I have been using sp-request to scrape pages within a site collections on SP2013 for a couple years now. I now need to update my node code to work with SPO, app-only permissions. I'm able to authenticate to SPO without issue however, the same technique to grab the HTML of a SP page no longer works.
With SP2013, I could simply pass the full URL and the response would return the HTML. When I try the same with SPO I get an authentication error. Here's the current code:
/*jshint esversion: 6 */
const sprequest = require('sp-request');
let spr = sprequest.create({
siteUrl: 'https://tenant.org/sites/dev',
clientId: '1beab0a4-a10d-4a6b-xxxx-b4c247faa95f',
clientSecret: 'SZvu9Gmbxxxx9fHGeZ+KC9QBxxxx1ApHAlLeNAermug='
});
const url = encodeURI('https://tenant.org/sites/dev');
function peek(url) {
url = url.toLowerCase();
return new Promise(function () {
spr(url)
.then(function (response) {
console.log(response.body);
})
.catch(function (err) {
console.log('Something went wrong.... ' + err);
});
});
}
peek(url);