How to set the default value of a lookup field

(this post was written using a SharePoint 2010 environment)

If you are using lookup columns in your SharePoint environment, one thing you may want to do is set the column that appears first, or is the default value in the lookup list when a document is uploaded into a library.

Now this solution is not a no code solution, but it requires very little code and is really straight forward to read and understand…

Step one – prerequisites

My setup to achieve this contained the following:

  • A custom list with each lookup value as a seperate list item (name stored in Title column)
  • A document library with a lookup column looking at the title in the custom list

Step two – configure the edit form

  • Open the list which is using the lookup column, make a note of the display name of the column

NOTE
This caught me out while configuring, I naturally took the field name of the column from the URL string as opposed to the column name displayed in the edit form, make sure you use the latter as the field name won’t work.

  • Navigate to the lookup list where the lookup column is pointed to
  • Open the item in this list that you wish to be the default value
  • Open an item in a new window, in the address bar look for ID= and make a note of the number value as this is used for later
  • Navigate to the libraries edit form (just add /Forms/EditForm.aspx after the name of your list or library)
  • Press Site Actions > Edit Page
  • Add a web part > add a content editor web part to the page
  • Click inside the content editor web part > Under Editing Tools > Format Text press the HTML, Edit HTML Source button

Step three – add the code

  • In the HTML Source window, paste in the following:
<script src="https://code.jquery.com/jquery-1.11.0.min.js"> </script>

<script type="text/javascript">

$(document).ready(function() {

$("select[Title='lookup']").val('1');

});

</script>
  • You will need to change the [Title=’lookup’] to be the name of the lookup column as described earlier
  • You will also need to edit the number after .val to be the ID number of the lookup list item we made a note of earlier
  • Under the Page Tab > Press Stop Editing, make sure to save your changes

Test your changes!

Now when you upload a new document, your lookup column will default to the value we have specified!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s