Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

angular - REST calls vs. JSOM calls from a sandbox solution to a SharePoint 2013 Site

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

can you change this below line so that parent item id wont be ignored

let request = "/_api/web/Lists/GetByTitle('" + discussionBoardName + "')/items(" + parentItemId + ")" ;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...