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中看到动态,这样显然不能达到社交的要求,也尝试过用
但是问题是likePost里面的参数是Socail Thread ID,一般的列表项是没有的。
后来笔者发现了:
http://msdn.microsoft.com/en-us/library/microsoft.office.server.reputationmodel.reputation.setlike.aspx
再通过摸索,笔者找到下面的正确方案(请确保页面引用了/_layouts/15/reputation.js):
例1 like/unlike当前页面
例2 like/unlike blog子网站的某个帖子
但有时觉得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'); }); });
相关文章推荐
- SharePoint 2013 中使用 JavaScript Like 和Unlike list item/page/document
- Sharepoint ECMAScript对象模型系列 使用ECMA操作列表项对象(listitem)
- SharePoint【ECMAScript对象模型系列】-- 06. 使用ECMA操作列表项对象(list item)
- SharePoint 2013 使用JavaScript对象模型配置智能提示
- SharePoint 2013 使用嵌入JavaScript方式实现一个垂直滚动列表
- [MSDN] 使用 SharePoint 2013 中的 JavaScript 库代码完成基本操作
- 如何在SharePoint的列表中使用通配符来filter出ListItem?
- SharePoint 2013 中使用 delegate control AdditionalPageHead 的注意事项
- SharePoint 2013 Step by Step——使用自定义的List Template
- SharePoint 2013 使用JavaScript对象模型配置智能提示
- SharePoint 2010/2013 使用Javascript 动态添加Ribbon 按钮
- SharePoint 2010/2013 使用Javascript来判断权限的三种方法
- SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
- Sharepoint学习笔记—ECMAScript对象模型系列-- 6、使用ECMA操作列表项对象(list item)
- SharePoint 2013 List 备份使用
- SharePoint 2013/2010 使用JavaScript 将Div转换(render)成 Rich Text Box
- SharePoint 2013 中使用客户化的 (Custom) Filter web part 过滤OOB list view
- SharePoint 2013 Step by Step——使用自定义的List Template
- SharePoint 2013 使用Javascript 为人员控件赋值。(Set peoplepicker value use JS)
- [转载]SharePoint 2013 解决方案中使用JavaScript