3

I have applied a JSLink to the custom list's new form in which I want to get the Current List title to access the list column further.

My code is:

(function () {

    var DepartmentCtx = {};


    DepartmentCtx.Templates = {};
    DepartmentCtx.Templates.Fields = {
        "oiplbDepartment": {
            "NewForm": oiplbDepartment
        }
    };

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

function oiplbDepartment(ctx) {

    var managerField = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);    

    // Here I want to get the current NewForm's list title.
    return "<Label ID='lblDepartmentValue' Style='color:red'>Please Enter Project Owner</Label>";
}

What should I do to get the list title?

2 Answers 2

1

You can get list title in following way:

var _listGuid = managerField.listAttributes.Id;

Or

var _listTitle = managerField.listAttributes.Title;
3
  • listAttributes does not have Title it has only Id and ListTemplateType.
    – users1100
    Commented Nov 25, 2013 at 9:45
  • You can get list title from Id using ECMA script. You can also get list title from var _itemUrl = managerField.itemAttributes.Url; This will return item url and you can extract the name of list from here. Commented Nov 26, 2013 at 12:51
  • var listTitle = ctx.ListTitle; Commented Jul 13, 2020 at 14:48
0

One of the ways of finding List Title in pages like Display form/Edit Form/New Form etc is to use JQuery selectors. The follwing code will give you the title of the list in NewFrom.aspx:

var listName = $(".die a:first").html();

Also, you cannot rely on the Url manipulation as the Url of the list can be different from its title. Attached is the screenshot which shows how to get the List Title using IE developer tools: enter image description here

3
  • In JSLink that would mean that this jQuery would be executed for all list items (if the JSLink is added on a field), not super Commented Dec 9, 2013 at 10:46
  • 1
    The user is asking for a way to get title in NewForm.aspx, so, I think this query will be executed only once in the newform. I have also just started working on JSLink so my understanding may be wrong. Isn't it that the code you provided to get the Id of the list using listAttribute will also run for all items (if the JSLink is added on a field) Commented Dec 9, 2013 at 11:08
  • 1
    It was not me that provided it, but I get what you mean. The difference is that the other answers code is looking at a global variable (that will be set regarding of if we use it or not), while your code will iterate over the DOM (hence performing a quite heavy operation) at least one time. And as said, many many times if not added to the form but a field :) Commented Dec 9, 2013 at 11:11

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.