Multiple lists with 1 lookup

Discussions about Cross-site Lookup
Locked
Hubbaroo
Posts: 60
Joined: Sat Sep 21, 2013

13 Jan 2015

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.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

14 Jan 2015

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?

Hubbaroo
Posts: 60
Joined: Sat Sep 21, 2013

14 Jan 2015

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.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

15 Jan 2015

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";
}

Hubbaroo
Posts: 60
Joined: Sat Sep 21, 2013

15 Jan 2015

This is sweet and will work perfect!!! Better than my getting the url and splitting out the list name to filter on. Thanks again!!!

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests