Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

JSOM Querying lists with values for all lookup objects

$
0
0

I have a list called 'NavList' which includes a lookup column representing a Site Page.
When editing the list, the lookup column is selected from 'Site Pages' column 'PageTitle'.
This value is stored in 'NavList' as 'LinkTarget'
I then include the Page's ID, and this appears in NavList as 'LinkTarget:ID'

Now, when I come to my CEWP and my JSOM code; I need to return all the contents of NavList, but also the included 'LinkTarget:ID' number.
As I understand it, for each individual list item, I must make a second executeQueryAsync to load the lookup item, then pull the 'LinkTarget:ID' field from that object.

So, first question:
Iterating through my primary list, and making individual calls for the additional information on each iteration ... does not intuitively 'feel' like an efficient strategy.
Is it possible at all to do this with a CAML join query ?
I know that you can join two plain lists .... but I am comletely lost when it comes to joining lists to lookup objects.

If a join is not possible, then I guess it's back to plan 'A'.
My code is below.
I call executeQueryAsync to get my navList items.
I then iterate the items.
For each item, I am trying to do a second executeQueryAsync, and here I get lost.
I appear to have correctly got a handle on the lookup object. (var lookupItem)
I then try to executeQueryAsync with a function that 'should' return the 'LinkTarget:ID'.

But as you can see form my code, I am completely confiused as to what parameters to pass in the call, and how to refernce the required objects/values from function onSuccess_lookup.

I have to admit, this is a mess ....
.. if anyone will take pity on me, it would be appreciated !

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js");

var objContext = null;
var objWeb = null;
var objList = null;
var objItem = null;
var curritem = null;

    function MainFunction() {
        objContext = new SP.ClientContext.get_current();
        objWeb = objContext.get_web();
        objList = objWeb.get_lists().getByTitle("NavList");

    	var camlQuery = new SP.CamlQuery();
    	camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="itemParent" Ascending="True"/></OrderBy></Query></View>');
        this.objitems = objList.getItems(camlQuery);
        objContext.load(objitems);
        objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
    }

    function onSuccess(sender, args) {

	// -- we now have the NavList items
	// -- but we still need : lookupobject > LinkTarget:ID

	var _returnParam;
        var itemcount = this.objitems.get_count();
        var itemenum = this.objitems.getEnumerator();

	    -- for each item in list
            while (itemenum.moveNext()) {
                curritem = itemenum.get_current();

		//-- get lookup field details (target page)
		var linkTargetField = curritem.get_item("LinkTarget");
		if(linkTargetField != null)
		{
			var lookupId = linkTargetField.get_lookupId();
			var lookupItem = objList.getItemById(lookupId);

			objContext.load(lookupItem);
			objContext.executeQueryAsync(
				Function.createDelegate(this,function(){ _returnParam = onSuccess_lookup();}),
				Function.createDelegate(this, onFail_lookup)
			);


			// -- test
			alert(_returnParam);
			//--- Do stuff with returned data here
		}

            }
    }

    function onSuccess_lookup(sender, args) {
		return something.get_item("LinkTarget:ID");
    }


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>