Last active
April 16, 2016 10:19
-
-
Save JustinGrote/4fd3b5e7bf0420c283a9 to your computer and use it in GitHub Desktop.
Sharepoint CSR JSLink to summarize task notes
This file contains 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
// This is a Sharepoint jsLink file that will render the description field (known internally as Body) | |
// in a Task List view as simply the last line in the multiline field. | |
// Useful for showing only the latest activity on a task summary sheet. | |
// Written 2016-02-02 by Justin Grote <justin@grote.name> | |
(function () { | |
// Create object that have the context information about the field that we want to change it's output render | |
var bodyFieldContext = {}; | |
bodyFieldContext.Templates = {}; | |
bodyFieldContext.Templates.Fields = { | |
// Apply the new rendering for the Recent Status field on list view | |
"Body": { "View": bodyFieldTemplate } | |
}; | |
// Apply the new truncated value | |
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext); | |
//Part 2: Rename Column Title to "Latest Status" | |
function preTaskFormRenderer(renderCtx) { | |
modifyColumns(renderCtx); | |
} | |
function modifyColumns(renderCtx) | |
{ | |
var arrayLength= renderCtx.ListSchema.Field.length; | |
for (var i=0; i < arrayLength;i++) | |
{ | |
if(renderCtx.ListSchema.Field[i].DisplayName == 'Description') | |
{ | |
var newTitle= "Latest Status"; | |
var linkTitleField = renderCtx.ListSchema.Field[i]; | |
linkTitleField.DisplayName = newTitle; | |
} | |
} | |
} | |
function registerRenderer() | |
{ | |
var ctxForm = {}; | |
ctxForm.Templates = {}; | |
ctxForm.OnPreRender = preTaskFormRenderer; | |
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctxForm); | |
} | |
ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js'); | |
})(); | |
// Get the body field and truncate it | |
function bodyFieldTemplate(ctx) { | |
var bodyValue = ctx.CurrentItem["Body"]; | |
//Strip the html tags for a unique title that can be used | |
var regex = /(<([^>]+)>)/ig; | |
var bodyValueTitle = bodyValue.replace(regex, ""); | |
//Convert the rich HTML fragment into a DOM object for easier processing | |
parser = new DOMParser(); | |
var doc = parser.parseFromString(bodyValue, "text/html"); | |
//Get the last line element of the Body field for the last status | |
//This should be in proper HTML so we want to get the last <p> block text | |
//Sometimes Outlook or cut and paste screws this up and converts <p> to something else | |
//so we try <div> and then finally <span> as fallbacks if no <p> is present | |
var bodyElements = doc.querySelectorAll("p"); | |
if (!bodyElements.length) { | |
bodyElements = doc.querySelectorAll("div"); | |
} | |
if (!bodyElements.length) { | |
bodyElements = doc.querySelectorAll("span"); | |
} | |
if (!bodyElements.length) { | |
bodyElements = " "; | |
} | |
//Find the last non-empty element | |
//TODO: Go back and try another element if all elements of the specified type are empty | |
var i = bodyElements.length; | |
var newBodyValue = ""; | |
while (!(/\S/).test(newBodyValue) || i < 0) { | |
i--; | |
try { | |
newBodyValue = bodyElements[i].innerText; | |
if (!newBodyValue) {continue;} | |
} catch (err) { | |
continue; | |
} | |
} | |
//Try to Trim and make the fields unreferenced if anything goes wrong. | |
try { | |
newBodyValue = newBodyValue.trim(); | |
bodyValueTitle = bodyValueTitle.trim(); | |
} | |
catch (err) { | |
newBodyValue = " "; | |
bodyValueTitle = "err-unreferenced"; | |
} | |
return "<span title='" + bodyValueTitle.trim() + "'>" + newBodyValue.trim() + "</span>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment