Pass Default Value From Web Part Page to New Item

Another name for this post could be “Master-Detail Items”

This is a question that is asked all the time, and there are several examples to explain the concept.  For example, if I have a web part page with a list of projects, and on that web part page, the lists of tasks and issues associated with that project are displayed.  The problem is that out-of-box when someone clicks a button to create a new task associated with that project, it gives them the new task form, but no default project information is passed.

Another example of this type of thing came up recently, where someone was gathering information using a SharePoint list form, but wanted to be able to have the equivalent of a repeating table within the form, so that there could be multiple sub-items related to the parent item.  I’ll use a very simple example of a site that has a library full of customer contracts, one for each customer.  Since each customer can have multiple locations, there is a separate list on the site called Customer Locations.  When we are looking at a page that shows all the data for one customer, we want it to be filtered to show only that customer’s locations, and when you add a new location, the customer field in the Customer Locations list will default to that customer name.  So here’s how this is accomplished.

I’m going to do this in SharePoint 2010, which only slightly different than the way it’s done in 2007, just because the SharePoint Designer interface is so different.

  1. Read my blog post called Query String URLs are Magical. In this specific example, we only have a Customer Contracts library, and a Customer Locations custom list.  There’s also a video and a series of 4 total posts on the query string concept.
  2. The Customer Locations list has the following fields:
    Title (changed to “Location Name”)
    Location Contact Name – single line of text
    Location State – single line of text
    Customer Lookup – Lookup field to the Customer Contracts library.  Use the “Title” as the main field, and also select the check box next to the ID field.
  3. Following the instructions in the aforementioned blog post, create a workflow and a web part page called CustomerDetails.aspx, and put web parts for Customer Contracts and Customer Locations on the page.  Use the query string URL filter to send a query string called Customer to both web parts:
    Query String filter –> Customer Contracts – connect to send filter to the ID field.
    Query String filter –> Customer Locations – connect to send filter to the Customer Lookup:ID field.
  4. Once the web part page, workflow and query strings are all in place and filtering correctly, and all of the instructions in the query string URL post have been followed, you should have a link in each customer’s row that says “Customer Details” that will take you to that detail page that only shows that customer’s document and that customer’s location.
    ch5[79]
  5. Okay, here’s where this blog post REALLY starts.  How to add the form to add new customer locations on this page, where the form defaults to  the current customer.  Open your site in SharePoint Designer, and open up this web part page (CustomerDetail.aspx)
  6. Put your cursor in the bottom of the lower web part, and in SharePoint Designer 2010, click the Insert tab on the ribbon, click Display Item Form, and choose the Customer Locations list.
    newitem
    If you’re using SP2007, click the Insert menu, choose SharePoint Controls, click Custom List Form, and choose the name of the Customer Locations form and New Item Form
  7. Now that your new item form is at the bottom of the  web  part page, in the Options tab in the ribbon, click Add/Remove Columns, to add the 4 columns listed in step 2 above.  It’s important that you add the Customer field.
  8. Click the Parameters button in the ribbon.  Add a new parameter called CustomerParam, as so and click OK
    ch5[83]
  9. In your New Form, click to select the Customer Lookup field like this: and…
    HIT THE DELETE KEY.  YES, DELETE THE CUSTOMER LOOKUP DROP-DOWN.
    ch5[84]
    If you’re in SP2007, you don’t have to delete the existing one, you can just click the little gray chevron on it and change it to a text box, and then skip step 10 and 11.
  10. Leave your cursor in the same spot, and click the Insert tab in the ribbon, choose SharePoint, and choose Text Box.
    newitem2
  11. On the new text box, click the Data Field drop-down box, and choose the Customer Lookup field.
    newitem3
  12. At the bottom of the screen in SharePoint Designer, click to switch to split view, because we have to do a tiny thing in the code.  Once you’re in split view, click to select this new text box in the bottom pane, so that you can see its code in code view.  You’ll see something like this:
    ch5[88]
  13. The text= is the part that needs to be changed, so change it to this:
    ch5[89]
    It’s important that the @ is changed to a $, and if you didn’t do step 8, there will be problems.  What we’re doing here, is sending the customer parameter straight from the URL to this field.  We’ll hide this field later, after testing.
  14. Save this page, time to test.  Go to the customer detail page for one customer, as shown in step 4. Make sure that you have a real customer ID in the URL, and you see at least one item in the Customer Contracts library on this page.  Notice that you see the ID from the URL now showing in the new form web part!
    ch5[90]
  15. Okay, now fill out the form and submit it, and you’ll notice that your new item will then appear in the list of customer locations for that customer!  Since it’s been tested, the field can now be hidden.  Go back to SharePoint Designer.
  16. Back to SharePoint Designer, to the code view, back to the same spot you were.  The following will allow you to just hide that whole “customer lookup” row.  Find the <tr> which is the row of the customer lookup, and add this: style=”visibility:hidden”
    ch5[91]
  17. Save.  Done.

Now there are some things you’re going to want to do to clean this up and look nicer and more presentable.  On each of the web parts in the browser, go to the web part properties, and change the Toolbar to “none”.   Also, you may want the NewForm itself to look nicer, like make all of the fields line up with the fields in the web part above.  That’s a matter of adding columns to the table in your new form web part, and rearranging fields.  Messy and time consuming, but sometimes it’s gotta be done.

Some things to note:  I used the ID field in the main Customer library, and even though it is a lookup field in the Locations list, it is stored as an ID number.  This is why a number is necessary in this field that is being automatically populated.  It won’t work if you try to type the actual name of the customer in that text box.

Thanks to Mark Rackley for showing me how to do this a couple of years ago in SharePoint 2007.  Another blog post of mine that is similar to this one is SharePoint List Form – Default User Information.

15 comments

  • Nice, Laura! It’s a shame that something so basic is this complicated, but … I used a slightly different approach and added some embedded JavaScript to check for parameter values and automatically fill in out-of-the-box form fields if found. Same net effect though.

  • Areon.Jackson@lamresearch.com

    Grerat post as usual. My company is deploying 2010 in about a month so I am going to wait to until that is done to complete these steps in Dev. I know there are people that have asked and will continue to ask for in production. I’ll be able to use this to provide them what they need. Thank you for your post, Areon Jackson

  • Thanks – this is great. I ran across this new tool the other day, which does something similar and may be easier. I haven’t tested it yet, but for those who are interested in this type of functionality it might be interesting to follow. Here’s the link: http://code.google.com/p/sp2010-related-list-prefill/

  • I am implementing this data view web part in a project office solution in sp2007. In the web part page I have the following: Web part header 0 – Query string filter for Project Id Web part left zone 0 for project listing filtered by query string on project id = id Web part left zone 1 for listing of project issues filtered by query string on project id = projid in issues list. Right zone 0 – Data view web part to insert a new issue based on the example above. I find however if the user inserts a new issue and refreshes the page (F5) to see the effect, the insert gets duplicated for how many times the user refresh the page? even though the data view web part is based on a new insert with a required property on the Issue Name field. How do I ensure that SP2007 web part page does not resubmit the insert of the issue on refresh of the web part page?

  • The code mentioned by Joel only seems to work when the Listbox has less than 20 items, due to SP’s wonderful “feature” of not using HTML Select elements when there are more than 20 items. Instead it using input elements with lots of javascript to simulate the select function.

  • Charld, Good question, I’ve never seen that happen, though. Usually when I implement this, the displayed list automatically is updated when the new item is added, so the users don’t have to refresh the page.

  • Has anyone ever tried this with a document library? Usually when adding a document, it loads the upload form then the edit form to add the metadata. This is where I want the query string value to be passed to a field on the form. I am able to do this on the form. However, when I click on “Save” it is not updating the field in the document library. This has been done for a regular list and there are no problems.

  • Hi Kay, This is totally different for a document library. When I googled this, I found that people usually use custom code to accomplish it. I got it to work once, but I don’t remember how I did it and I do remember that it was very fluky and sort of duct-taped together. 😉

  • Hi
    I have a question bit related to the topic.

    SharePoint 2010:

    ListA: NewForm: I have a field called “company name” ddl(lookup column).

    Right next to the column I did put a link that opens a popup window(NewForm of ListB).
    In ListB NewForm I have a same field like ListA: company name(lookup).

    Requirement: when user click the link to open popup, the company name field value should be pre-select as the same value which is selected in listA: company name.

    My Link Code to open a popup window:

    (Summary:

    List A is a company list and List B is contact list.
    Usually when user select the company name(ListA: NewForm) from ddl, it filters and gives the contact name of that company in the next field called “contact”,but if the company didn’t have any contact( none), then user click on the link to add new contact., so after clicking on the link, it pops the listB new form window to add a new contact and the field company name on listB should be pre-selected, as the one which is selected on listA New form.)

    Thanks, any ideas really appreciatable.

    Cheers
    Ven

  • The link to your Query Strings are magical article does not point to the article, I think that is should be going to http://sharepoint911.com/blogs/laura/Lists/Posts/Post.aspx?List=daba3a3b%2Dc338%2D41d8%2Dbf52%2Dcd897d000cf3&ID=70&Web=dbb90e85%2Db54c%2D49f4%2D8e97%2D6d8258116ca0

  • It used to work on mine applications for around a year!
    But now hiting the Save button leads to the error: “The data source control failed to execute insert command…….”
    I am wandering if you have seen such kind of error?
    Thank you
    galina.gilman@genzyme.com

  • Thanks for the most helpful information I could find anywhere.
    How about some help on defaulting values when adding (uploading) files to a library?

    twitter: GrodKnight

  • Ven, no I don’t know how the code works, or how to troubleshoot it.
    Galina, I’ve never seen that error, sorry.

    Gerard, see my previous answers to other questions about that.

  • Awesome. Something this simple should be documented in the manuals, or at least some of the how-to books. This has been driving me crazy for several weeks.

    I found another fix for increasing the size of the text box to accommodate larger entries here:
    http://stackoverflow.com/questions/2125509/how-do-i-set-the-size-of-an-html-text-box
    Essentially you add a new css entry and point the class parameter of the control to the new entry. The article describes this nicely.

    Thanks!

  • Thanks Mark, that’s helpful!

Leave a Reply