unable to get property ‘getenumerator’ of undefined or null reference SharePoint

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.

unable to get property 'getenumerator' of undefined or null reference
unable to get property ‘getenumerator’ of undefined or null reference
unable to get property 'getenumerator' of undefined or null reference sharepoint
unable to get property ‘getenumerator’ of undefined or null reference sharepoint

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.

unable to get property 'getenumerator' of undefined or null reference sharepoint
unable to get property ‘getenumerator’ of undefined or null reference sharepoint
unable to get property 'getenumerator' of undefined or null reference sharepoint online
unable to get property ‘getenumerator’ of undefined or null reference sharepoint online
unable to get property 'getenumerator' of undefined or null reference sharepoint 2013
unable to get property ‘getenumerator’ of undefined or null reference sharepoint 2013

You may ECMA script tutorials:

Hope this SharePoint tutorial explains how to solve Unable to get property getenumerator of undefined or null reference in ECMA Scripting.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>