SharePoint Document Generator – Part 1: How to establish a parent – child relationship in SharePoint
Guest Author: Mindaugas Bliudzius
I want to show you one of the ways to quickly establish a parent – child relationship in SharePoint.
By using this, you will be able to add child items on the parent item view page as shown in the image below:

The idea of adding a new child item form is borrowed from the OOTB SharePoint Blog application template. By investigating how comments are implemented on the Post.aspx page I was able to reuse OOTB SharePoint:SubmitCommentButton control which is used by the BlogCommentsForm SharePoint:RenderingTemplate. BlogCommentsForm rendering template is defined in Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.ascx file. So here are quick steps to achieve this:
- First you need parent and child lists. In my example I have Orders and Oder details lists. In the child list you need to create a lookup column to the parent list named PostTitle (you must use this name because this is the name of the column which is used by OOTB SubmitCommentButton functionality). It is important to choose ID column in the “In this column” combo box. You can hide PostTitle lookup column if you want.
- Using SharePoint Designer 2007 open your site and navigate to the parent list folder. Copy DispForm.aspx and rename the copy CustomDispForm.aspx (you can choose whatever name you want).
- In the CustomDispForm.aspx replace ListFormWebPart with DataFormWebPart.
- Add the new ListFormWebPart for the new child list item form. Choose the New item form in List or Document Form dialog.
- Add the child list Web Part for displaying all child list items.
- Set CustomDispForm.aspx to be the parent list default display form.
- In the DefaultTemplates.ascx find the SharePoint:RenderingTemplate with ID=”BlogCommentsForm”. Select and copy all SharePoint:RenderingTemplate tags.
- Create a new file SubmitNewProduct.ascx and paste the code you copied in step 7 (you can choose another file name).
- Find SharePoint:SubmitCommentButton tag and modify the Text attribute to whatever you want (in my example I will use “Add Product”). Save the file in the same directory as DefaultTemplates.ascx.
- Using SharePoint Designer 2007 open CustomDispForm.aspx created in step 2. Find the ListFormWebPart added in step 4. Change the TemplateName tag to the same name you used in step 8.
- Now when you open Order (parent list item) you already can add new child items. But the last web part on the page still displays not filtered child items list. All you need to do is Edit the page and modify the last web part by setting the following filter: PostTitle is equal to [ID].
In the next post I show how to implement a child item selection from another list. If you noticed in the image above Order details item have a lookup column named Product. By selecting Product child item fields are automatically filled. In others solutions it may be enough to have just a lookup column, but in my case I need to fix product details (name, price and etc.) at the time of Order or Invoice creation, because later on the product price may change.
Guest Author: Mindaugas Bliudzius
Mindaugas Bliudzius has been a software developer for a 15 years. For the past few years he has focused heavily on SharePoint based solutions development and consulting. Mindaugas is co-founder of www.sharepointdrive.com and co-owner of a SharePoint hosting company based in Lithuania.
- SharePoint Document Generator - Part 1: How to establish a parent – child relationship in SharePoint
- SharePoint Document Generator - Part 2: Picking default columns values from a related list
Hi Mindaugas,
This is very interresting and clearly explained.
will definitelly have to try your solution.
Quick question: can you ‘roll-up’ calculated values from the child list to the parent one.
(like the discount total amount or the count of product in the child list?
Thanks for sharing,
Greg
This is really good and I was following Mindaugas’ blog as well.
I think it would be helpful if you (or Laura) did a screencast to see how to aactually do this because as we’ve seen in some of Laura’s previous screencasts, there are some finicky issues with modifying forms in SharePoint Designer.
Hi Greg,
Nice to know you liked it. Your question is exactly the one to which i still don’t have the answer :)
What I’m thinking (in my demo Sales Orders Management application) is to use event receivers to listen on child item changes and simply make changes to parent item. I hope that it will be one of my future posts.
Hi,
This looks like a the perfect solution I’ve been looking for. I’m a lowly Site Collection Admin though so don’t have access to DefaultTemplates.ascx as far as I know (can’t find it within SPD at least). Any chance of some further details or code snippets to help us lesser mortals along our way?
Thanks
Ben
Hye,
Thank you for your post. I use it in my current project. whith parent and child i a different list.
However a few questions to ask :
1. How do you remove the toolbar form. If I remove it, it will casue error to the page.
2. When i save the child using ‘Add product’ it will redirect to the child list. How can I set it to redirect to the same page?
thank you in advanced
Hi Mindaugas,
Thanks for publishing this..I am trying to accomplish something similar for a project dealing with Fire Rescue Weekly Supplies. What they need is to be able to submit weekly supply requests for each fire station. Each fire station will contain a list of pre-determined items to be associated with the Fire Station ID and the requested date….anyway…I followed exactly your above instructions…however for some reason they did not work for me! This is where I am currently stuck: steps 4-6 do not seem to work for me in Designer:
4. Add the new ListFormWebPart for the new child list item form. Choose the New item form in List or Document Form dialog. [This step is definitely NOT CLEAR, where do you do this? at the parent list level or the child list level?] I assumed it should be at the parent list level, so I went ahead and did it.
5. Add the child list Web Part for displaying all child list items. [same as above, parent or child?]
I am currently stuck exactly between these 2 steps. Since designer is giving me an error and it is not allowing me to save the page. This is the error I am getting after making the changes as stated on steps 4 and 5: Server error: The URL ‘Lists/Orders/CustomDispForm.aspx; is invalid. It may refer to a nonexistent file or folder that is not in the current Web.
6. Set CustomDispForm.aspx to be the parent list default display form. [how in the world do you do this if this option is grey out in designer?]
I also notice in step 3 that we are supposed to do the replacement as follows: In the CustomDispForm.aspx replace ListFormWebPart with DataFormWebPart. [this step is previous as adding the new item form and child list form, which by the way overwrite the replacement…?
I sincerely appreciate if you can walk me through this process, so I can get this accomplished.
Thank you.
Hi,
1. If you are talking about child item form toolbar then you can remove it by adding following line just before tag:
None
2. You can use here a couple of solutions :
You can remove Source parameter from query string on page load and then redirect like this:
$(document).ready( //using jQuery
function()
{
var i = document.location.href.lastIndexOf(”&Source”);
if (i > -1){
document.location.href = document.location.href.substring(0,i);}
}
);
or
You can use PreSaveAction() which is called just before form submit and change Source parameter in here. It may look like this:
function PreSaveAction() {
var srcUrl=GetSource();
var i = aspnetForm.action.lastIndexOf(srcUrl);
aspnetForm.action = aspnetForm.action.substring(0,i) ;
return true;
}
You can add JavaScript to the page using Content Editor Web Part.
Hi Carlos:
All steps are done in the CustomDispForm.aspx
4. You can insert ListFormWebPart by going Insert->SharePoint controls->List form … . In the “List or Document Form” dialog choose your child list and “New item form” option.
5. Again in the same CustomDispForm.aspx just below list form web part you inserted in step 4. Select Insert->SharePoint controls->Web Part … and select child list web part. Actually you can insert it using SharePoint user interface by editing page in the browser.
6. Right mouse click on parent list folder in SharePoint Designer. Select Properties-> Supporting Files tab. Then select correct content type and Browse for the new display item form ( CustomDispForm.aspx). Best to do it when content type for which you are setting supporting files is the default content type configured in parent list settings (using standard SharePoint user interface).
3. You must replace the default ListFormWebPart that is already there when you copied the default DispForm.aspx and renamed it to CustomDispForm.aspx.
Also there is some bugs in last steps. You can find them explained in my blog over here http://www.sharepointdrive.com/blog/Lists/Posts/Post.aspx?ID=6