I have an Angular 2+ application that happily execute calls via REST API to a SharePoint 2013 hosted in a different server. It works quite good but unfortunately for a very specific need (discussion boards) it's not enough. Here what I am doing:
async addMessageREST(callback, siteUrl, discussionBoardName, parentItemId, newMessageBody) {
const requestOptions = {
withCredentials: true,
crossDomain: true
};
var messagePayload = {
// '__metadata': { "type": "SP.Data.DiscussionsListItem" },
'Body': newMessageBody,
'ParentItemID': parentItemId, //unfortunately will be ignored
'FileSystemObjectType': 0,
'ContentTypeId': '0x0107008822E9328717EB48B3B665EE2266388E'
};
// retrieve the contextinfo
let contextinfo = await this._http
.post(
siteUrl + '/_api/contextinfo',
null,
requestOptions
)
.toPromise();
// finally post the request
const requestOptionsAddMessage = {
withCredentials: true,
crossDomain: true,
headers: new HttpHeaders().set('X-RequestDigest', contextinfo["FormDigestValue"]).set("Accept", "application/json;odata=verbose")
};
let request = "/_api/web/Lists/GetByTitle('" + discussionBoardName + "')/items";
let discussionItem = await this._http
.post(
siteUrl + request,
messagePayload,
requestOptionsAllMessages
)
.toPromise();
callback(discussionItem);
}
Technically this works fine, but unfortunately it's not enough because as you may know REST API doesn't offer specific functionalities to manage discussions items. For this JSOM provide quite good methods, but unfortunately I am not able to establish a connection with it (I get a 401 error). Here what I am doing:
addMessageJSOM(callback, siteUrl, discussionBoardName, parentTopicId, newMessageBody) {
let clientContext = new SP.ClientContext(siteUrl);
let list = clientContext.get_web().get_lists().getByTitle(discussionBoardName);
let discussionItem = list.getItemById(parentTopicId);
let properties = {
'Body': newMessageBody
};
let messageItem = SP.Utilities.Utility.createNewDiscussionReply(clientContext, discussionItem);
for (var propName in properties) {
messageItem.set_item(propName, properties[propName])
}
messageItem.update();
clientContext.executeQueryAsync(() => {
callback(messageItem);
}, (error: any) => {
console.log('Request failed', error);
});
}
All works good, until it comes to the executeQueryAsync, and I get the said 401 error. I suppose it's because somehow I am not passing the digest/the options that I am passing via HTTP in the REST Call.
Any idea how I could pass these options via JSOM?
Alternatively, there is a way to call the JSOM endpoint via HTTP without relying on the SP libraries?
Thanks in advance for your reading until here!
question from:
https://stackoverflow.com/questions/65952323/rest-calls-vs-jsom-calls-from-a-sandbox-solution-to-a-sharepoint-2013-site 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…