How to establish a parent – child relationship in SharePoint

I want to show you one of the ways how to quickly establish parent – child relationship in SharePoint. By using this way you will be able to add child items on the parent item view page as shown in the image below:
091709_0840_Howtoestabl1

The idea how to add new child item form is borrowed from OOTB SharePoint Blog application template. By investigating how comments are implemented on Post.aspx page I was able to reuse OOTB SharePoint:SubmitCommentButton control which is used by 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 how to achieve this:

  1. First you need a parent and child lists. In my example I have Orders and Oder details lists. In child list you need to create a lookup column to parent list named PostTitle (you must use this name because this is the name of 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.
  2. Now 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).
  3. In the CustomDispForm.aspx replace ListFormWebPart with DataFormWebPart.
  4. Add new ListFormWebPart for the new child list item form. Choose New item form in List or Document Form dialog.
  5. Add child list Web Part for displaying all child list items.
  6. Set CustomDispForm.aspx to be the parent list default display form.
  7. In the DefaultTemplates.ascx find SharePoint:RenderingTemplate with ID=”BlogCommentsForm”. Select and copy all SharePoint:RenderingTemplate tag.
  8. Create new file SubmitNewProduct.ascx and paste the code you copied in step 7 (you can choose another file name).
  9. Find SharePoint:SubmitCommentButton tag and modify Text attribute to whatever you want (in my example I will use “Add Product”). Save the file in the same directory as DefaultTemplates.ascx.
  10. Using SharePoint Designer 2007 open CustomDispForm.aspx created in step 2. Find ListFormWebPart added in step 4. Change TemplateName tag to a name you named the file in step 8.
  11. Now when you open Order (parent list item) you already can add new child items. But last web part on page still displaying not filtered child items list. All you need to do is Edit the page and modify 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 name Product. By selecting Product automatically are filled child item fields. 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 product price may change.

Posted in Customization, javascript, relationship
43 comments on “How to establish a parent – child relationship in SharePoint
  1. Help with "TemplateName" says:

    I’ve tried walking through you example on a demo site and can’t get it to work properly. The “SubmitNewProduct” template is not loading. I’m new to SharePoint development, so I guess I’m overlooking something. In step 10, you said change “TemplateName” tag to a name you named the file in step 8. However, when I add a new ListFormWebPart (in step 4), there is no templateName tag on the webpart. I’ve tried adding the tag but can’t seem to get it work. Can you elaborate on step 10? Perhaps a code snippet of the TemplateName tag and where to place it?

  2. My fault says:

    I missed one step. In SubmitNewProduct.ascx file you must change ID to “SubmitNewProduct” also:
    SharePoint:RenderingTemplate ID=”SubmitNewProduct”

    In step 10 you must look for this tag:

    <TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm"> ListForm</TemplateName>

    and change ListForm to SubmitNewProduct.

    P.S. Sometimes you need to save, close and reopen your page in SharePoint Designer to see full ListFormWebPart code.

  3. Almost there.. says:

    Thanks for the quick reply. You were correct in having to close and re-open SP Designer to see the full ListFormWebPart code. I still can’t get the template to load. If I change the template name to “BlogCommentsForm”, the template loads and everything works. However, when i change the template name to SubmitInvoiceProduct, nothing loads. I’ve tried the following to simplify things.

    Copied the BlogCommentsForm template and pasted it at the bottom of the “DefaultTemplates” control. changed the ID to <SharePoint:RenderingTemplate ID="SubmitNewProduct" , changed the template name to SubmitNewProduct, saved and reloaded the page. Template does not load. Do i need to restart IIS or SharePoint services to refresh the templates or load it into the GAC?

    Also, you said to change ID (in SubmitNewProduct.ascx) to SubmitNewProduct. I'm not sure what ID you're refering to. I only have the <SharePoint:RenderingTemplate ID. What other code needs to be included int the template (SubmitNewProduct.ascx)?

    Thanks for the help. If I get this working properly it will open up some options for developing forms with wss 3.0. (we don't have info path on the client machines in our company).

  4. Strange... says:

    Try reset IIS.

    I’m talking about RenderingTemplate ID:

    <SharePoint:RenderingTemplate ID="SubmitInvoiceProduct" runat="server">

    Beside that you don’t need to change anything in ascx file.

    I see in your last comment that you are using SubmitNewProduct everywhere so TemplateName also must be set to SubmitNewProduct not SubmitInvoiceProduct. In other words TemplateName must be the same as RenderingTemplate ID. (this means that there is one more bug in my 10th step where i said that TemplateName must be set to the file name)

  5. Thanks. says:

    Finally got this working. The issues I had were

    1.) Did not see all the markup for the list created in step 4 until I closed and reopened the custom display form. I was then able to modify the TemplateName.

    2.) I had to restart IIS for the changes to take effect.

    3.) After all of this, SharePoint Designer decided to remove the “~/” from the src attribute on my control templates (including the DefaultTemplates.asxc control) which broke the entire Intranet.

    In short, I am not a fan of SharePoint Designer.

    thanks for the excellent post and the help troubleshooting.

  6. Thanks says:

    I cannot get the filter to work, PostTitle is equal to [ID]

  7. Mindaugas says:

    In step 1 it is important to choose ID column in the “In this column” combo box. That means your PostTitle column must be set to show ID field of related list item.

  8. Filter issues says:

    I did select ID as the lookup for the PostTitle column. The PostTitle even shows the correct ID number, but when I add the filter to the webpart itdoesn’t show any items.

  9. Mindaugas says:

    Maybe we can arrage a quick TeamViewer session or similar so i can take a look?

  10. Help says:

    Sorry, could you explain in step 4. I’m new in SharePoint 2007

  11. Mindaugas says:

    In SharePoint Designer Insert menu choose SharePoint Controls->List Form. Don’t forget to select child list.

  12. Help says:

    after adding DataFormWebPart, when i view the “customdispform.aspx” in IE, i am getting error “An error occurred processing the data view. The XslText property is empty.”

  13. Hack says:

    This is a total hack and really bad instructions, took me 4 hours to understand this and it still doesn’t work correctly as per your instructions!

    I think i’ll go the .net route

  14. Can you do this by code? says:

    Hello
    this is good, but can you do this by code? using VSeWSS1.3,

  15. Cannot link PostTitle says:

    Hi,

    Thanks for this post. Saved me alot of time!

    However, i still encounter 1 problem. I cant seem to link the PostTitle to the ID. When I do link it, nothing displays when adding a product. It usually displays PostTitle = “[ID]”. I’ve even tried [ListID] (which is an available selection), @ID, etc. etc.

    Any ideas?

    Thanks. Regards.

    • FilterByParentID says:

      Hello, I have the same problem, ΒΏcan you resolve this problem?, I try to resolve using XSLT, and JavaScript, not work.

      Thanks.

  16. Cannot link PostTitle says:

    By the way, the previous post refers to Step 10.

    Thanks.

  17. Re: How to establish a parent – child relationship in SharePoint says:

    Anyone? i.e. Previous post?

  18. Late response says:

    Did you ckecked this:

    “TemplateName must be the same as RenderingTemplate ID. (this means that there is one more bug in my 10th step where i said that TemplateName must be set to the file name)”

  19. Re: Later response says:

    Hi, thanks for the reply.

    Actually, what i’ve done it, instead of creating a new file, i just refernced the ListForm to BlogCommentsForm.

    Basically, I’m using the same Default*.acsx file. I didnt create a new 1.

  20. Re: Later response says:

    Glad to know you made it πŸ™‚

  21. List name says:

    How can i get the name of the List when i get to the RenderingTemplate and send it to my WS?
    i succeded to send parameters to my ws function,but does not know how to get the list name or the url that called RenderingTemplate .

  22. To List name says:

    Sorry can’t understand what you are asking. What do you mean by “send it to my WS” ?

  23. WSS error says:

    in MOSS works fine, but in WSS only “unexpected error” after submit action.

  24. Regard WSS error says:

    It should work on both WSS and MOSS, sorry but from information you provided it is hard to suggest a solution for you problem.

  25. STEP 3 ERROR says:

    Hi,

    i was trying to follow all the steps, even the suggestions included in the comments. but i cannot overcome the error in Step 3. i cannot change ListFormWebPart with DataFormWebPart cause i cannot save the CustomDisplayForm afterwards. the error message is
    Server Error. The URL is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current web

  26. STEP 8 Error says:

    Hi,

    I cannot get it to work, made a new ascx file:

    &lt;SharePoint:RenderingTemplate ID=&quot;NewPayoutRequest&quot; runat=&quot;server&quot;&gt;
    &lt;Template&gt;
    &lt;table width=275 cellpadding=0 cellspacing=0 border=0&gt;&lt;tr&gt;&lt;td&gt;
    &lt;SPAN id='part1'&gt;
    &lt;SharePoint:InformationBar runat=&quot;server&quot;/&gt;
    &lt;TABLE class=&quot;ms-formtable&quot; border=0 cellpadding=0 cellspacing=0 width=100%&gt;
    &lt;SharePoint:ChangeContentType runat=&quot;server&quot;/&gt;
    &lt;SharePoint:FolderFormFields runat=&quot;server&quot;/&gt;
    &lt;SharePoint:ListFieldIterator runat=&quot;server&quot; ExcludeFields=&quot;PostTitle&quot;/&gt;
    &lt;SharePoint:ApprovalStatus runat=&quot;server&quot;/&gt;
    &lt;SharePoint:FormComponent TemplateName=&quot;AttachmentRows&quot; runat=&quot;server&quot;/&gt;
    &lt;/TABLE&gt;
    &lt;TABLE cellpadding=0 cellspacing=0 width=100% style=&quot;padding-top: 10px&quot;&gt;&lt;tr&gt;&lt;td width=100%&gt;
    &lt;wssuc:ToolBar CssClass=&quot;ms-formtoolbar&quot; id=&quot;toolBarTbl&quot; RightButtonSeparator=&quot;&amp;nbsp;&quot; runat=&quot;server&quot;&gt;
    &lt;Template_RightButtons&gt;
    &lt;SharePoint:SubmitCommentButton CssClass=&quot;ms-ButtonHeightWidth2&quot; runat=&quot;server&quot; Text=&quot;Add New&quot;/&gt;
    &lt;/Template_RightButtons&gt;
    &lt;/wssuc:ToolBar&gt;
    &lt;SharePoint:InitContentType runat=&quot;server&quot;/&gt;
    &lt;SharePoint:ItemHiddenVersion runat=&quot;server&quot;/&gt;
    &lt;/td&gt;&lt;/tr&gt;&lt;/TABLE&gt;
    &lt;/SPAN&gt;
    &lt;/tr&gt;&lt;/td&gt;&lt;/table&gt;
    &lt;SharePoint:AttachmentUpload runat=&quot;server&quot;/&gt;
    &lt;/div&gt;
    &lt;/Template&gt;
    &lt;/SharePoint:RenderingTemplate&gt;

    And if i change the:

    &lt;TemplateName xmlns=&quot;http://schemas.microsoft.com/WebPart/v2/ListForm&quot;&gt;ListForm&lt;/TemplateName&gt;

    The input entry is gone from CustomDispForm.aspx

    Getting this in the eventlog..

    Load control template file /_controltemplates/NewPayoutRequest.ascx failed: Unknown server tag ‘SharePoint:RenderingTemplate’.

    What am i missing ?

    /F

  27. Regard STEP 8 error says:

    Try adding this at the top of you custom control (ascx) file:

    &lt;%@ Control Language=&quot;C#&quot; %&gt;
    &lt;%@Assembly Name=&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %&gt;
    &lt;%@Register TagPrefix=&quot;SharePoint&quot; Assembly=&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; namespace=&quot;Microsoft.SharePoint.WebControls&quot;%&gt;
    &lt;%@ Register TagPrefix=&quot;wssuc&quot; TagName=&quot;ToolBar&quot; src=&quot;/_controltemplates/ToolBar.ascx&quot; %&gt;
    &lt;%@ Register TagPrefix=&quot;wssuc&quot; TagName=&quot;ToolBarButton&quot; src=&quot;/_controltemplates/ToolBarButton.ascx&quot; %&gt;

    Those code lines is taken from the top of DefaultTemplates.ascx
    (it may be different on your computer).

  28. Regards STEP 3 error says:

    I googled little bit and found that it can be related to a not closed tag or even missing some tags. Check this out : http://social.msdn.microsoft.com/forums/en-US/sharepointcustomization/thread/4bab44a1-e907-4f18-b165-1e8b4a25883b

  29. Clear Fields after 'Add Product' says:

    Hi this is awesome !

    Have a question: How can i get it to clear numeric fields after i press ‘Add product’ ? (It clears text fields but not numeric or currency)

  30. Re: How to establish a parent – child relationship in SharePoint says:

    Question for Step 3:

    How do you replace ListFormWebPart with DataFormWebPart?

  31. Randy says:

    Just as an fyi. I have found one small inconvenience when BDC fields are used.

    After submitting the modified form the BDC field is not refreshed or cleared for the next entry.

    Have you seen this?

  32. subhash says:

    Hello how can i get ubmitNewProduct.ascx page. and what is OOTB SharePoint Blog application template.

    Please suggest me ….

  33. subhash says:

    Hello how can i get submitNewProduct.ascx page. and what is OOTB SharePoint Blog application template.

    Please suggest me ….

  34. Prithvi says:

    Hi,

    i cant understand 11th step..See i cant add any filters..Please sort out this prob..

    Thanks

  35. Prithvi says:

    friends pls refer this below link

    chick here

  36. Gomathy says:

    Hi,

    Is it possible to achieve the same functionality using Javascript (JQuery also) and Sharepoint designer instaed of using user control?

    Since I don’t have server control, I need to achieve the result using site & designer.

    Please suggest me

    Thanks
    Gomathy

  37. TylerJL says:

    Could you please rewrite the post with some images because it is too hard to understand your post?

  38. Jessie says:

    Hi Guys,

    This is great, thanks for posting this solution, I already implemented this to one of my sharepoint lists. My only issue is that when i added items for my child list it would
    trigger the save and stop editing, can you please guide me on how to remove the save and stop editing. thanks in advance. πŸ˜€

  39. Jeff Brown says:

    Could you lay out your instructions with more detail for us beginners? I’m new to SharePoint Designer and could use some more details to explain them a bit.

  40. Jeff Brown says:

    I was wondering if you or someone else might be able to provide more details. I am new to SharePoint Designer and working on one to many lists. Your example looks like something I need to do here at my work, however I’m a bit confused due to my lack of experience. I would appreciate any assistance with providing more in depth details. Thanks.

  41. Amy says:

    I made it through the steps… Some notes for SP 2010:

    1.No notes
    2.I couldn’t figure out how to copy the display form and have it show up in the Designer view, so I created a new form and saved it as default.
    3.Edit the default webpart and select to Close and Hide it under the Layout option in the toolpane. Add a dataview webpart (Insert/SharePoint/List Form[Select your parent list and the display form option]) You can also delete some of the extra stuff if you want to… second Close button, etc.
    4.(Insert/SharePoint/List Form[Select your child list and the new form option]).
    5.Go to the parent list settings and modify the default display form under “Form Web Parts” to add a view of the child list. Edit the current view and add the fields you’d like to display.
    6.If you create your own form, this is already done.
    7.Make sure you include the code above the template tag.
    8.No notes.
    9.No notes.
    10.There was no TemplateName tag, so I copied 0 and changed it to .
    11.Select the last web part which shows all child items. Go to Options/Filter to create a filter = PostTitle is equal to [ID].

    **I had to disable attachments in my parent list… it was throwing an error when I submitted. I was able to keep attachments enabled for my child list. I ended up hiding the PostTitle lookup column, so now users can only enter items through the parent list. If items are created directly in the child list, the PostTitle column will not be populated and the items will not show up in the merged form. I also added the Edit icon to the view so users can open a new window and edit the child items.

    Hope this helps!

    • Amy says:

      10. The text didn’t show up. I copied the ListItemId tag and updated it to TemplateName.