Sp.Js Error When Running CAML Queries
-
31-12-2020 - |
Question
I have added the below code to a (non-Publishing feature enabled) page in SPO to grab some items from another list and render them out; have done this sort of thing before without an issue (code is being reused from one such instance).
JSLink parts at the top can be ignored - these are rendering the links required fine.
Based on the debug alert() calls I added in, the code is executing at least as far as the listEnum loop inside getCamlPass(), but after that alert is triggered I get the following error in the console:
Have tried recreating the page to no avail, same error. Finding it very hard from the error to determine where the problem lies, but I can't see any errors with my JS code - all the variables have been declared as globals just to avoid any scope problems in the first run.
Code below - any ideas?
<script type="text/javascript">
// Declare global variables
var clientContext;
var oWeb;
var oList;
var allItems;
var camlQuery;
var camlQueryString;
var listEnum;
var currentItem;
var thisCurrentId;
var thisCurrentName;
var markupBlock = "<b>Related Minute Documents</b><br />";
// Load this function AFTER the meetings list so the column is available for editing
(function () {
// Create an object that has the context information about the fields that we want to change the rendering of.
var mlContext = {};
mlContext.Templates = {};
mlContext.Templates.Fields = {
// Apply the new rendering for these fields on List View - note the Static Name must be used
"minutesLink": { "View": mlMarkup }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(mlContext);
})();
// This function provides the rendering logic for the list view
function mlMarkup(ctx) {
// Construct the markup block to pass to the function inserted at the top of the page
var jslMarkupBlock = "<a href='javascript:showMinutes(\"";
jslMarkupBlock += ctx.CurrentItem.meetingTitle;
jslMarkupBlock += "\")';'>Click me</a>";
return jslMarkupBlock;
}
// Async fail for getting the initial list info
function getListFail() {
alert("Failed to get list.");
}
// Async fail for running the CAML query
function getCamlFail() {
alert("Failed to issue CAML query.");
}
// Async success for getting the initial list info
function getListPass(meetingTitle) {
alert("Passed value: "+meetingTitle);
alert("Building CAML query...");
camlQuery = new SP.CamlQuery();
camlQueryString = "<View><Query><Where><Eq><FieldRef Name=\"fkMeetingTitle\" /><Value Type=\"Lookup\">";
camlQueryString += meetingTitle;
camlQueryString += "</Value></Eq></Where></Query></View>";
camlQuery.set_viewXml(camlQueryString);
allItems = oList.getItems(camlQuery);
clientContext.load(allItems);
clientContext.executeQueryAsync(getCamlPass,getCamlFail);
}
// Async success for running the CAML query
function getCamlPass() {
alert("Parsing query result...");
listEnum = allItems.getEnumerator();
while (listEnum.moveNext()) {
alert("Enumerating items...");
currentItem = listEnum.get_current();
thisCurrentId = currentItem.get_item('ID');
thisCurrentName = currentItem.get_item('Name');
markupBlock += thisCurrentName;
markupBlock += "<br />";
document.getElementById('relatedMinutes').innerHTML = markupBlock;
}
}
// Click function for links rendered by JSL
function showMinutes(meetingTitle) {
alert("Meeting: "+meetingTitle);
// Run the initial query to get the list
alert("Opening client context...");
clientContext = SP.ClientContext.get_current();
oWeb = clientContext.get_web();
oList = oWeb.get_lists().getByTitle("Meeting Minutes");
clientContext.load(oList);
clientContext.executeQueryAsync(getListPass(meetingTitle),getListFail);
}
</script>
Solution
I'm guessing your are trying to get items from a library, and you are trying to get the name:
thisCurrentName = currentItem.get_item('Name');
The internal name of that column is not Name
, you could try FileLeafRef