您的位置:首页 > Web前端 > JavaScript

SharePoint 2013 中使用 JavaScript Like 和Unlike list item/page/document

2013-08-07 18:50 435 查看
SharePoint 2013中新增了很多社交功能,比如用户可以like/unlike 任何一个 list item/page/document,这是一个非常不错的功能。

但有时觉得like/unlike按钮不是很好看,希望用自己的按钮,就需要用JavaScript代码来 Like 和Unlike list item/page/document。

或者在非本级站点客户化显示blog列表等,也需要用JavaScript代码来 Like 和Unlike list item/page/document。

为了实现这个功能,笔者先是尝试直接修改LikesCount 和 LikedBy , 但是这样做不能从 _api/social.feed/my/Feed中看到动态,这样显然不能达到社交的要求,也尝试过用

clientContext = SP.ClientContext.get_current();
feedManager = new SP.Social.SocialFeedManager(clientContext);
feedManager.likePost('6162164E-5542-4530-9E7B-F3EF4A05D562');
clientContext.executeQueryAsync(LikeSucceeded, LikeFailed);


但是问题是likePost里面的参数是Socail Thread ID,一般的列表项是没有的。

后来笔者发现了:

http://msdn.microsoft.com/en-us/library/microsoft.office.server.reputationmodel.reputation.setlike.aspx

再通过摸索,笔者找到下面的正确方案(请确保页面引用了/_layouts/15/reputation.js):

EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
var  aContextObject = new SP.ClientContext();  // 此处如果不是当前网站,需要相对或绝对地址作为参数,如: /blog
Microsoft.Office.Server.ReputationModel.
Reputation.setLike(aContextObject,
listGuid,  // 列表的Guid, 如:EDBA153B-7EBB-4242-B842-FCA6E233E11F
itemIntId, // Item的整数id  例如: 1
false); // Like 为true, unlike 为false
aContextObject.executeQueryAsync(
function () {
// Do something if successful
}, function (sender, args) {
// Do something if error
});
});


例1 like/unlike当前页面

EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
Microsoft.Office.Server.ReputationModel.
Reputation.setLike(aContextObject,
_spPageContextInfo.pageListId.substring(1, 37),
_spPageContextInfo.pageItemId, false);

aContextObject.executeQueryAsync(
function () {
// Do something if successful
}, function (sender, args) {
// Do something if error
});
});


例2 like/unlike blog子网站的某个帖子

EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
var aContextObject = new SP.ClientContext('/blog'); // 初始化一个blog 子站点的上下文
Microsoft.Office.Server.ReputationModel.Reputation.setLike(
aContextObject,
'EDBA153B-7EBB-4242-B842-FCA6E233E11F', //  blog子网站下posts列表的Guid
1, // posts 列表中item的整数id
false);

aContextObject.executeQueryAsync(function () {
alert('like successed');
}, function (sender, args) {
alert('like failed');
});
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: