Extending Lookup Fields with “Add new item” option

, , , , ,

SharePoint Lookup Field extended with a "Add new item" link How many times were you working with Lookup Fields and just when you were about to pick a value, you found out that it’s not there. How many clicks did you have to make to get to the Lookup List, add the new Lookup Item, get back to where you were first and add your item? While it sounds trivial, it’s the only way things can be done in out of the box SharePoint. You could of course create a whole new Custom Field Type which would contain an “Add new item” link, but there is an easier way to get this done.

Inspiration is just around the corner

A couple of days ago I attended a Dutch Information Worker User Group (DIWUG) meeting. Two great sessions were scheduled for that evening: “Adding new building blocks to SharePoint” by SharePoint MVP Ton Stegeman and “Best practices Excel Services” by C# MVP Dennis Vroegop. It was Ton’s session that inspired me with solving the Lookup Fields challenge.

First things first: what could you do?

Before we focus on what I’ve done to improve the Lookup Fields experience, let’s a have a look at some standard approaches.

Custom Field Type

Probably the very first thing that comes to your mind, when talking about changing the behavior of SharePoint Field Types, would be to create a Custom Field Type. You would inherit from the SharePoint Lookup Field and extend it with an “Add new item” link. While it sounds simple, there are a couple of issues here that you should keep in mind.

As you know SharePoint ships with Lookup Fields and Multi Lookup Fields which allow you to select multiple values. The problem is, that the underlying Field Types are hard coded so once you change the “Allow Multiple Values” setting, the Field Types revert to the standard SharePoint Field Types: so much for inheritance and a Custom Field Type.

Developing Custom Field Types is not easy especially if you want to do it right. Once provisioned, a Custom Field Type is available on all Web Applications in the Farm. This means that a Custom Field Type should be generic, reusable and not depending on any Web Application/Site Collection specific resources. Additionally, while creating a Custom Field Type you have to consider all the different places a Site Column can be used: all List Forms, Page Layout Controls, Grid View, not mentioning the Document Information Panel which doesn’t support Custom Field Types at all.

Creating a Custom List Iterator

All fields on the List Forms are displayed by a very smart control called List Iterator. What it does, is that it iterates through all available Fields in the given List and renders the display/input controls for every one of them. What you could do is to subclass the List Iterator and extend it with some extra functionality which would add an “Add new item” link to every Lookup Field.

The problem with a Custom List Iterator is that you would have to add it to the List Forms of the given List before you can use it. This means that you wouldn’t be able to use any of the standard SharePoint lists: quite a price to pay for something that is supposed to improve the editing experience.

…so let’s extend the List Iterator with a Control Adapter?

You could try to extend the functionality of a List Iterator with a Control Adapter. Basically you would do the same as in the previous approach but then you would implement the logic in a Control Adapter which would automatically apply it to every List Iterator on your site. Unfortunately, also this approach has a couple of drawbacks.

First of all Control Adapters are not very easy to deploy. A Control Adapter consists of an assembly and a .browser file which has to be deployed to the App_Browsers folder in your Web Application’s IIS directory. As deploying to App_Browsers isn’t supported by SharePoint Solution Packages (WSP), you would have to create a Timer Job to get this done properly.

Another thing that you should consider is that a Control Adapter is scoped to a Web Application. Once provisioned, it is being applied to every instance of the given type in given Web Application unless specifically disabled by that very instance. Not surprisingly 99,9% controls don’t disable Control Adapters what means that you could extend List Forms that you didn’t want to customize in the first place.

What about <SomeOtherApproach>?

The great thing about SharePoint is that you can do almost anything in numerous ways. I’m quite sure there are some other approaches which you could try to get this done. As long as they involve custom development, I’m quite sure they would get too complex very soon. While adding a simple “Add new item” link to a Lookup Field sounds easy, it isn’t – at least unless you get out of the development box.

How I got inspired

In his presentation Ton discussed some customizations he did for a few of his customers. All of those customizations had one thing in common: they could’ve been done using the standard approach that involves custom development. Instead Ton used a combination of JavaScript/jQuery and SharePoint Delegate Controls to get the job done – the easy way.

In one of the cases Ton presented a Lookup Field with an “Add new item” link. In the case, that Ton discussed, the link was attached to a specific Lookup Field. That’s when I got inspired: why not take that concept and use it to create a generic solution that would work for every Lookup Field…

The requirements

While thinking about the solution I’ve defined some requirements.

Support standard Lookup and Multi Lookup Fields

The very first requirement that I defined was that the solution would have to work with the standard SharePoint Lookup Fields: both single and multiple choice. No Custom Field Type should be developed.

"Add new item" link added to both single and multiple choice SharePoint Lookup Fields

Support different Lists

The solution would have to be generic enough to work with every Lookup Field within the given Site/Site Collection. As every Lookup Field can retrieve data from a different List the solution should make it possible to define the target List for the “Add new item” link.

Be end-user friendly

While I could include the “Add new item” link in the solution itself I though that it would be better if it was possible to define the text for every Lookup Field separately. Like this you could add an “Add new Category” link to a Lookup Field that allows you to pick a Category instead of a standard “Add new item” link.

Support Content Types

Depending on your scenario the items in the Lookup List might consist of only the Title, but they might be more complex as well. The solution should support all kinds of content. Additionally if the Lookup List contains items of multiple Content Types the solution should allow you to define which Content Type you want to use while adding new item.

The solution

The solution I came up with was the same one Ton discussed in his presentation: to use a SharePoint Delegate Control which would include some JavaScript to get the job done.

The most challenging part was how to complete all the different requirements while working with nothing more than standard SharePoint Lookup Fields. To get the job done I decided to use the Field’s Description to store all the information required by the “Add new item” link.

Configuring the "Add new item" link in the Field's Description

Adding the “Add new item” link

Before you start extending your Lookup Fields with “Add new item” links, you have to activate the Imtech Extended Lookup Fields Feature. Depending on your scenario you can activate adding the links either for a particular Site or a whole Site Collection. After you activated one of the two Features you can proceed to configuring your Lookup Fields.

In order to provide all the required information to the JavaScript script I came up with the following syntax:

List=ListUrl;Label=Link Text[;CT=Content Type ID]

for example:

List=/Lists/Categories;Label=Add new Category;CT=0x01001234

The List parameter contains a valid URL to a List. The URL of the List New Form (/NewForm.aspx) is being appended to that URL.

The Label parameter contains the text that is being displayed as the “Add new item” link. That text can contain any chars except the ; (semicolon).

The last parameter – CT is optional and allows you to provide the ID of the Content Type that you want to use to add new item to the Lookup List.

And that’s all you really need to extend your Lookup Fields with an “Add new item” link. A working SharePoint Solution Package is available on CodePlex. As the solution doesn’t contain any custom code you can explore the source by changing the package’s extension to CAB and exploring its contents.

Download: Imtech SharePoint Extended Lookup Fields (23KB, WSP)


Possibly related posts

107 Responses to “Extending Lookup Fields with “Add new item” option”

  1. Marc D Anderson Says:

    Waldek:

    This is a very cool idea. I hope you don't mind, but I'm borrowing the idea for our jQuery Library for SharePoint Web Services (http://spservices.codeplex.com). By using the Web Services, we can make this work without having the user provide the List and Label in the Description.

    Our new function SPServices.SPLookupAddNew will be called like this:
    $().SPServices.SPLookupAddNew({
    lookupColumn: "Region Name", // The display name of the Lookup column
    promptText: "Add new " // Text to use as prompt + column name
    });

    M.

  2. Waldek Mastykarz Says:

    @Marc: great to hear it's something you can use :)

  3. Bas Lijten Says:

    Waldek,

    what a nice, simple and clean solution! This inspired me to realise some other things that I have mind.

  4. Explicit Says:

    oh much thanks, Waldek! That's great and easy solution!

  5. Matt Says:

    Is anyone else having problems getting this javascript to work in IE? It works fine in FireFox and Opera but I get nothing in IE8

  6. Waldek Mastykarz Says:

    @Matt: IE8 is the only browser I've tested it with to be honest. Sure you're not having cache problems? Do you see any JavaScript errors?

  7. Matt Ranlett Says:

    I think the problem was the complexity of the selector statement. Your downloadable source includes this line:
    $("#onetIDListForm select[id$=Lookup]").parents("td.ms-formbody").each(function() {

    I had to change that line to this to get it to work:
    $("td.ms-formbody").each(function()

    I'm sure that my selector is less elegant, gets a few more elements and does require more work. But it works on all of the machines and all of the browsers I tested it on.

    I wonder if an element of complexity was that I have three lookup fields on the same form I was testing with? I didn't try having only a single lookup on the page.

  8. Waldek Mastykarz Says:

    @Matt: I'm really surprised you got it working in FF but not in IE. I would suspect something like a difference in selector to depend on some SharePoint Features but not on a browser, especially with a framework like jQuery.
    I have tried it on three fields myself (two single select and one multi select). It worked okay, so the difference should be in the HTML rendered by SharePoint. Different ServicePack perhaps?

  9. Phoenix Says:

    I am trying to use this feature for a multi-select list option scenario, but after activating the feature,I am unable to use the multi-select list option lookup control. Is there a way to fix this? I am using IE-8 in my dev. box.

  10. Waldek Mastykarz Says:

    @Phoenix: do you have any more details about the error you're getting?

  11. Phoenix Says:

    Waldek: I am unable to use the lookup control at all. Once I select the item from left list box, when I click on button to move the item to right listbox, the item doesnt move at all. Is there any code change required?

  12. David J. Schulz Says:

    Hi Waldek, same here. It seems to work fine with single lookup but not multiple. No JavaScript-errors afaik.

  13. Phoenix Says:

    Waldek: Any update on this resolution? I am relying on this option and hence can you pl. let me know if you have a resolution?

  14. Waldek Mastykarz Says:

    @Phoenix: I've managed to solve the issue. It had to do with altering the html source using the jQuery html() function. Here is the new code (remove the var link = '…'; and add instead):

    this.lastChild.data = "";
    var div = document.createElement("div");
    div.setAttribute("style", "padding-top: 5px");
    div.innerHTML = '<img alt="" src="/_layouts/images/setrect.gif" style="vertical-align: baseline"/>  <a href="' + lookupList + '/NewForm.aspx?Source=' + escape(location.href) + '" rel="nofollow">' + label + '</a>';
    this.appendChild(div);

  15. Suzz Says:

    Nice post Waldek !. Is there a way to get your code working for a Document library lookup ? There is this lookupID which is required for upload.aspx – which I am not sure how to achieve using your code.
    Can you please help?

  16. Waldek Mastykarz Says:

    @Suzz: do you mean the URL of the upload.aspx page? The only thing I've noticed there is the List ID and the URL of the RootFolder. Could you explain a bit more what is it that you would like to achieve?

  17. Suzz Says:

    Yes Waldek !. you are correct. I am trying to hook a multiple lookup from a document library within SharePoint list, so that users can upload the document and then further provide SharePoint list data. How do I achieve this scenario? How do I get the List ID?

  18. Waldek Mastykarz Says:

    @Suzz: I've just browsed through the rendered HTML and it seems like there is no variable that points to the list ID. There are some links inside the page that contain the list ID but I'm not sure if it's a reliable way to obtain it.

  19. Elizabeth Says:

    What a fabulous solution! I have a very complex series of related lists and this was just what I needed.

    However, I've run into a snag. It's working fine on a couple of lists, and it was working on a Request list I have, but suddenly it's stopped working for the Request list. I added a custom Display form, but the New and Edit forms are the defaults. I am also using content types. Any ideas as to why this would stop working suddenly? If you can help, I'd really appreciate it as the whole solution is depending on this.

  20. Elizabeth Says:

    I also tried the code above to fix the multiple lookup issue, but now nothing is working. No link, just the code showing as the description of the form.

  21. Elizabeth Says:

    Ok, even odder. It works for some fields on the same form, but not others! I\'ve tried deleting the description and re-adding it. I\'ve copied the exact same description from one field that is working, and copied it to another. It is not making any sense.

  22. Waldek Mastykarz Says:

    @Elizabeth: Have you customized anything about either the Master Page or the List Form? The solution uses a Delegate Control to hook up JavaScript. If the Delegate Control is missing in your Master Page, the control can't get attached. Also, if you have changed anything about the List Form, it might happen, that your HTML is different than default SharePoint HTML and is not being picked up by the JavaScript selector for rewriting.

  23. Marc D Anderson Says:

    Elizabeth:

    Not to "steal"anyone from Waldek, but you could try my jQuery Library for SharePoint Web Services (http://spservices.codeplex.com) solution (first comment above). Folks have been using it with no issues. I've added more options, so it's pretty flexible.

    M.

  24. Elizabeth Says:

    Thanks for the suggestions. Unfortunately, I haven\'t customized anything on the forms or master page so that\'s not it. The odd thing is that it works on some of the fields on the page but not others (all on the same page). And it\'s consistent across both the new and edit pages (the default Microsoft ones).

    The only thing I can think of is that I\'m still testing and I deactivated then reactivated the solution. I think the ones I added before I deactivate/reactivate are the ones not working, and the ones I added afterwards are the ones working.

  25. Waldek Mastykarz Says:

    @Elizabeth: as far as I can tell, the DelegateControl that I'm using shouldn't 'hang'. I think you would just have to do some more debugging as it's quite odd that it works for some fields but it doesn't for other. Perhaps there is something different about the non-working fields as for how the HTML is being rendered?

  26. Elizabeth Says:

    @Waldek: Thanks for the suggestions – I ended up using the JQuery solution Marc suggested and that worked.

  27. Marc D Anderson Says:

    Elizabeth:

    Glad to hear my solution worked for you! (I can\'t tell if you\'re one of the Elizabeths I\'ve been corresponding with.)

    M.

  28. Waldek Mastykarz Says:

    @Elizabeth: Great to hear you got it working.
    @Mark: Thanks for the assistance :D

  29. zerabba Says:

    I have the same problem as Phoenix. Can you tell me where I have to change the code? I need the sources for that? Thanks!

  30. Waldek Mastykarz Says:

    @zerabba: see comment

  31. Tony Caballero Says:

    Hi Waldek,

    I am a novice trying to get to grips with WSS 3.0
    I need the facility for adding addtional items to lookup field and have downladed and deployed the wsp on my site using the stsadm utility.

    I know it is a silly question but, how do I use it?
    Is ther anything I have to do in the site settings to apply your code?
    A step-by-step procedure would be greatly appreciated.

    Thanks in advance!

  32. Waldek Mastykarz Says:

    @Tony Caballero: You said you have deployed the WSP. Have you also activated the Feature in the Site (Collection) Features? The last thing that you need to do is to edit the description of the particular Lookup Field as shown in the images in the article above.

  33. Tony Caballero Says:

    Hi Waldek,

    Thanks for your prompt response (even on a sunday!).
    I did deployed and activated the Feature and it shows in Central Administration.
    However, I did change the Feature name slightly before I deployed it,(renamed to: ExtendedLooupFiedls.wsp).
    Since then I have tried to retract it and this error error shows in Solution Management:

    extendedlookupfields.wsp Error Globally deployed.

    Now when I try to redeploy it again, I am told that the application is already activated.

    Not sure what to do now…

    Thanks

  34. Tony Caballero Says:

    Hi,

    Back again, I have managed to remove the feature (I found an excellent link about using stsadm to remove failed solutions) so I have re-applied again, this time without renaming the Feature. It shows as Active in Site Collection Features.

    I dont understand from the text above where do I make the changes for the LookUp field?

    I have a list named Customers and a List called Support Calls, in Support Calls I have a lookup field that picks the company name automatically from the Customer's list.
    Could you please give me a simple "idiots guide" of where do I make the changes to utilize your Feature.

    If it is too much bother, please say so and I will go away :)

    Thanks in advance.

    Antonio

  35. Waldek Mastykarz Says:

    @Tony: you have to edit the particular Lookup Field and then edit its description as described in the "Adding the “Add new item” link" section in the article above.

  36. Tony Caballero Says:

    Hi,

    If I put any content in the Lookup description field such as:
    List=/Lists/Categories;Label=Add new Category;CT=0×01001234

    This text appears in beneath the Field when I insert a new item.
    I think I am being totally thick here, but I cant figure it out…
    Is there any other examples I can see… please?

  37. chris Says:

    I am seeing the same issue as Tony – I am finding the text below the insert field showing the link and the label under the link.

  38. The Wire Says:

    Great solution! Can you publish the updated version on Codeplex? (Update allowing selections to be added based on http://blog.mastykarz.nl/extending-lookup-fields-add-new-item-option/#comment-33603)
    I'm also wondering if the rest of the data in the form can persist after adding a new lookup item. The entire form is cleared otherwise.
    Thanks!

  39. Elizabeth Says:

    @The Wire: We're having the exact same issue with data persistance. That would be a great addition to this functionality. The clearing of the form is really bothering our users.

  40. GMax Says:

    I found that if my lookup list length < 21 or I use multiply values, all work ok, but if I use single-valued lookup with more than 20 elements I get plain description instead of link

  41. Waldek Mastykarz Says:

    @GMax: this would probably have something to do with the fact that such control has a different markup and you would have to modify the jQuery selector to pick up such a list as well.

  42. GMax Says:

    @Waldek: Yes, I understand it, but I have no knowledge about jQuery and can't make new selector, I try to add "#onetIDListForm text[class=ms-lookuptypeintextbox]" but failed… may be I need 'less elegant' Matt Ranlett's solution

  43. Waldek Mastykarz Says:

    @GMax: you can always try Matt's suggestion and check if it does the job for you.

  44. GMax Says:

    Yes this solution seems to work… while it interfere with Marc D Anderson's SPServices.SPLookupAddNew doubling links.
    pure your code do not give this interference.

    anyway thanks for cool solution :)

  45. Waldek Mastykarz Says:

    @GMax: Great to hear you got it working! :D

  46. Carl Says:

    Ok, so here's my script with the updated function:

    $(function() {
    $("#onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate]").parents("td.ms-formbody").each(function() {
    var fieldWrapper = $(this);
    var text = fieldWrapper.text();
    var regex = /List=([^;]+);Label=([^;]+)(?:;CT=([0-9a-fx]+))?/i;
    var match = regex.exec(text);
    if (match != null) {
    var list = match[1];
    var label = match[2];
    var ct = match[3];
    var href = list + '/NewForm.aspx?Source=' + escape(location.href);
    if (ct != null && ct.length > 0) {
    href += '&ContentTypeId=' + ct;
    }
    var link = this.lastChild.data = "";
    var div = document.createElement("div");
    div.setAttribute("style", "padding-top: 5px");
    div.innerHTML = ' ' + label + '';
    this.appendChild(div);
    fieldWrapper.html(fieldWrapper.html().replace(regex, link));
    }
    })
    });

    Still doesn't work. Under the multipick fields all I see now is: "List=/Lists/Country;Label=Add New Country" instread of the "Add New …" hyperlink.

    There's no errors showing up in Visual Studio so I don't know what's wrong. :(

  47. Carl Says:

    Oh BTW, the first line of the ExtendedLokkupFields.ascx is also correctly pointing to the specific js file under /_layouts/…… so that's not an issue.

  48. GMax Says:

    here is my fix for multipick fields:
    selector line shuld looks like this:
    $("#onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate], #onetIDListForm input[class=ms-lookuptypeintextbox]").parents("td.ms-formbody").each(function() {

  49. Carl Says:

    Great GMax, however, looks like your copy-pasting cut off part of the code.

  50. GMax Says:

    I paste _only_ selector string, change yours(2nd in your post) to my and try it

  51. Carl Says:

    Nope, nothing. Are you using the origianl code or the modified version Waldek suggested on Nov 9, 2009?

  52. Carl Says:

    I've tried all angles. With and with out Waldek's method, with and without your new selector string.

    Can you paste your entire function please?

    Also, you could try Marc Anderson's jQuery method. It works very well, except I need one like this for when using wiki pages.

  53. Carl Says:

    This is what I have, and nothing is showing up under the multipick field.

    $(function() {
    $("#onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate], #onetIDListForm input[class=ms-lookuptypeintextbox]").parents("td.ms-formbody").each(function() {
    var fieldWrapper = $(this);
    var text = fieldWrapper.text();
    var regex = /List=([^;]+);Label=([^;]+)(?:;CT=([0-9a-fx]+))?/i;
    var match = regex.exec(text);
    if (match != null) {
    var list = match[1];
    var label = match[2];
    var ct = match[3];
    var href = list + '/NewForm.aspx?Source=' + escape(location.href);
    if (ct != null && ct.length > 0) {
    href += '&ContentTypeId=' + ct;
    }
    var link = this.lastChild.data = "";
    var div = document.createElement("div");
    div.setAttribute("style", "padding-top: 5px");
    div.innerHTML = '' + label + '';
    this.appendChild(div);
    fieldWrapper.html(fieldWrapper.html().replace(regex, link));
    }
    })
    });

    Carl

  54. GMax Says:

    here is my function
    it is work as well as Marc Anderson;s jQuery variant

    $(function() {
    $("#onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate], #onetIDListForm input[class=ms-lookuptypeintextbox]").parents("td.ms-formbody").each(function() {
    var fieldWrapper = $(this);
    var text = fieldWrapper.text();
    var regex = /List=([^;]+);Label=([^;]+)(?:;CT=([0-9a-fx]+))?/i;
    var match = regex.exec(text);
    if (match != null) {
    var list = match[1];
    var label = match[2];
    var ct = match[3];
    var href = list + '/NewForm.aspx?Source=' + escape(location.href);
    if (ct != null && ct.length > 0) {
    href += '&ContentTypeId=' + ct;
    }
    this.lastChild.data = "";
    var div = document.createElement("div");
    div.setAttribute("style", "padding-top: 5px");
    div.innerHTML = ' ' + label + '';
    this.appendChild(div);
    }
    })
    });

    as far as I look, you do not implement latest changes in innerHTML, as described in above comments…

  55. GMax Says:

    hehe, some of innerHTML lost. I try to repeat it here:

    div.innerHTML = '<img alt="" src="/_layouts/images/setrect.gif" style="vertical-align: baseline"/> <a href="' + list + '/NewForm.aspx?Source=' + escape(location.href) + '" rel="nofollow">' + label + '</a>';

  56. GMax Says:

    hehe, some of innerHTML lost. I try to repeat it here:

    div.innerHTML = \'&lt;img alt=\"\" src=\"/_layouts/images/setrect.gif\" style=\"vertical-align: baseline\"/&gt; &lt;a href=\"\' + list + \'/NewForm.aspx?Source=\' + escape(location.href) + \'\" rel=\"nofollow\"&gt;\' + label + \'&lt;/a&gt;\';

  57. Carl Says:

    Tx GMax. My div.innerHTML was there, but cut off like yours during copy-paste.

    CW

  58. davidtjmarsh Says:

    Have you looked at how you can prevent all the other fields from being cleared after adding a new option to a drop-down? I understand you are just redirecting back to the new form after adding the new option in order for it to reload and get the new value. I can imagine it would be quiet complex to maintain the other field values but I thought I would check to see if you had any suggestions.

  59. Waldek Mastykarz Says:

    @David: I'm not sure this can be done, especially since the standard new form doesn't leverage AJAX out of the box. I guess you would have to use less of the standard components and use AJAX to add a new item in the list and then refresh the drop-down list. Still you would have to be careful not to break the ViewState.

  60. Marc Anderson Says:

    @David: Waldek's conclusion is the same I've arrived at. The only way I can think of to maintain the values is to use a modal window for the lookup list NewForm, which is a chunk of work I have decided not to take on, at least yet.
    This is a common request for SPServices. When I built SPLookupAddNew based on Waldek's post here, I thought it would be a nice little utility function which would be used by admins to set up lookup lists. Turns out lots of people are offering it up to end users as well.

    M.

  61. Marc Haddad Says:

    Hi guys,
    I had the same issues for the lookup field not working in IE and working onlyn in FireFox.
    Note: I am implementing it on SharePoint 2010.
    I did some investigation using FireBug and IE Developer tool. I found that in IE the drop down list is not rendered as an HTML select element; it is rendered as an input and an image elements.
    This behaviour happens only in IE for single value lookup field and when there are a lot of items in the list to look in (in my case 50 items).
    The HTML input field does not have any special naming in its ID, but it has a special class attribute set to 'ms-lookuptypeintextbox'.
    Therefore, I have updated the JQuery selector statememt to select the parent element of the input control having the previous class name. This finally worked for me.

    The selector script is now:
    #onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate], #onetIDListForm input[class$=ms-lookuptypeintextbox]

    I hope it will help who is facing such issues in IE.

    Regards,
    Marc Haddad

  62. Bianca Says:

    Hi,

    thanks Waldek, I like this solution.
    thanks Marc, your solution solved my IE issue too.
    But till now it only works for single value lookup fields. For multi value lookup fields the link is available and it is possible to create a new item. But it is not possible to add one or more of the Items. I click "Add >" ("Hinzufügen >") and nothing happened.
    I'm using SP Foundation (German).
    Do you have any Idea?

    Kind regards,
    Bianca

  63. Marc Haddad Says:

    Hi Bianca,

    In order to use this solution with SharePoint 2010, I did some modifications to the regex expression to use the new dialog box which display the new item form for SharePoint 2010. In addition, it is working for both single and multiple selections of lookup fields.

    I will post all the code here.

    First, in the description of the field, you should fill in the following express:
    Site=/SiteA;List={000-…..00};Label=Add new item

    Note that for the site parameter, you need to put the site where the lookup list is available

    the user control will be the following:

    $(function () {
    $("#onetIDListForm select[id$=Lookup], #onetIDListForm select[id$=SelectCandidate], #onetIDListForm input[class$=ms-lookuptypeintextbox]").parents("td.ms-formbody").each(function () {
    var fieldWrapper = $(this);
    var text = fieldWrapper.text();
    var regex = /Site=([^;]+);List=([^;]+);Label=([^;]+)(?:;CT=([0-9a-fx]+))?/i;
    var match = regex.exec(text);
    if (match != null) {
    var site = match[1]
    var list = match[2];
    var label = match[3];
    var ct = match[4];
    var href = site + '/_layouts/listform.aspx?PageType=8&ListId=' + list + '&RootFolder=';
    if (ct != null && ct.length > 0) {
    href += '&ContentTypeId=' + ct;
    }
    var openScript = "javascript:NewItem2(event, \'" + href + "\');javascript:return false;";
    this.lastChild.data = "";
    var div = document.createElement("div");
    div.setAttribute("style", "padding-top: 5px");
    div.innerHTML = '  ' + label + '';
    this.appendChild(div);
    }
    })
    });

    This solution is working perfectly in both IE and FireFox; I am using it also for single and multiple lookup fields available at the same time in the form.

  64. Bianca Says:

    Hi Marc,

    can you please post this Part again:
    div.innerHTML = ' ' + label + ";

    Thanks a lot!
    Bianca

  65. Marc Haddad Says:

    Hi Bianca,

    It seems that the script was changed when pasting the code on the website.
    the line of code is

    div.innerHTML = '  ' + label + '';

    this.appendChild(div);

    It includes an image, anchor, and label inside the div.

    Regards,
    Marc

  66. Marc Haddad Says:

    Hi Bianca,

    It seems that the script was changed when pasting the code on the website.
    the line of code is

    div.innerHTML = \'<img alt=\"\" src=\"/_layouts/images/rect.gif\" style=\"vertical-align: middle\">&nbsp; <a href=\"javascript:;\" onclick=\"\' + openScript + \'\">\' + label + \'</a>\';

    this.appendChild(div);

    It includes an image, anchor, and label inside the div.

    Regards,
    Marc

  67. Bianca Says:

    Hi Marc,

    many thanks!
    It's working :-).

    Kind regards,
    Bianca

  68. Randy Says:

    Any way the link can open in a new window?

  69. Waldek Mastykarz Says:

    @Randy: While it could be done by modifying the JavaScript responsible for rendering the link and adding the target attribute to the link it would have a serious flaw: after adding a new lookup value it wouldn't automatically appear in the drop down list as the page needs to be refreshed.

  70. XexeX Says:

    There is noting happen after deploy the WSP.
    I've downloaded the WSP, addsolution and deploysolution on WSS3.0, active it in site setting, add a lookup column and fill in the description as below:
    List=/it/Lists/laptop;Label=Add new Model;CT=0x0100E4E81E4D55F39940846D77F321942332
    Is there any wrong? I've tried Chrome, IE8 and FF.
    Thanks.

  71. Waldek Mastykarz Says:

    @XexeX: Have you checked if you have any errors in the JavaScript? Did you do any changes on the New/Edit Form?

  72. XexeX Says:

    @Waldek Mastykarz: How can I check the Javascript error? there is no error pop-up in IE8 and error in status bar.
    Is there any code that I should hardcore to SharePoint?
    I'm not a programmer, there may be somethings missing…
    I don't make any changes on New/Edit Form.

    Thanks.

  73. Waldek Mastykarz Says:

    @XexeX: Debugging JavaScript is easier when using Firefox. You should check the Error Console and see if there are any errors.

  74. XexeX Says:

    Yes, I've found a error "Unknown property "wordwrap"" in NewForm.aspx

  75. Waldek Mastykarz Says:

    @XexeX: Unfortunately, it doesn't ring a bell. Do you know where does this error come from? Additionally, you might try attaching a breakpoint to the JavaScript code and checking if it's failing or not.

  76. XexeX Says:

    @Waldek Mastykarz
    Do I need to insert JavaScript code to use WSP?
    Moreover, I may type the wrong Content Type ID in Description, will the "add new item" show with wrong ID?

  77. Waldek Mastykarz Says:

    @XexeX: Sorry but I don't really understand your question. What do you mean with 'Do I need to insert JavaScript code to use WSP'?
    The Content Type ID is being used to preselect the right Content Type in the New Form but you still should be able to see the link.

  78. XexeX Says:

    @Waldek Mastykarz: I've just enable this add-on in Site Setting, and fill in the syntax in description of lookup field, that's all.
    As I know, some add-on such as jQuery need to insert code in the page.

  79. Waldek Mastykarz Says:

    @XexeX: In this solution the JavaScript code is being automatically added using a Delegate Control so all you need to do is to activate the Feature.

  80. XexeX Says:

    @Waldek Mastykarz: IC, I don't know where's the problem.
    Thanks for your help.

  81. XexeX Says:

    @Waldek Mastykarz: IC, I don\'t know where\'s the problem.
    Thanks for your help.

  82. SPLookupAddNew for SharePoint 2010 « Chaya – Digital Zen Says:

    [...] for the user to follow // which allows them to add a new value to the Lookup list. // Based on http://blog.mastykarz.nl/extending-lookup-fields-add-new-item-option/ // by Waldek Mastykarz // altered for SharePoint 2010 by Emerson Bruce [...]

  83. Marc Shapiro Says:

    Extending Lookup Fields with “Add new item” option
    Windows 2003 Enterprise Server SP2
    WSS v3
    When the "Allow Muliple Items" is selected you cannot "Add" or "Remove" any of the times.
    Recommendation?

  84. Waldek Mastykarz Says:

    @Marc: what do you mean with 'Remove' is not available? What are you referring to?

  85. Marc Shapiro Says:

    Thank you for the replay. On a New or Edit form (not custimized), with "Multi Look Field", your example http://mastykarz.nl/blog/images/ExtendingLookupFieldswithAddnewitemoptio_6981/SingleMultipleLookupFields_thumb.pngthe "Add and Remove" function does not execute.

  86. Marc Shapiro Says:

    Thank you for the replay. On a New or Edit form (not custimized), with \"Multi Look Field\", your example http://mastykarz.nl/blog/images/ExtendingLookupFieldswithAddnewitemoptio_6981/SingleMultipleLookupFields_thumb.pngthe \"Add and Remove\" function does not execute.

  87. Marc Shapiro Says:

    Sorry wrong link to the png.

    http://mastykarz.nl/blog/images/ExtendingLookupFieldswithAddnewitemoptio_6981/SingleMultipleLookupFields.png

  88. Marc Anderson Says:

    Marc:

    I borrowed Waldek's idea and expanded upon it for SPServices. The SPLookupAddNew function ought to give you the functionality you need if you want to try it out.

    M.

  89. DNS Lookup Says:

    The problem with a Custom List Iterator is that you would have to add it to the List Forms of the given List before you can use it.
    _____________________
    Kim

  90. Zarko Says:

    Hi, tnx for this great solution. I have a problem to implement it on one of my sites. The strange thing is that it is just one site, it works ok on others. Does anybody have any idea what could cause this? Thanks in advance.

  91. Waldek Mastykarz Says:

    @Zarko: Could you tell something more about the problems you're having?

  92. Zarko Says:

    It is probably due to a template I am using for this particular site. Even lookup field drop down looks different, I see the description as it was entered(I see "List=/Lists/ListName;Label=…"). If you have any clue what could cause this kind of behaviour please let me know. Many thanks in advance

  93. Waldek Mastykarz Says:

    @Zarko: Most common reasons of this not working is a JavaScript somewhere on the page or a custom Master Page that uses different HTML and therefore requires modifying the selectors.

  94. Renee Says:

    Hello – how can you do something similar with SharePoint 2010 list form customized with InfoPath 2010? Is it possible?

    Thank you very much!

  95. Waldek Mastykarz Says:

    @Renee: While you could add some JavaScript on the page, if I'm not wrong InfoPath forms are loaded in an iframe and most browsers do not allow to modify contents of an iframe. I guess you would have to check whether it's possible in your scenario (ie. os + browser + form).

  96. russell Says:

    I created a feature in 2010 to create a link to the newform.aspx and then after the user closes the moodal dialog, the item the user just added is selected in the dropdon box on the 'parent' form. I posted the code at
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010customization/thread/bb8e0783-1ba9-4d1e-8e2a-bd96813279de

  97. Deepali Says:

    @ Marc Haddad : I tried your code , but its not working in my sharepoint2010 site. Description text appears in beneath the Field as it is.
    Steps I did:
    upload Jquery version 1.6 to layout folder
    Modify .ascx file with your code and J query path.
    Not sure what I did wrong.

  98. Deepali Says:

    its not working for sharepoint2010 multivalue lookup column…any solution for sharepoint2010 multivalue lookup with add new item functionality?

  99. Waldek Mastykarz Says:

    @Deepali: Mavention offers an updated version of this solution that supports SharePoint 2010. It is however a commercial product, so you would need to purchase a license if you're interested.

  100. Marc Haddad Says:

    @Deepali: It is probably the issue in the reference to the Jquery file. In my case, I am adding the reference in the master; i have too many lookup fields in the same form, so it will be useless to add the reference in each ASCX control that is loaded at runtime.
    try to put the JQuery path in the master page and let me know the result.
    It is working properly for me in SharePoint 2010.

  101. Deepali Says:

    Thanks Waldek and Marc for your quick response.
    @Marc: I tried as you suggested but its still not working. Also I tried pitting alert message box in code to see where it goes wrong but that is also not working.
    is it possible for you to post your wsp file for sharepoint2010 solution?

  102. Deepali Says:

    @Marc : I tried this on new SP2010 site and Its working now…Thanks alot.
    One more problem I am facing with this solution. If we add any value in lookup source list, it is not getting updated in newform.aspx of target list automatically.

  103. Deepali Says:

    Its working fine with single value look up and as soon as I add new entry in source list, it appears in look up column.But same thing is not working with multivalued look up. Please assist me in solving this problem.

  104. Miguel Pereira Says:

    Hi, how do i open the "add new item" opening on a new windows instead of the same one!?

  105. Miguel Pereira Says:

    I've realized that it only show the add link when i have less than 20 items!!! Why?

  106. Waldek Mastykarz Says:

    @Miguel: You could do this using JavaScript or by adding the target="_blank" attribute to the link. In SharePoint 2010 however you would preferably use Modal Dialogs.

  107. Waldek Mastykarz Says:

    @Miguel: When exceeding the 20 items limit, SharePoint uses different control, hence the add link stops working.

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2012 Waldek Mastykarz

Creative Commons License