none
Change Lookup column sort order

    Question

  • I would like to change the default ascending sort order of a Lookup column in a new or edit form to descending.  Can this be set via the API?

    Thanks

    Fred

    Friday, February 24, 2012 4:37 PM

Answers

  • You'll have to play around with the sort function to get it the way you want it, but here is some code to get you started:

    $(function(){
    	var fieldLabel = "LookupField";
    	var lookup = $("h3:contains("+ fieldLabel +")").closest("td").next().find("select");
    	$(lookup).find("option").sort(function(a,b){
    		if(b.innerHTML == 'Test2') return 1;
    		return 0;
    	}).appendTo($(lookup));
    });

    • Marked as answer by Fox4 Friday, March 02, 2012 8:48 AM
    Friday, February 24, 2012 10:23 PM
  • Hi Fox4,

    When you add the lookup column to the list, the look items will be shown as ASC by default. For example, when there are four Lookup columns (“IT”, “FN”,”GN”), they will be shown as the following order: “FN”,”GN”,”IT”. And there is no API to change their order as far as I Know.

    I agree buckenn’s idea. you have to do this using Javascript or JQuery.

    Thanks,
    Wayne Fan
    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact  tnmff@microsoft.com.


    Wayne Fan

    TechNet Community Support

    • Marked as answer by Fox4 Friday, March 02, 2012 8:48 AM
    Friday, March 02, 2012 8:11 AM

All replies

  • You'll have to play around with the sort function to get it the way you want it, but here is some code to get you started:

    $(function(){
    	var fieldLabel = "LookupField";
    	var lookup = $("h3:contains("+ fieldLabel +")").closest("td").next().find("select");
    	$(lookup).find("option").sort(function(a,b){
    		if(b.innerHTML == 'Test2') return 1;
    		return 0;
    	}).appendTo($(lookup));
    });

    • Marked as answer by Fox4 Friday, March 02, 2012 8:48 AM
    Friday, February 24, 2012 10:23 PM
  • Thanks buckenn - I will keep it in mind.  I was hoping for a server side solution to change the behaviour of the dropdown box control.

    Fred

    Monday, February 27, 2012 9:04 AM
  • Hi Fox4,

    When you add the lookup column to the list, the look items will be shown as ASC by default. For example, when there are four Lookup columns (“IT”, “FN”,”GN”), they will be shown as the following order: “FN”,”GN”,”IT”. And there is no API to change their order as far as I Know.

    I agree buckenn’s idea. you have to do this using Javascript or JQuery.

    Thanks,
    Wayne Fan
    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact  tnmff@microsoft.com.


    Wayne Fan

    TechNet Community Support

    • Marked as answer by Fox4 Friday, March 02, 2012 8:48 AM
    Friday, March 02, 2012 8:11 AM
  • Thanks to both of you.  I will mark both as answers as "No" to the API question, and how to implement client side.  I decided to customize the new and edit forms, replacing the lookup field(s) form controls with dropdown boxes.  That way I control the sort and optionally can filter the choices.

    Fred

    Friday, March 02, 2012 8:53 AM
  • Hi, i have quation. i don't know where should i insert this code. can you help me with it?

    Thx

    Friday, January 03, 2014 3:05 PM
  • You'll have to play around with the sort function to get it the way you want it, but here is some code to get you started:

    $(function(){
    	var fieldLabel = "LookupField";
    	var lookup = $("h3:contains("+ fieldLabel +")").closest("td").next().find("select");
    	$(lookup).find("option").sort(function(a,b){
    		if(b.innerHTML == 'Test2') return 1;
    		return 0;
    	}).appendTo($(lookup));
    });

    Hi, i have quation. i don't know where should i insert this code. can you help me with it?

    Thx


    Friday, January 03, 2014 3:09 PM
  • Hi Babak,

    You could enter that code on the New or Edit form for the list.

    In SharePoint 2010, open the New form or Edit form in a new tab, not in a modal dialog (remove "isDlg=1" from the URL if necessary) and click Site Actions -> Edit Page. Then add a content editor web part to the page to contain your script.

    To get the above example to work, you would need to wrap the code in <script></script> tags and add a reference to the jQuery library.

    Friday, January 03, 2014 4:26 PM