Page 1 of 1

Multiple lists with 1 lookup

Posted: 13 Jan 2015
by Hubbaroo
We are working on creating a library template that uses the cross site lookup to 1 list. In the end we need to create items in the child list, each item related to the parent list, not a single parent item. The required functionility being the cross site lookup only shows items created from that parent list. I'm struggling with the best logic.

Re: Multiple lists with 1 lookup

Posted: 14 Jan 2015
by Dmitry Kozlov
Hi,

You can create a flag field (Yes/No) that indicates whether an item was created from the parent list and prepopulate it with the appropriate value via JavaScript in the New form. Next, you can modify 'Request items' template of the lookup field to request items filtered by the flag. Does it make sense?

Re: Multiple lists with 1 lookup

Posted: 14 Jan 2015
by Hubbaroo
Almost. After we create several lists from the template we need to filter the lookup based on what list is doing the lookup. So when the item is created in the lookup list it needs to have a column to identify what list created it. When the parent list creates the child record from the "Add new item" link on the lookup what can we get to identify the parent list? In this usage we have a many to one, not a one to many.



Thanks again.

Re: Multiple lists with 1 lookup

Posted: 15 Jan 2015
by Dmitry Kozlov
Hi,

You can get Id of the parent list from the child form via JavaScript following way:

Code: Select all

window.top._spPageContextInfo.pageListId
Next, save it into a hidden field and filter the Cross-site Lookup by this field:

Code: Select all

function (term, page) {
  if (!term || term.length == 0) {
    return "{WebUrl}/_api/web/lists('{ListId}')/items?" + 
		"$select=Id,{LookupField}&" +
		"$orderby=Created desc&" +
		"$top=10&" +
		"$filter=ParentListId eq '" + _spPageContextInfo.pageListId + "'";
  }
  
  return "{WebUrl}/_api/web/lists('{ListId}')/items?" +
	"$select=Id,{LookupField}&" +
	"$orderby={LookupField}&" +
	"$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and ParentListId eq '" + _spPageContextInfo.pageListId + "'&" +
	"$top=10";
}

Re: Multiple lists with 1 lookup

Posted: 15 Jan 2015
by Hubbaroo
This is sweet and will work perfect!!! Better than my getting the url and splitting out the list name to filter on. Thanks again!!!