最近研究SharePoint 2013的 social feature, 參照微軟MSDN上的例子做一個是使用JS讀取social feed的demo.
其中遇到幾個問題,發出來和博友們共享。
1. 首先是運行到SP.UserProfiles.js的代碼是報錯:
SCRIPT438: Object doesn't support property or method 'get_context'
我debug 了一下,發現報錯的對象是 SocialFeedManager類型的,所以就覆蓋了這個方法
feedManager.get_context = function () { return clientContext };
結果果然沒有報這個錯了,估計是補丁問題,但是我貌似打了所有的補丁,很奇怪,好在問題解決了。
2. 微軟的例子裏面使用雙斜槓來傳遞用戶名
var targetUser = 'domain\\alias';
我自己自做聰明,認爲應該是單斜槓 'domain\alias';, 就遇到了這個錯誤:
hexadecimal value 0x0B, is an invalid character. Line 1, position 1072
這裏問題在於可能微軟在將用戶名拼接成xml的時候沒有轉義單斜槓,導致xml格式出錯,解決辦法是用雙斜槓,這樣就在拼接xml之前轉義了單斜槓。
下面是我跑成功的完整代碼,希望對遇到類似問題的博友有幫助:
<script src="/_layouts/15/SP.UserProfiles.js"></script>
<table width="100%" id="tblPosts"></table><br/>
<button id="btnDelete" type="button"></button><br />
<span id="spanMessage" style="color: #FF0000;"></span>
<script type="text/javascript">
// Replace the placeholder value with the account name of the target user.
var targetUser = 'domain\\alias';
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
function ReadFeedOnLoad() {
// SP.SOD.executeOrDelayUntilScriptLoaded(GetFeeds, 'SP.UserProfiles.js');
SP.SOD.loadMultiple(['SP.Runtime.js', 'sp.js', 'SP.UserProfiles.js'], GetFeeds);
tblPosts = document.getElementById("tblPosts");
}
// Declare global variables.
var clientContext;
var feedManager;
var personalFeed;
var newsFeed;
var timelineFeed;
var targetUserFeed;
function GetFeeds() {
// Initialize the current client context and the SocialFeedManager instance.
clientContext = SP.ClientContext.get_current();
feedManager = new SP.Social.SocialFeedManager(clientContext);
feedManager.get_context = function () { return clientContext };
// Set parameters for the feed content that you want to retrieve.
var feedOptions = new SP.Social.SocialFeedOptions();
feedOptions.set_maxThreadCount(10); // default is 20
// Get all feed types for current user and get the Personal feed
// for the target user.
personalFeed = feedManager.getFeed(SP.Social.SocialFeedType.personal, feedOptions);
newsFeed = feedManager.getFeed(SP.Social.SocialFeedType.news, feedOptions);
targetUserFeed = feedManager.getFeedFor(targetUser, feedOptions);
// Change the sort order to optimize the Timeline feed results.
feedOptions.set_sortOrder(SP.Social.SocialFeedSortOrder.byCreatedTime);
timelineFeed = feedManager.getFeed(SP.Social.SocialFeedType.timeline, feedOptions);
clientContext.load(feedManager);
clientContext.executeQueryAsync(CallIterateFunctionForFeeds, RequestFailed);
}
function CallIterateFunctionForFeeds() {
IterateThroughFeed(personalFeed, "Personal", true);
IterateThroughFeed(newsFeed, "News", true);
IterateThroughFeed(timelineFeed, "Timeline", true);
IterateThroughFeed(targetUserFeed, "Personal", false);
}
function IterateThroughFeed(feed, feedType, isCurrentUser) {
tblPosts.insertRow().insertCell();
var feedHeaderRow = tblPosts.insertRow();
var feedOwner = feedManager.get_owner().get_name();
// Iterate through the array of threads in the feed.
var threads = feed.get_threads();
for (var i = 0; i < threads.length ; i++) {
var thread = threads[i];
var actors = thread.get_actors();
if (i == 0) {
// Get the name of the target user for the feed header row. Users are
// owners of all threads in their Personal feed.
if (!isCurrentUser) {
feedOwner = actors[thread.get_ownerIndex()].get_name();
}
feedHeaderRow.insertCell().innerText = feedType.toUpperCase() + ' FEED FOR '
+ feedOwner.toUpperCase();
}
// Use only Normal-type threads and ignore reference-type threads. (SocialThreadType.Normal = 0)
if (thread.get_threadType() == 0) {
// Get the root post's author, content, and number of replies.
var post = thread.get_rootPost();
var authorName = actors[post.get_authorIndex()].get_name();
var postContent = post.get_text();
var totalReplies = thread.get_totalReplyCount();
var postRow = tblPosts.insertRow();
postRow.insertCell().innerText = authorName + ' posted \"' + postContent
+ '\" (' + totalReplies + ' replies)';
// If there are any replies, iterate through the array and
// get the author and content.
// If a thread contains more than two replies, the server
// returns a thread digest that contains only the two most
// recent replies. To get all replies, call the
// SocialFeedManager.getFullThread method.
if (totalReplies > 0) {
var replies = thread.get_replies();
for (var j = 0; j < replies.length; j++) {
var replyRow = tblPosts.insertRow();
var reply = replies[j];
replyRow.insertCell().innerText = ' - ' + actors[reply.get_authorIndex()].get_name()
+ ' replied \"' + reply.get_text() + '\"';
}
}
}
}
}
function RequestFailed(sender, args) {
$get("spanMessage").innerText = 'Request failed: ' + args.get_message();
}
_spBodyOnLoadFunctionNames.push('ReadFeedOnLoad');
</script>