Instantly share code, notes, and snippets.

Embed
What would you like to do?
SharePoint Blogs Feed web part
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BlogsWebPart.ascx.cs" Inherits="Portal.Blogs.WebParts.BlogsWebPart.BlogsWebPart" %>
<SharePoint:CssRegistration ID="CssRegistration1" Name="/_layouts/15/Portal/Blogs/BlogsWebPart.css" runat="server" After="corev15.css" />
<SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="~SiteCollection/_layouts/15/Portal/Blogs/BlogsWebPart.js" Localizable="False"></SharePoint:ScriptLink>
<div id="blogsWebPart" ><img alt="Loading..." src="/_layouts/15/images/loading.gif"/></div>
.blog-pictureBox {
float:left;
width: 79px;
height: 79px;
}
.blog-picture {
background-image: url('/_layouts/15/images/PersonPlaceholder.96x96x32.png');
background-size: cover;
width: 79px;
height: 79px;
}
(function (blogs) {
function getSearchResultsUsingREST(startRow) {
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='ContentTypeId:0x0110*'&selectproperties=" +
"'SPSiteUrl, Title, BodyOWSMTXT, Path, LikesCount, NumComments, PublishedDateOWSDATE, Author'";
var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);
executor.executeAsync({
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: onGetSearchResultsSuccess,
error: onGetSearchResultsFail
});
}
function onGetSearchResultsSuccess(data) {
var jsonObject = JSON.parse(data.body);
var results = jsonObject.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
var searchResultsHtml = '<ul class="ms-blog-postList">';
for (var i = 0; i < results.length; i++) {
var cellResults = results[i].Cells.results;
var spSiteUrl = cellResults[2].Value;
var title = cellResults[3].Value;
var body = cellResults[4].Value;
var path = cellResults[5].Value;
var likesCount = cellResults[6].Value;
var numComments = cellResults[7].Value;
var publishedDate = new Date(cellResults[8].Value);
var author = cellResults[9].Value;
var pictureUrl = spSiteUrl.replace('/personal/', '/User%20Photos/Profile%20Pictures/') + '_MThumb.jpg';
var defaultPicture = '/_layouts/15/images/PersonPlaceholder.96x96x32.png';
searchResultsHtml += '\
<li>\
<div class="ms-blog-postBox ms-shadow">\
<div class="blog-pictureBox" >\
<img class="blog-picture" src="' + pictureUrl + '" onerror="this.src=\'' + defaultPicture + '\';"/>\
<div class="ms-blog-postBoxDate">\
<div class="ms-textSmall">' + publishedDate.getFullYear() + '</div>\
<div class="ms-textXLarge ms-blog-dateText">' +
(publishedDate.getMonth() + 1) + '/' + publishedDate.getDate() + '\
</div>\
</div>\
</div>\
<div class="ms-blog-postBoxMargin">\
<h2><a href="' + path + '">' + title + '</a></h2>\
<div class="ms-metadata ms-textSmall">\
<span>\
by\
<span class="ms-noWrap ms-imnSpan">\
<a class="ms-subtleLink" href="' + spSiteUrl + '">' + author +'</a>\
</span>\
at ' + publishedDate.getHours() + ':' + publishedDate.getMinutes() +'\
</span>\
</div>\
<p></p>\
<div class="ms-blog-postBody">\
<div class="ms-rtestate-field">' + body + '</div>\
<p></p>\
<div>\
<a class="ms-comm-metalineItemSeparator ms-blog-command-noLeftPadding ms-textSmall" href="' + path + '#comments">' +
numComments + ' comments\
</a>\
<span class="ms-blog-command" style="display: inline-block;">\
<span class="ms-noWrap">\
<span class="ms-comm-likesMetadata ms-metadata">\
<span class="ms-comm-likesImgContainer">\
<img src="/_layouts/15/images/LikeFull.11x11x32.png?rev=23">\
</span>\
<span class="ms-comm-likesCount ms-comm-reputationNumbers">' + likesCount + '</span>\
</span>\
</span>\
</span>\
</div>\
</div>\
</div>\
</div>\
<div class="ms-blog-postDivider"></div>\
</li>';
}
searchResultsHtml += '</ul>';
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
m$.ready(function () {
m$('#blogsWebPart').empty();
m$('#blogsWebPart').append(results.length == 0 ? 'No related documents were found' : searchResultsHtml);
});
}, 'mQuery.js');
}
function onGetSearchResultsFail(data, errorCode, errorMessage) {
m$('#blogsWebPart').empty();
m$('#blogsWebPart').append('An error occurred whilst searching for related content - ' + errorMessage);
}
SP.SOD.registerSod('sp.requestexecutor.js', '/_layouts/15/sp.requestexecutor.js');
SP.SOD.executeFunc('sp.requestexecutor.js', 'SP.RequestExecutor', function () {
getSearchResultsUsingREST(0);
});
})(window.blogs = window.blogs || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment