Freigeben über


How to set the default value for a look-up field, for an item as it is being created?

Actually, it may seem to be a very simple task to achieve. Just go to the specific list in the site and then to the custom look up column and then try setting default value...... Actually, there is no way you can set the default value of a look up field (Custom or OOB).

And of course when the SharePoint admin drop to their knees and pull their hair from their heads, then come the SharePoint developers, the saviours of dire straits. Yes, as a developer you can set the default value of a look-up column. The code is very simple, just need to take care of the highlighted part.

<Code>

SPList docs = w.Lists["Documents"];

SPFieldLookup fl = (SPFieldLookup)docs.Fields["City"];

fl.DefaultValue = "1;#Mexico City";

fl.Update();

</Code>

And that’s it, Voila! Another SharePoint admin problem beaten to dust by a SharePoint developer code piece.

Comments

  • Anonymous
    June 09, 2009
    PingBack from http://jointpainreliefs.info/story.php?id=1961

  • Anonymous
    November 09, 2009
    Or just use this handy utility to do the same without any code: http://www.codeplex.com/spstipsUtilityPack

  • Anonymous
    November 21, 2010
    Default Value set in Lookup Table asprunner

  • Anonymous
    November 21, 2010
    Default Value set in Lookup Table asprunner

  • Anonymous
    November 23, 2011
    Where do i have to paste the code snippet, to make it work ? I'm one of the dumb amdins ;)

  • Anonymous
    April 03, 2012
    See here for more info: rrfreeman.blogspot.com/.../sharepoint-lookup-column-defaultvalue.html

  • Anonymous
    June 29, 2013
    I wrote a simple add-on, which inject a "default value" section into "Create Column" dialog. www.sharepointadd-ons.com/sharepoint_default_value

  • Anonymous
    September 22, 2014
    Also, if a Lookup field allows multiple values, then you can set the default by separating each with ';#': fl.DefaultValue = "1;#Mexico City;#2;#Los Angeles";

  • Anonymous
    March 04, 2015
    I'm new to this, but where would you insert this code?