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:
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
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!
Great stuff Ting! Useful and easy to follow article.
ReplyDeleteDo you have an example to create a folder in SP2013/SP2010 workflow?
ReplyDeleteThis 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?
ReplyDeleteHow about of you have more than one related child list?
ReplyDeleteWhile 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?
ReplyDeleteQ2: 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.
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???
ReplyDeleteCould 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