I have written a JSlink for custom display of my announcement web part. JSlink is working fine and i can see the results.

Now my customer wants to place 2 different announcement web parts in one page and want to see the same custom display for both of them.

I know that i can solve this issue either using the list name or the view name.(I google this and found many article about it)

but here is the problem. I don't want to hardcode any value in the JSlink as I want other people from the same site collection (different sub-site under site collection) also use the same jslink for there own Announcement web parts. They can give any name to there announcement list.

kindly help.

here is the code:

(function () {
(window.jQuery || document.write('<script src="../SiteAssets/Javascripts/Announcements/jquery.js"><\/script>'));
function CustomAnnouncementItem(ctx) 
if ((ctx.ListTitle == "General Announcements") || (ctx.ListTitle == "HR Announcements") || (ctx.ListTitle == "Team Announcements") || (ctx.ListTitle == "Announcements"))
    ctx.BaseViewID = 90;    
    var bodyValue = ctx.CurrentItem.Body;
    var _announcementID = ctx.CurrentItem.ID; 
    var regex = /(<([^>]+)>)/ig;
    bodyValue = bodyValue.replace(regex, "");
    var newBodyValue = bodyValue;   
    var ret = "";
    ret += "<div style='Padding:3px;border:1px solid #87CEFA;margin:5px'><b>" + ctx.CurrentItem.Title + "</b><br>"
    if (bodyValue && bodyValue.length >= 100)
        newBodyValue = bodyValue.substring(0, 100) + " <a href='" + ctx.displayFormUrl  + "&ID=" + _announcementID  + "'>Read More...</a>";
        ret += newBodyValue + "</div>";
        ret += newBodyValue + "</div>"
return ret;
    return RenderItemTemplate(ctx);
var overrideAnnouncementCtx = {};
overrideAnnouncementCtx.Templates = {};
overrideAnnouncementCtx.Templates.Header = " ";
overrideAnnouncementCtx.Templates.Footer = " ";
overrideAnnouncementCtx.Templates.Item = CustomAnnouncementItem;
overrideAnnouncementCtx.ListTemplateType = 104;

  • Try to use the current Context in your jslink file. If you can share the code, then we can understand why you have used the list title and then we can suggest an alternative. – Arvi Feb 24 '16 at 1:42
  • The WebPart ID will only change when you remove it and recreate another WebPart but if you remove the WebPart the JSLink connection will be gone anyway – Danny '365CSI' Engelman Feb 24 '16 at 9:49
  • I updated my code. – yogendra Singh Rawat Feb 24 '16 at 10:17
  • You say you want to apply the same rendering to more than one Anouncements list view web part on the same page, and also have the same CSR code file available for Anouncements lists on subsites. Is there the case that there is an Announcements list view web part that should not have the custom rendering on the same page as the ones that should? – Dylan Cristy Feb 26 '16 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.