2

I am using the jslink code below to display the title of the list in a List View web part as a hyperlink. But the problem is that I don't know how to point to the url of that item? so how can I get the url of that item so that when the user clicks on it it will redirect him to the display page as it normally done by default?

(function () {
    var overrideContext = {};
    overrideContext.Templates = {};
    overrideContext.Templates.Header = "<ul id='local-news2'>"
    overrideContext.Templates.Item = CBody;
    overrideContext.Templates.Footer = "</ul>"
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideContext);
})();

function CBody(ctx) {

    var ret =  "<li>" +
                     "<a href='" + url here + "'>" + ctx.CurrentItem.Title + "</a>" +
                "</li>";
    return ret; }

3 Answers 3

4

Try this. The url will take you to the display form of the item.

(function () {
    var overrideContext = {};
    overrideContext.Templates = {};
    overrideContext.Templates.Header = "<ul id='local-news2'>"
    overrideContext.Templates.Item = CBody;
    overrideContext.Templates.Footer = "</ul>"
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideContext);
})();

function CBody(ctx) {
    var url = String.format('{0}&amp;ID={1}', ctx.displayFormUrl, ctx.CurrentItem.ID);
    var ret =  "<li>" +
                     "<a href='" + url + "'>" + ctx.CurrentItem.Title + "</a>" +
                "</li>";
    return ret; 
}
2

you can format your URL as below

_spPageContextInfo.webAbsoluteUrl+"_layouts/15/start.aspx#/Lists/YOUR_LISTNAME_HERE/DispForm.aspx?ID=ITEM_ID_HERE"
0

In my SharePoint-Site (SP-Online) I am using following (by JSLink connected) script for achieving your demand:

(function () {
    var globalOverrideCtx = {
        Templates: {
            Fields: {
                'LinkToPicture':{
                    'View': GetLinkOfItem
                }
            }
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(globalOverrideCtx);
})();


function GetLinkOfItem(ctx)

{
    var ret

    if (!ctx.CurrentItem.LinkToPicture) {
        ret = "<a href='" + JSON.stringify(ctx.CurrentItem.FileRef).replace(/['"]+/g, '') + "'>" + "Item Link" + "</a>" 


    }

    return ret;
}

You only need to additionally create a SharePoint-Column (I used Text-Column) with the RealFieldName: "LinkToPicture"

My Output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.