Back to contents.
How to update custom callout content on its opening
Summary
This
article describes my approach to modification of the content inside the custom callout in real time - on every opening.
function CalloutRenderItemTemplate(renderCtx) {
var ret = [];
if (renderCtx.ListSchema.IsDocLib)
ret.push(CalloutRenderFilePreview(renderCtx));
ret.push(CalloutRenderLastModifiedInfo(renderCtx));
ret.push(CalloutRenderSharingStatus(renderCtx));
ret.push(CalloutRenderSourceUrl(renderCtx));
return ret.join('');
}
CalloutRenderItemTemplateCallbacks.SharingStatus.renderSection = false;
CalloutRenderItemTemplateCallbacks.LastModifiedInfo.Before = function(renderCtx)
{
// generate the content for the section based on current rendering context
var newSectionHtml = ‘
This is the content of the new callout section.
’
return newSectionHtml;
}
It’s the simplest code of the callback made for the sake of simplicity of the example. Now let us see the more realistic example. Now we are rendering the data of the field of the list item we render:
CalloutRenderItemTemplateCallbacks.LastModifiedInfo.Before = function(renderCtx)
{
// Create the id for the div inside our section markup where we will place new content.
// We create the dynamic Id based on the item being rendered. We can get the current item from the rendering context.
// Please note that we can get an info on the item being rendered from the supplied rendering context ('renderCtx' parameter)
var id = 'calloutAdditionalInfoSection_' + renderCtx.CurrentItem.ID;
// Let's define the function to render the content of the new section inside the created container div
function fillSection(sender, args){
// Find the container inside the created section
var div = document.getElementById(id);
// Add the content of the item's field via Client Side Object Model (CSOM)
div.textContent = oListItem.get_item('DocLibCalloutPreviewTestColumn');
}
// Load the item data via rendering context and CSOM
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getById(renderCtx.listName);
var oListItem = oList.getItemById(renderCtx.CurrentItem.ID);
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, fillSection));
// return the new section HTML
return 'LastModifiedInfo.Before for file "' + renderCtx.CurrentItem.FileLeafRef + '"';
}
// Hide the out-of-the-box Sharing Info section
CalloutRenderItemTemplateCallbacks.SharingStatus.renderSection = false;
<asp:ScriptManager runat="server"></asp:ScriptManager>
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// The following code gets the client context and Title property by using TokenHelper.
// To access other properties, you may need to request permissions on the host web.
var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];
HostWebUrl = hostWeb;
using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Response.Write(clientContext.Web.Title);
}
}
public string HostWebUrl { get; set; }
}
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/1033/initstrings.debug.js"></script>
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/1033/strings.js"></script>
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/init.debug.js"></script>
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/core.debug.js"></script>
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/mquery.debug.js"></script>
<script type="text/javascript" src="<%=HostWebUrl %>/_layouts/15/callout.debug.js"></script>
<link href="<%=HostWebUrl %>/_layouts/15/1033/styles/Themable/corev15.css" rel="stylesheet" type="text/css"/>