0

I have a list with around 1000 columns and I need to be a able to prepopulate another field with a lookupid of another lookup field on the same list

I am getting error when I use:

var LookUpColumnId = oListItem.get_item('LookUpColumn').get_lookupId();

Error:

Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed.

See snippet below:

function onUpdateSucceeded() {

    var itemEnumerator = items.getEnumerator();
    while (itemEnumerator.moveNext()) {
        var item = itemEnumerator.get_current();
        // You now have the item you were looking for!
        //console.log(item.get_item("ID"));
        IDs.push(item.get_item("ID"));
    }
    console.log(IDs);

    var siteUrl = '/sites/Dev/'; 
    var clientContext = new SP.ClientContext(siteUrl);
    oList = clientContext.get_web().get_lists().getByTitle('Test');

    var itemsToLoadForUpdate = Array(); // Holds our "current" items to update
    var totalItemsToUpdate = 900;
    var limitItemsToUpdate = 50; // Update every 90 items

    var batchUpdatesCounter = Math.ceil(totalItemsToUpdate / limitItemsToUpdate); // If you need to count the remaining batch updates
    var restItemsToUpdate = 0; // Items to update after (see below)

    for (var i = 0; i < IDs.length; i++) {
        var oListItem = oList.getItemById(IDs[i]);

        /* Newly addedd get lookup id of items*/

        //var lookupFieldValue = oListItem.get_item('LookUpColumn');
        //var lookupId = lookupFieldValue.get_lookupId();

        //console.log(LookUpColumnId);
        //alert(lookupFieldValue);

        //This works but when i set the lookupId above it doesn't i can;t even get the lookupId 
        oListItem.set_item('LookUpID', 1);

        oListItem.update();
        itemsToLoadForUpdate[i] = oListItem;

        clientContext.load(itemsToLoadForUpdate[i]);
        if (i && i % limitItemsToUpdate == 0) {
            console.log('Updating ' + limitItemsToUpdate + ' items...');
            clientContext.executeQueryAsync(this.onUpdateSucceeded, this.onUpdateFailed);
            restItemsToUpdate = 0;
        }
        else { // Remaining items to update
            restItemsToUpdate = i % limitItemsToUpdate;
        }

    }

    if (restItemsToUpdate) {
        //console.log('Updating rest items...');
        console.log('Updating ' + restItemsToUpdate + ' items...');

        clientContext.executeQueryAsync(this.onUpdateSucceeded, this.onUpdateFailed);
    }
}

Thanks in Advance

New Update code :-

 var camlQuery = new SP.CamlQuery();
    //camlQuery.set_viewXml('<View><Query></Query><RowLimit>900</RowLimit></View>');
    camlQuery.set_viewXml("<View><Query></Query><RowLimit>900</RowLimit><ViewFields><FieldRef Name='LookUpColumn' LookupId='TRUE' /></ViewFields></View>");
    items = oList.getItems(camlQuery);
    //clientContext.load(items);    
    clientContext.load(items,'Include(LookUpColumn)');
    clientContext.executeQueryAsync(onUpdateSucceeded, onUpdateFailed);
0

You have to request the Property of the item with something like clientContext.load(yourItemCollection , 'Include(LookUpColumn)') or for a single item clientContext.load(yourItem, 'LookUpColumn')

Take a look at Complete basic operations using JavaScript library code in SharePoint and How to include custom fields in clientContext.load

  • Thanks for that but i'm still getting the error after adding "clientContext.load(items,'Include(LookUpColumn)');" and changed the query to :- camlQuery.set_viewXml("<View><Query></Query><RowLimit>900</RowLimit><ViewFields><FieldRef Name='LookUpColumn' LookupId='TRUE' /></ViewFields></View>"); – naijacoder Mar 25 at 3:09
  • At which line do you get the error? Try to run the following line after loading the Column.... <code>var lookupValue = oListItem.get_item('LookUpColumn'); What's the result? – mitti Mar 25 at 15:44
  • Thanks.On this line :- var LookUpColumnId = oListItem.get_item('LookUpColumn') – naijacoder Mar 26 at 19:36
  • Please accept the answer if it solved your problem. Thank you. – mitti Mar 27 at 9:19
  • mitti that didn't work. The issue i have is this line var oListItem = oList.getItemById(IDs[i]); How can i get the lookup field using the below when using the oListItem from the above //var lookupFieldValue = oListItem.get_item('LookUpColumn'); //var lookupId = lookupFieldValue.get_lookupId(); Thanks – naijacoder Apr 4 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.