lookup filter per view

Discussions about Cross-site Lookup
Locked
Eax
Posts: 32
Joined: Wed Oct 28, 2015

30 Nov 2015

Hello guys,

I have a question : is it possible to get a filtering lookup not by items but by a view?

i have 3 Colums : Reporting Period - Start Date - End Date.

In my custom view, my Start Date is less than or equal at [Today] & End Date is equal or more than at [Today].

In my lookup, i want to show all concerned Reporting Period.

Is it possible?

Best regards,

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

30 Nov 2015

Hi,

No, unfortunatelly it's not possbile. You'll have to replicate the conditions you use in the view in the request query. It's not hard. Here's the basic idea of what you'll need to do:

Code: Select all

function (term, page) {

  if (!term || term.length == 0) {

    return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$filter=(Id ge 2) and (Id lt 5)&$orderby=Created desc&$top=10";

  }

  return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby={LookupField}&$filter=((Id ge 2) and (Id lt 5)) and startswith({LookupField}, '" + encodeURIComponent(term) + "')&$top=10";

} 
Notice the $filter variable. What we've done is added is a condition that tells the query executor to return items with Id value in between 2 (inclusive) and 5 (exclusive).

Check the following link for more information:

https://msdn.microsoft.com/en-us/librar ... 42385.aspx

Eax
Posts: 32
Joined: Wed Oct 28, 2015

30 Nov 2015

Hi,

Thanks

I try it but "Loading Error".

This is my code :

function (term, page) {

if (!term || term.length == 0) {

return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$filter=(Date_x0020_de_x0020_d_x00e9_but ge 'datetime') and (Date_x0020_de_x0020_fin le 'datetime')&$orderby=Created desc&$top=10";

}

return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby={LookupField}&$filter=((Date_x0020_de_x0020_d_x00e9_but ge 'datetime') and (Date_x0020_de_x0020_fin le 'datetime')) and startswith({LookupField}, '" + encodeURIComponent(term) + "')&$top=10";

}



Something wrong ?

Best regards,

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

01 Dec 2015

If you want to use datetime to do your filtering, then the $filter variable would look something like this:

Code: Select all

...$filter=(Date_x0020_de_x0020_d_x00e9_but ge datetime'" + today.toISOString() + "') and...
where today is a JavaScript Date object. You'd be able to create it anew like this:

Code: Select all

var today = new Date();
or retrieve it from a Date/Datetime field like this:

Code: Select all

var today = fd.field('DateTimeField').control('getDate');
More about date objects:

http://www.w3schools.com/jsref/jsref_obj_date.asp

Eax
Posts: 32
Joined: Wed Oct 28, 2015

02 Dec 2015

Thanks,

This is my message error :

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"fr-FR","value":"L'expression \u00ab\u00a0(Date_x0020_de_x0020_d_x00e9_but ge datetime')function toISOString() { [native code]}(Date_x0020_de_x0020_fin le datetime')\u00a0\u00bb n'est pas valide."}}}


And this is my code actually :

function (term, page) {
var today = new Date();

if (!term || term.length == 0) {

return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$filter=(Date%5Fx0020%5Fde%5Fx0020%5Fd%5Fx00e9%5Fbut ge datetime')" + today.toISOString + "(Date%5Fx0020%5Fde%5Fx0020%5Ffin le datetime')&$orderby=Created desc&$top=10";

}

return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby={LookupField}&$filter=((Date%5Fx0020%5Fde%5Fx0020%5Fd%5Fx00e9%5Fbut ge datetime')" + today.toISOString + "(Date%5Fx0020%5Fde%5Fx0020%5Ffin le datetime')) and startswith({LookupField}, '" + encodeURIComponent(term) + "')&$top=10";

}

User avatar
rostislav
Moderator
Posts: 364
Joined: Mon Oct 19, 2015

02 Dec 2015

Use the following code:

Code: Select all

..
if (!term || term.length == 0) {
	return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$filter=(Date_x0020_de_x0020_d_x00e9_but le datetime'" + today.toISOString() + "') and (Date_x0020_de_x0020_fin ge datetime'" + today.toISOString() + "')&$orderby=Created desc&$top=10";
}
	return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby={LookupField}&$filter=(Date_x0020_de_x0020_d_x00e9_but le datetime'" + today.toISOString() + "') and (Date_x0020_de_x0020_fin ge datetime'" + today.toISOString() + "') and startswith({LookupField}, '" + encodeURIComponent(term) + "')&$top=10";
.. 

Eax
Posts: 32
Joined: Wed Oct 28, 2015

02 Dec 2015

Thank you !

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests