Last active
April 16, 2016 10:17
-
-
Save dustin-cooler/6ab56e268069dcfb3913 to your computer and use it in GitHub Desktop.
Display Excerpts In Sharepoint 2013 Blogs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2015 Dustin Cooler & Fynydd, LLC | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
Excerpts = { | |
FirstPostID: -1, | |
LastPostID: -1, | |
// Instead of the contents of the Body field, returns a temporary placeholder that we can easily find and replace. | |
// Also finds and stores the lowest and highest post IDs for later use. | |
ReplaceBody: function(ctx) { | |
var id = parseInt(ctx.CurrentItem.ID); | |
if(id < Excerpt.FirstPostID || Excerpt.FirstPostID === -1) { | |
Excerpt.FirstPostID = id; | |
} | |
if(id > Excerpt.LastPostID) { | |
Excerpt.LastPostID = id; | |
} | |
var ret = "<p id='excerpt_placeholder_" + id + "'>Loading...</p><p><a href='" + _spPageContextInfo.webServerRelativeUrl + "/Lists/Posts/Post.aspx?ID=" + id + "'>Read More</a></p>"; | |
return ret; | |
}, | |
// Fetches the excerpts and replaces the placeholder elements with them. | |
ReplaceExcerptPlaceholder: function() { | |
$(document).ready(function() { | |
$.ajax({ | |
url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getByTitle('Posts')/items?$select=ID,Excerpt&$filter=ID le " + Excerpts.FirstPostID + " and ID ge " + Excerpts.LastPostID, | |
type: "GET", | |
headers: { | |
"accept": "application/json;odata=verbose", | |
}, | |
success: function (data) { | |
var res = {}; | |
for (var i = 0; i < data.d.results.length; i++) { | |
res = data.d.results[i]; | |
$('#excerpt_placeholder_' + res.ID).replaceWith(res.Excerpt); | |
} | |
}, | |
error: function (err) { | |
console.log(JSON.stringify(err)); | |
} | |
}); | |
}); | |
} | |
}; | |
// Override the Body View | |
(function () { | |
var override = {}; | |
override.Templates = {}; | |
override.Templates.Fields = { | |
'Body': { | |
'View': Excerpts.ReplaceBody | |
} | |
}; | |
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(override); | |
})(); | |
// Load jQuery if it doesn't already exist | |
(function() { | |
if(typeof jQuery === 'undefined') { | |
var headTag = document.getElementsByTagName("head")[0]; | |
var jqTag = document.createElement('script'); | |
jqTag.type = 'text/javascript'; | |
jqTag.src = '//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js'; | |
jqTag.onload = Excerpts.ReplaceExcerptPlaceholder; | |
headTag.appendChild(jqTag); | |
} else { | |
Excerpts.ReplaceExcerptPlaceholder(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment