Created
September 12, 2016 12:13
-
-
Save vgrem/c5327ddbb1ceede884c14e35bb21117e to your computer and use it in GitHub Desktop.
Demonstrates how to create a message (reply) in Discussion Board via SharePoint REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While this appears to work (the reply shows up in the discussion thread of the parent post) it does not set the
ParentItemID
property on the reply, it remainsnull
which breaks some of the features of discussions. See this article: REST api support for discussion list