Function

$().SPServices.SPLookupAddNew

Certification

Certified for SharePoint 2007 Works with Caveats with SharePoint 2010
While this function works with SharePoint 2010, because of the modal dialogs the user will get "lost". Alternatively, if you've turned off the modal dialogs, the function works just great.

Functionality

The SPLookupAddNew function allows you to provide a link in forms for Lookup columns so that the user can add new values to the Lookup list easily. It is based on a blog post by Waldek Mastykarzv (see Credits).

How Does It Work?

The function works like this:
  1. Uses GetList for the current list to determine the details for lookupColumn
  2. Uses GetList for the Lookup column's list to determine the list's URL
  3. Uses GetFormCollection to get the URL for the NewItem form for the Lookup column's list
  4. Appends the link to the Lookup columns's formbody table cell using the promptText provided
  5. The link is enclosed in a DIV for easier manipulation with completefunc. The DIV's id takes the form: "SPLookupAddNew_" + lookupColumnStaticName, e.g., if the lookupColumn is 'a b c', then the id="SPLookupAddNew_a_x0020_b_x0020_c"

Prerequisites

  • The form displays a Lookup column.

Syntax

$().SPServices.SPLookupAddNew({	
	lookupColumn: "",
	promptText: "Add new {0}",
	newWindow: false,
	ContentTypeID: "",
	completefunc: null,
	debug: true
});
lookupColumn
The DisplayName of the Lookup column in the form

promptText
The text to display as a link to add a new value to the lookup list. If you include the {0} placeholder, it will be replaced with the value of looukupColumn. The default value is "Add new {0}".

newWindow
If true, the link will open in a new window without passing the Source. The default value is false.

ContentTypeID
If ContentTypeID is specified, it will be passed on the Query String to the NewForm for the Lookup column's list. e.g., "/SiteName/NewForm.aspx?ContentTypeID=0x0100FD8C376B70E78A46974ECF1B10F8D7AD"

completefunc
If specified, the completefunc will be called upon successful completion of the call to SPLookupAddNew. Potential uses for the completefunc: consistent default formatting overrides, additional lookup customizations, image manipulations, etc. You can pass your completefunc in either of these two ways:
completefunc: function() {
  ...do something...
},
or
completefunc: doSomething,                  // Where doSomething is the name of your function
debug
Setting debug: true indicates that you would like to receive messages if anything obvious is wrong with the function call, like using a column name which doesn't exist. I call this debug mode.

Example

$(document).ready(function() {
	$().SPServices.SPLookupAddNew({	
		lookupColumn: "Region Name",
		promptText: "Add new {0} "
	});
});

The prompt in this example will be 'Add new Region Name'. Other prompt examples:
  • 'Add {0}' --> 'Add Region Name'
  • 'Enter a new {0} if you want' --> 'Enter a new Region Name if you want'
  • 'Add one' --> 'Add one' (by not providing the {0} placeholder, the lookupColumn name is not displayed)

Last edited Dec 4, 2011 at 6:50 PM by sympmarc, version 22

Comments

edelwater Jun 8, 2013 at 3:52 PM 
I didnt want the link because (a) users get lost and (b) it resets the selected values as some users have commented above.

So I wrote a little piecs of code that :
a) displays a single lookup box (needs to be in the list)
b) when the user enters in here a value that exists in the multiselect ootb box then the button "add" appears
c) when the user enters in here a value that does not exists yet a button "insert" appears that under the cover inserts that new value in a list

obviously this works only when your reference list has just 1 column that is required such as e.g. a buch of codes or numbers and not when the lookup list has multiple columns that all should be filled in. (in that situation, dynamically add some more fields using jquery under the insert button).

code: http://edward.de.leau.net/sharepoint-lookup-add-insert-field-multi-select-lookup-column-20130608.html

thamera Apr 17, 2013 at 5:14 AM 
This is failing for me on the new docset form. It works everywhere else. I get the debug message stating that the column does not appear to be a lookup (even though it works when I edit an existing item). Anyone seen this before and know a solution? If it matters, I am on SP2013 and this is truely the first issue I have found with SPServices on 2013 and I am using SPServices fairly extensively.

mikeatkins Oct 1, 2012 at 4:37 PM 
The only problem with this is that after the new Item is added, the other field values in the form are reset to nothing. Is there a way around this?

bellsan Sep 27, 2012 at 8:50 AM 
Hello Im new here and I trying to use JQuery to custom my sharepoint portal, but I didnt succed with implementation of this function.
I discrpite whaat I done so far.
jQuery.SPServices-0.7.2 and jQuery.SPServices-0.7.2.min are uploaded to libary, In NewForm.aspx I added HTM Web part Editor to past code that looks like this

<script language="javascript" type="text/javascript" src="http://tsharepoint/bweb/JQuery/jquery.SPServices-0.7.2.js"></script>
<script language="javascript" type="text/javascript" src="http://tsharepoint/bweb/JQuery/jquery.SPServices-0.7.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices.SPArrangeChoices({
lookupColumn: "City",
promptText: "Add new {0} "
});
});
</script>

And still i cant get result , can somone share how to deploy this function

deepali14 Jan 3, 2012 at 8:35 AM 
While using this function with Share Point 2010,I turned off modal dialogs but still user will get "lost". any other solution for this.

sympmarc Mar 18, 2011 at 1:33 PM 
Kevin: If you're willing to customize your forms, you can really do this lots of ways. I built this function to work where the form isn't customized and the person doing the development needs a solid, general solution. Thanks, M.

KevinAnnfield Mar 17, 2011 at 5:32 AM 
My first comment on this solution....firstly everything to do with SPServices is damn fantastic. Thanks so much Marc. Secondly, I have worked out a pretty clean way of doing this one. I'm creating custom New, Disp and Edit forms for SP2010 and needed to add new items to a lookup list. I just added a <div> after the FieldForm control (before the FieldDescription) and included a simple <a href> with the link going to the NewForm for the lookup list (I copied the link from the "add new item" link on main AllItems.aspx page of the lookup list. So that new value is then available in the custom form I turned on Asynchronous updates and the new item appears instantly after creation. Awesome stuff and nice and easy.

KeithHudson Nov 25, 2010 at 2:54 AM 
Marc, this is great! I've been hunting for how to add new lookup items on the fly, on a site where I have no access to the server. This solved my problem, and SOOOO easily.