本随笔讲述如何用JavaScript来读取SharePoint 2013 中blog相关的帖子列表。
ASCX File Content:
<div id="divGetItemsFromPosts" title="读取贴子列表"></div><br /> <div id="divGetItemsFromComments" title="读取某个帖子的所有回贴"></div><br /> <script src="/_layouts/15/Learning_ECMAScript/Learning_WP_Blog.js"></script>
JS File Content(Learning_WP_Blog.js):
1 //读取帖子列表里的前10条帖子 2 //Get top 10 items from Posts List in the blog site 3 var collListItem; 4 var getItemsFromPosts = function (blogSiteUrl) { 5 var clientContext = new SP.ClientContext(blogSiteUrl); 6 var oWebsite = clientContext.get_web(); 7 var oList = oWebsite.get_lists().getByTitle(‘Posts‘); 8 var camlQuery = new SP.CamlQuery(); 9 camlQuery.set_viewXml(‘<View><Query><Where><IsNotNull><FieldRef Name=\‘Title\‘ /></IsNotNull></Where></Query><RowLimit>10</RowLimit></View>‘); 10 collListItem = oList.getItems(camlQuery); 11 clientContext.load(collListItem, ‘Include(Id,Title,Body,Created,Author,NumComments,LikesCount,LikedBy)‘); 12 clientContext.executeQueryAsync(getItemsFromPostsSucceeded, getItemsFromPostsFailed); 13 }; 14 15 var getItemsFromPostsSucceeded = function () { 16 var itemsFromPostsHTML = ‘<strong>---------------Get top 10 items from Posts List in the blog site-------------</strong><br/>‘; 17 var listItemEnumerator = collListItem.getEnumerator(); 18 var oListItem, i = 1, tempCreated; 19 var tempAuthor, tempNumComments, tempLikedBy, tempAuthorArr, tempNumCommentsArr, tempLikedByArr; 20 while (listItemEnumerator.moveNext()) { 21 oListItem = listItemEnumerator.get_current(); 22 tempCreated = oListItem.get_item(‘Created‘); 23 tempCreated = tempCreated.getDate() + ‘/‘ + (tempCreated.getMonth() + 1) + ‘/‘ + tempCreated.getFullYear() + ‘ ‘ + tempCreated.getHours() + ‘:‘ + tempCreated.getMinutes(); 24 25 tempAuthor = oListItem.get_item(‘Author‘); 26 tempNumComments = oListItem.get_item(‘NumComments‘); 27 tempLikedBy = oListItem.get_item(‘LikedBy‘); 28 tempAuthorArr = []; 29 tempNumCommentsArr = []; 30 tempLikedByArr = []; 31 32 for (var property in tempAuthor) { 33 if (typeof tempAuthor[property] != ‘function‘ && property == ‘$2d_1‘) 34 tempAuthorArr.push(tempAuthor[property]); 35 } 36 for (var property in tempNumComments) { 37 if (typeof tempNumComments[property] != ‘function‘ && property == ‘$1E_1‘) 38 tempNumCommentsArr.push(tempNumComments[property]); 39 } 40 if (tempLikedBy) { 41 for (var property in tempLikedBy) { 42 if (typeof tempLikedBy[property] != ‘function‘) 43 tempLikedByArr.push(tempLikedBy[property].get_lookupValue()); 44 } 45 } 46 47 tempNumComments = tempNumCommentsArr.join(‘, ‘); 48 if (tempNumComments != ‘0‘) 49 tempNumComments = ‘ <input type="button" onclick="getItemsFromComments(\‘/Nigel\‘,‘ + oListItem.get_id() + ‘,\‘‘ + oListItem.get_item(‘Title‘) + ‘\‘);" value="‘ + tempNumComments + ‘ Comments" />‘; 50 51 itemsFromPostsHTML += ‘<div style="margin-bottom:20px;background-color:‘ + (i % 2 == 1 ? ‘#ACC8DF‘ : ‘#CCC9BF‘) + ‘"><strong>**Top ‘ + i + ‘</strong><br/>‘ + 52 ‘ID: ‘ + oListItem.get_id() + ‘<br/>‘ + 53 ‘Title: ‘ + oListItem.get_item(‘Title‘) + ‘<br/>‘ + 54 ‘Body: ‘ + oListItem.get_item(‘Body‘) + ‘<br/>‘ + 55 ‘Created: ‘ + tempCreated + ‘<br/>‘ + 56 ‘Author :‘ + tempAuthorArr.join(‘, ‘) + ‘<br/>‘ + 57 ‘NumComments: ‘ + tempNumComments + ‘<br/>‘ + 58 ‘LikesCount: ‘ + (oListItem.get_item(‘LikesCount‘) || 0) + ‘<br/>‘ + 59 ‘LikedBy: ‘ + tempLikedByArr.join(‘, ‘) + ‘</div>‘; 60 i++; 61 } 62 document.getElementById(‘divGetItemsFromPosts‘).innerHTML = itemsFromPostsHTML; 63 }; 64 65 var getItemsFromPostsFailed = function (sender, args) { 66 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 67 }; 68 69 //读取某个帖子的所有回贴 70 //Get items from Comments List in the blog site 71 var collListItem2; 72 var currentPostTitle; 73 var getItemsFromComments = function (blogSiteUrl, postId, postTitle) { 74 var clientContext = new SP.ClientContext(blogSiteUrl); 75 var oWebsite = clientContext.get_web(); 76 var oList = oWebsite.get_lists().getByTitle(‘Comments‘); 77 currentPostTitle = postTitle; 78 var camlQuery = new SP.CamlQuery(); 79 camlQuery.set_viewXml(‘<View><Query><Where><Eq><FieldRef Name=\‘PostID\‘/><Value Type=\‘Lookup\‘>‘ + postId + ‘</Value></Eq></Where></Query><RowLimit>10</RowLimit></View>‘); 80 collListItem2 = oList.getItems(camlQuery); 81 clientContext.load(collListItem2, ‘Include(Id,Title,Body,Created,Author)‘); 82 clientContext.executeQueryAsync(getItemsFromCommentsSucceeded, getItemsFromCommentsFailed); 83 }; 84 85 var getItemsFromCommentsSucceeded = function () { 86 var itemsFromCommentsHTML = ‘<strong>---------------Get items from Comments List in the blog site---------------</strong><br/>‘; 87 itemsFromCommentsHTML += ‘<strong>The Comments of Post (‘+ currentPostTitle +‘)</strong><br/>‘; 88 var listItemEnumerator = collListItem2.getEnumerator(); 89 var oListItem, i = 1, tempCreated; 90 var tempAuthor, tempAuthorArr; 91 while (listItemEnumerator.moveNext()) { 92 oListItem = listItemEnumerator.get_current(); 93 tempCreated = oListItem.get_item(‘Created‘); 94 tempCreated = tempCreated.getDate() + ‘/‘ + (tempCreated.getMonth() + 1) + ‘/‘ + tempCreated.getFullYear() + ‘ ‘ + tempCreated.getHours() + ‘:‘ + tempCreated.getMinutes(); 95 96 tempAuthor = oListItem.get_item(‘Author‘); 97 tempAuthorArr = []; 98 99 for (var property in tempAuthor) { 100 if (typeof tempAuthor[property] != ‘function‘ && property == ‘$2d_1‘) 101 tempAuthorArr.push(tempAuthor[property]); 102 } 103 104 itemsFromCommentsHTML += ‘<div style="margin:0 20px 20px 20px;background-color:‘ + (i % 2 == 1 ? ‘#CFBCCF‘ : ‘#E4E8A4‘) + ‘"><strong>**Top ‘ + i + ‘</strong><br/>‘ + 105 ‘ID: ‘ + oListItem.get_id() + ‘<br/>‘ + 106 ‘Title: ‘ + oListItem.get_item(‘Title‘) + ‘<br/>‘ + 107 ‘Body: ‘ + oListItem.get_item(‘Body‘) + ‘<br/>‘ + 108 ‘Created: ‘ + tempCreated + ‘<br/>‘ + 109 ‘Author :‘ + tempAuthorArr.join(‘, ‘) + ‘</div>‘; 110 i++; 111 } 112 document.getElementById(‘divGetItemsFromComments‘).innerHTML = itemsFromCommentsHTML; 113 }; 114 115 var getItemsFromCommentsFailed = function (sender, args) { 116 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 117 }; 118 119 //初始化 120 //Init 121 _spBodyOnLoadFunctionNames.push(‘documentReady‘); 122 function documentReady() { 123 SP.SOD.executeFunc(‘sp.js‘, ‘SP.ClientContext‘, function () { 124 getItemsFromPosts(‘/Nigel‘); 125 }); 126 }
效果:
时间: 2024-10-23 18:33:12