Get certain field from returned JSON object
-
08-02-2021 - |
Question
I am editing a SharePoint List using JS Link.
Within a SharePoint-Column, I would like to display information from another SharePoint column. The source is a SharePoint Person Column (Entliehenan
).
When using the following script:
(function () {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Fields = { 'Status': { 'View': CustomField } };
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function CustomField(ctx){
var ret
if (ctx.CurrentItem.Status) {
ret = "<img style='height: 60px; width: auto;' src='~sites/100037/SiteAssets/Ampel/AmpelRot.png'></img><br><br><span style='background-color: #ffc7ce;'>lent to: " + ctx.CurrentItem.Entliehenbis + " an " + JSON.stringify(ctx.CurrentItem.Entliehenan) + "</span>";
}
else if (!ctx.CurrentItem.Status) {
//ret = "<span style='background-color: #c6efce;'>Kann ausgeliehen werden</span>";
ret ="<img style='height: 60px; width: auto;' src='~sites/100037/SiteAssets/Ampel/AmpelGRUEN.png'></img>";
}
else {
}
return ret;
}
(The responsible part in the script:)
JSON.stringify(ctx.CurrentItem.Entliehenan)
it returns
[{"id":"16","title":"Doe, John","email":"john.doe@jdoe.com","sip":"john.doe@jdeo.com","picture":""}]
Now I would like to only display the Title of the JSON-object ("Doe, John")
.
How to do this?
I have tried to use:
JSON.stringify(ctx.CurrentItem.Entliehenan.title)
and
JSON.stringify(ctx.CurrentItem.Entliehenan[title])
but both return undefined
.
Solution
Please Try using below:
ctx.CurrentItem.Entliehenan[0].title;
There is similar thread on MSDN forum: How to get people picker value using JS Link feature.
Also there is a nice article on Handling field values in JSLink:
Handling field values in JSLink.
Update(to Remove double quotes)::
You can use replace
function in JavaScript as given here:
JSON.stringify(ctx.CurrentItem.Entliehenan[0].title).replace(/['"]+/g, '')