This SharePoint tutorial explains, how to resolve Unable to get property getenumerator of undefined or null reference issue in jsom SharePoint 2013/2016/Online.
As part of my project, I need to retrieve or read the lookup field value using ECMA script block in SharePoint 2013/2016/Online. When I try to do it I got the below error message.
I was trying to set the lookup value with the below code:
var strTxtFiled = "";
var ObjLstEnum = this.allItems.getEnumerator();
while(ObjLstEnum.moveNext())
{
var objCrrItem = ObjLstEnum.get_current();
strTxtFiled += objCrrItem.get_item('Title') + '-' +objCrrItem.get_item('CompanyName').Value + '\n';
}
Note: This works for a normal field but for a Lookup Field it will not work.
unable to get property ‘getenumerator’ of undefined or null reference sharepoint
We must have to read the lookup field value with get_lookupValue() method.
The code snippet will be like as below:
strTxtFiled += objCrrItem.get_item('Title') + '-' +objCrrItem.get_item('CompanyName').get_lookupValue() + '\n';
The complete code will be like as below:
function fnGetLkpValue()
{
var objClntContext = new SP.ClientContext.get_current();
var objWeb = objClntContext.get_web();
var ObjLst = objWeb.get_lists().getByTitle("Car Inventory");
var query = SP.CamlQuery.createAllItemsQuery();
allItems =ObjLst.getItems(query);
objClntContext.load(allItems, "Include(Title,CompanyName)");
objClntContext.executeQueryAsync(Function.createDelegate(this, this.fnSccGetVal), Function.createDelegate(this, this.fnFailGetVal));
}
function fnSccGetVal() {
var strTxtFiled = "";
var ObjLstEnum = this.allItems.getEnumerator();
while(ObjLstEnum.moveNext())
{
var objCrrItem = ObjLstEnum.get_current();
strTxtFiled += objCrrItem.get_item('Title') + '-' +objCrrItem.get_item('CompanyName').get_lookupValue() + '\n';
}
alert(strTxtFiled);
}
function fnFailGetVal(sender, args) {
alert("fnFailGetVal. Message:" + args.get_message());
}
Little background/prerequisites to execute this script:
- Create 2 lists
- Primary List
- Car Inventory
- Create lookup field from car inventory list to primary list
With this code operation completed successfully.
You may ECMA script tutorials:
- ECMA Script set lookup column value sharepoint 2013
- Get Current User Details Using JavaScript Object Model (jsom) and jQuery in SharePoint 2013/2016/Online
- Various ECMA List field operations in SharePoint 2013
- SharePoint crud operations using jsom
- Remove DIV tag from Multiline text field using ECMA
- Uncaught TypeError: SP.ClientContext is not a constructor in SharePoint Online JSOM (JavaScript object model)
I hope this SharePoint tutorial explains how to solve Unable to get property getenumerator of undefined or null reference in ECMA Scripting.
I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019