Skip to main content

SharePoint 2013 online Parent/Child relationship - A step by step demo

SharePoint uses lookup fields to represent the Parent-Child relationship. For child list items it is OK as you can trace back to its parent from the lookup field. For parent the OOB forms are not good enough as they are not displaying the children belong to it. In real world it is often required that when view the parent list item it also displays its children, further more it is great if user can add child list items directly from the parent.

In SharePoint on-premise we can use visual web parts and code the parent/child logic and use web part pages to replace the parent item forms. In SharePoint online we can't do server model code and visual web parts, so the solution is almost NO CODE and just a little bit client side scripts, and they work beautifully!

Here I will do a step by step demonstration to explain the ideas:

 

Step 1: Create a parent list called "Category" and a child list called "Product", in Product list add a lookup field to reference Category parent item, And then create some dummy list items



Image 1.1 - Category List

 



Image 1.2 - Product List











 

Step 2: Add child list items to parent view form

View any of the list items in "Category" list, click "Settings->Edit Page", and then press "Add a Web Part"
 
Press "Insert->Related List", and then click "Product"
 
 
After that you will see a list of related children list items show on the bottom of the main item
 
As in display view we just want user to view related items but don't want user to edit child items directly, we need to do the following changes for the newly added web part:
- Set Toolbar type to "No Toolbar"
- Set Chrome Type to "None"
 
Here we go, now when user open a category item, he can also see all its children, nice!
 
 

Step 3: Add child list items to parent edit form

Open any of the list items in "Category" list and switch to Edit, click "Settings->Edit Page", and then press "Add a Web Part"
 
 
Press "Insert->Related List", and then click "Product"
 
 
As in Editing view we not only want user to view related items but also to Add/Edit/Delete child items, we leave the toolbar on.
 
Now when user edit a category item, he can also see all its children too!
 
 
 

Step 4: Make it possible to add child list items directly

Now we can display child items in parent item views, but it will be even more handy if we can make it happen that user can create a new child directly from its parent edit view without specifying the lookup. This is especially useful for a big lookup list.
 
First of all, you need to include jquery in your page header. There are multiple ways to do that and I leave this to you.
 
Also in my script I use a 3rd party js file called Querystring to parse the query string, it is free:-)  You need to include this js in your page header as well. alternatively you can use or write similar functions for your own choice.
 
Change Category Edit Form
 
Now go to category item edit form, click "Settings->Edit Page", and then add a "Script Editor" web part.
 
Put the following script and save the page
 

This script overwrites the "New item" link in the form to let it open the adding form in modal dialog, and it refreshes the parent view when new item added. It also passes the current category item ID to the child form so it can set the parent lookup value by default.
 
Change Product NewItem Form
 
Go to product list and click "New Item", click "Settings->Edit Page", then add a "Script Editor" web part and put the following script in. After that save the page.
 

This script will set parent automatically by using the passed in ID and lock the selection. 
 
Job done! Now when you edit a category you can add/edit/delete its children easily!
 
 
 


Comments

  1. Great stuff Ting! Useful and easy to follow article.

    ReplyDelete
  2. Do you have an example to create a folder in SP2013/SP2010 workflow?

    ReplyDelete
  3. This is great! But what if your Category list has many fields, say 15 or so? When you view the related items you see all the fields listed vertically. Doesn't look so good. Any ideas?

    ReplyDelete
  4. How about of you have more than one related child list?

    ReplyDelete
  5. While this example appears to be simple and to the point, it is still confusing to those of us that do NOT "get" code. Q1: If "Category" is the name of your Parent Table and "Product" is the name of your Child Table, do I simply type in the names of my parent and child tables in the places where yours are referenced?
    Q2: when you reference the title under var options as "Add Product" do I replace the word Product with the name of my Child Table?
    Q3: In you script for changing the Category Edit Form you show that the var categoryID = $.QueryString as equal to the value of ("ID") but in the Change Product NewItem Form script you show the $.QueryString value for var categoryID to be = to ("categoryID") This confuses me. The ID value in my Parent table is "ID", but in my Child table I place it in a column titled "ParentID".
    Any clarification you can give would be appreciated.

    ReplyDelete
  6. Having the child item inherit the parent category when created from the New Item button in the parent is so useful, why in the 5 years I've been working with SharePoint hasn't the capability been baked into the product. I've been jury-rigging it with Java Script for years. Why is this an out of the box capability???

    ReplyDelete
  7. Could you please tell me how to include jquery and "Querystring" from point 4 in my page header? Or reference some tutorials? I'm totally new to jquery

    ReplyDelete

Post a Comment

Popular posts from this blog

A Step-by-Step guide to use HttpModule in SharePoint with SPContext

I guess you must be familiar with what is HttpModule and how can we leverage it for our web applications. But if not, better have a good read of this article . SharePoint is built on top of .NET, so it has all the rights to use the beauties of .NET. Here I want to demo a simple case of httpmodule on SharePoint. Say if in a company it has lots of site collections and user 'Picky Docky' has access for all of them, for a reason farm administrator want to block him for one of the specific site without change his SharePoint permissions. Here are the steps to do it:-)   Create a empty SharePoint solution add a class file to it named  CustomSecurityModule.cs , code as below Create a feature and scope to "WebApplication" Create a feature receiver. Code as below When activate the feature it will create a modle in the web.config file for the SharePoint web applicaiton you deployed to as below: Enjoy!

Gist

If you want to embed source code (e.g. C#, HTML..) to your blog post I recommend to use Gist https://github.com/ . Especially if you want to give your readers the opportunity to copy and paste your code.

A SharePoint 2013 weather web part

I created a nice SharePoint 2013 weather web part. It has extremely simple code-behind and so easy for you to use or extend.  The core is using Yahoo! Weather Feed Plugin for jQuery Please go to https://weatherwebpartsp2013.codeplex.com/  to grab it. It is free with all the source code. Enjoy!