本文讲述如何用JavaScript 操作 SharePoint 2013 中blog相关列表。
包括:发贴,读取贴子列表,Like/Unlike 帖子,回复帖子,读取某个帖子的所有回贴。
发贴:
var blogCtx = new SP.ClientContext('/blog'); var postsList = blogCtx.get_web().get_lists().getByTitle('Posts'); var createItem = new SP.ListItemCreationInformation(); var newPost = postsList.addItem(createItem); newPost.set_item('Title', 'Test submit post from parent site/web'); // Post title newPost.set_item('Body', 'Test post body from parent site/web'); // Post body newPost.set_item('PostCategory', 1); // category item id newPost.set_item('PublishedDate', new Date()); // publish date newPost.update(); blogCtx.executeQueryAsync( function () { alert('post added successed'); }, function (sender, args) { alert('Post added failed' + args); } );
读取贴子列表:
var blogCtx = new SP.ClientContext('/blog'); var oWebsite = blogCtx.get_web(); var oList = oWebsite.get_lists().getByTitle("Posts"); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><Query><Where><IsNotNull><FieldRef Name=\'Title\' /></IsNotNull></Where></Query>' + '<RowLimit>10</RowLimit></View>'); var collListItem = oList.getItems(camlQuery); blogCtx.load(collListItem, 'Include(Id,Title,Body,Created,Author,NumComments,LikesCount,LikedBy)'); blogCtx.executeQueryAsync(successHandler, errorHandler); function successHandler() { var listItemEnumerator = collListItem.getEnumerator(); var listItemInfo = ""; while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); listItemInfo += "ID: " + oListItem.get_id() + "<br/>" + "Title: " + oListItem.get_item("Title") + "<br/>" + "Body: " + oListItem.get_item("Body") + "<br/>" + "datetime:" + oListItem.get_item("Created") + "<br/>" + "Author:" + oListItem.get_item("Author") + "NumComments:" + oListItem.get_item("NumComments") + "LikesCount:" + oListItem.get_item("LikesCount") + "LikedBy:" + oListItem.get_item("LikedBy"); } alert(listItemInfo); } function errorHandler() { resultpanel.innerHTML = "Request failed: " + arguments[1].get_message(); }
Like/Unlike 帖子,请参考http://blog.csdn.net/abrahamcheng/article/details/9791433
回复帖子:
var blogCtx = new SP.ClientContext('/blog'); var commentsList = blogCtx.get_web().get_lists().getByTitle('Comments'); var createItem = new SP.ListItemCreationInformation(); var newComment = commentsList.addItem(createItem); var commentBody = "Test comments from parent site"; newComment.set_item('Title',SP.ScriptHelpers.removeHtmlAndTrimStringWithEllipsis(STSHtmlEncode(commentBody),30)); newComment.set_item('Body',commentBody); newComment.set_item('PostTitle',1); newComment.update(); blogCtx.executeQueryAsync( function () { alert('comment added successed'); }, function (sender, args) { alert('comment added failed' + args); } );
读取某个帖子的所有回贴:
var blogCtx = new SP.ClientContext('/blog'); var oWebsite = blogCtx.get_web(); var oList = oWebsite.get_lists().getByTitle("Comments"); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'PostTitle\' LookupId=\'TRUE\' />' + '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + '<RowLimit>10</RowLimit></View>'); var collListItem = oList.getItems(camlQuery); blogCtx.load(collListItem, 'Include(Id,Title,Body,Created,Author)'); blogCtx.executeQueryAsync(successHandler, errorHandler); function successHandler() { var listItemEnumerator = collListItem.getEnumerator(); var listItemInfo = ""; while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); listItemInfo += "ID: " + oListItem.get_id() + "<br/>" + "Title: " + oListItem.get_item("Title") + "<br/>" + "Body: " + oListItem.get_item("Body") + "<br/>" + "datetime:" + oListItem.get_item("Created") + "<br/>" + "Author:" + oListItem.get_item("Author"); } alert(listItemInfo); } function errorHandler() { resultpanel.innerHTML = "Request failed: " + arguments[1].get_message(); }