使用REST API在SharePoint在线讨论板上发布回复

时间:2016-08-09 06:24:21

标签: rest sharepoint

我试图通过REST API发布有关SharePoint在线讨论板的特定讨论的回复,但无法执行此操作。我不想使用SP.utilities,因为这个REST API将从Android App调用。

以下是我正在实施的代码:

$.ajax({
    url:"../_api/web/Lists/getbytitle(listname)/items?$filter=ParentItemID eq 40",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(itemProperties),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*"

    },
    success: function (data) {
        alert("Successfully posted!!");
    },
    error: function (error) {
    alert("error");
        console.log(JSON.stringify(error));
    }
});

它不是在讨论中创建回复,而是创建一个新的讨论项目。 任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

要在讨论区中创建消息项(回复),需要指定以下属性:

  • FileSystemObjectType,消息项目需要设置为0
  • ContentTypeId - 邮件项目的内容类型ID
  • ParentItemID - 讨论项目(消息容器)id

关于ParentItemID属性

无法通过消息有效内容指定

ParentItemID属性,因为它是只读属性,这意味着以下用于创建消息项的查询失败:

Url  /_api/web/lists/getbytitle('Discussions')/items
Method POST
Data { 
    '__metadata': { "type": "SP.Data.DiscussionsListItem" },
   'Body': "Message text goes here",  
   'FileSystemObjectType': 0, 
   'ContentTypeId': '<MessageContentTypeId>', 
   'ParentItemID': <DiscussionItemId> 
}    

<强>解决方案

以下示例演示如何通过SharePoint REST API在讨论板中创建消息(回复)。

  

用于在讨论项目(文件夹)下创建消息以下内容   使用方法:一旦创建了消息项,它就会被移动   在讨论项目下

var listTitle = "Discussions"; //Discussions Board title
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var messagePayload = {
   '__metadata': { "type": "SP.Data.DiscussionsListItem" },  //set DiscussionBoard entity type name
   'Body': "Message text goes here",  //message Body
   'FileSystemObjectType': 0, //set to 0 to make sure Message Item is created
   'ContentTypeId': '0x0107008822E9328717EB48B3B665EE2266388E', //set Message content type
   'ParentItemID': 123   //set Discussion item (topic) Id
};

createNewDiscussionReply(webUrl,listTitle,messagePayload)
.done(function(item)
{
    console.log('Message(reply) has been sent');
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

,其中

function executeJson(options) 
{
    var headers = options.headers || {};
    var method = options.method || "GET";
    headers["Accept"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: options.url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if("data" in options) {
      ajaxOptions.data = JSON.stringify(options.data);
    }  

    return $.ajax(ajaxOptions);
}


function createListItem(webUrl,listTitle,payload){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
    return executeJson({
        "url" :url,
        "method": 'POST',
        "data": payload
    });
}

function moveListItem(webUrl,listTitle,itemId,folderUrl){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")?$select=FileDirRef,FileRef";
    return executeJson({
        "url" :url
    })  
    .then(function(result){
        var fileUrl = result.d.FileRef;
        var fileDirRef = result.d.FileDirRef;
        var moveFileUrl = fileUrl.replace(fileDirRef,folderUrl);
        var url = webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
        return executeJson({
            "url" :url,
            "method": 'POST'
        });
     });
}


function getParentTopic(webUrl,listTitle,itemId){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getItemById(" + itemId + ")/Folder";
    return executeJson({
        "url" :url,
    });
}


function createNewDiscussionReply(webUrl,listTitle, messagePayload){ 
    var topicUrl = null;
    return getParentTopic(webUrl,listTitle,messagePayload.ParentItemID)
    .then(function(result){
        topicUrl = result.d.ServerRelativeUrl;
        return createListItem(webUrl,listTitle,messagePayload);
    })
    .then(function(result){
        var itemId = result.d.Id;
        return moveListItem(webUrl,listTitle,itemId,topicUrl);
    });
}