编辑回复消息(评论)到讨论 - SharePoint Online JavaScript

时间:2016-10-12 13:39:40

标签: javascript sharepoint sharepoint-2013 office365

我需要在JavaScript中执行此操作,因为整个解决方案在JavaScript中是最新的,这是最后一部分。

我需要能够更新现有讨论的回复消息(评论)。我可以更改讨论字段,但不能更改消息字段。我知道消息和讨论是两种不同的内容类型,并且回复消息位于讨论文件夹下,但我不知道如何编辑回复消息。 (有一个实用程序可以添加回复消息但不能编辑它)。

这是讨论的一个示例(在列表中),您可以看到有5个回复,我想通过JavaScript更改其中一个回复的正文。

Image of the Discussion Showing Replies I would like to update

例如,我想更改以下信息:

Image of Replies that I want to change the body text

我尝试使用此代码进行更新,但它只会更改讨论而不是消息。

我有一种感觉我需要告诉系统进入该文件夹以查找消息并更改其正文,但我不知道如何做到这一点并且在对互联网进行为期2天的搜索后,我可以&# 39;找到答案。 代码不起作用: function aeditListItem(){

    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getById('40b2fbd4-4f87-d92fb05f8044');  //ID changed to protect client

    this.oListItem = oList.getItemById(getParameterByName('commentid'));

    oListItem.set_item('Body', document.getElementById("ideaDetails").value.replace(/\r?\n/g, '<br />'));

    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );

}

function onQuerySucceeded() {
     alert('Item Updated: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

很多人感谢!

1 个答案:

答案 0 :(得分:0)

显然在线:

this.oListItem = oList.getItemById(getParameterByName('commentid')); 

getParameterByName('commentid')未返回正确的消息ID,请确保指定了消息列表项ID。

作为概念证明,以下示例说明如何:

  • 按身份查找讯息
  • 使用新正文替换消息

实施例

var listTitle = "Discussions";
var oldMessageBody = "";
var newMessageBody  = "";

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(createMessageFindQuery('Body',oldMessageBody));
ctx.load(items);
ctx.executeQueryAsync(
   function(){

      if(items.get_count() == 1){
            var foundItem = items.getItemAtIndex(0);
            foundItem.set_item('Body',newMessageBody);
            foundItem.update();    

            ctx.executeQueryAsync(
              function(){
                  console.log("Updated");   
              },
              function(sender,args){
                  console.log(args.get_message());
              });

      }   
      else
         console.log('Not found or multiple items are found')
   },
   function(sender,args){
       console.log(args.get_message());
   });

});   


function createMessageFindQuery(fieldName,fieldVal){
      var qry = new SP.CamlQuery;
      qry.set_viewXml(String.format('<View Scope="RecursiveAll"><Query><Where><Contains><FieldRef Name="{0}" /><Value Type="Text">{1}</Value></Contains></Where></Query></View>',fieldName,fieldVal));
      return qry;
}
相关问题