Skip Ribbon Commands
Skip to main content

:

> Posts > Creating a Parent/Child List Relationship in SharePoint 2013
August 08
Creating a Parent/Child List Relationship in SharePoint 2013

So, yes, it’s time once again for my vNext obligatory Parent/Child list relationship post! Can you blame me though? The second post I ever wrote was on the subject and it’s still one of my most read even though it’s so old that the video links are dead. Thanks archaic video hosting site, whoever you were.

Anyway, of course one of the first things I had to do was see what the differences were between SharePoint 2010 and SharePoint 2013 on this topic. You know what I found? Luckily, for the purposes of creating a parent/child list relationship the process is ALMOST identical to my previous SharePoint 2010 blog post on the subject:

Creating a SharePoint Parent/Child List Relationship–No SPD Version

I say “almost” because a couple of things did change:

  1. The anchor element for adding a new child entry changed on the list view
  2. It appears that we no longer have to worry about the +/- 20 item lookup issue!

Other than that, I didn’t have to modify the functionality from my previous blog post on the subject at all. In fact, it looks like at this point if you were comfortable using jQuery in SharePoint 2010 that the transition to SharePoint 2013 will be pretty much effortless. What’s the feeling? oh yeah.. that’s called relevancy… strange sensation…

Anyway, enough of the rambling, let’s run through the actual process of creating a parent/child list relationship in SharePoint 2013.

The Process

So, for those who care more about the “what” than the “how”, the steps we will be taking are:

  1. Create Lists and Document Library for scripts
  2. Add the Child list to the Parent item display form and create a web part connection to only display the appropriate child items
  3. Create script for the Parent’s display form and add script to the display form page
  4. Create script for the Child’s new form and add script to the new form page.

That’s is! That’s all there is to it. So, let’s get down to business.

Create Lists and Document Library for scripts

So, the biggest difference you’ll notice at first between SharePoint 2010 and 2013 is how you create lists. It’s now an “App” to create a list? Didn’t make much sense to me either. However, in an effort to help the lowest common denominator, the below video walks you through creating the lists and document library we will be using. To remain constant with my previous posts on the subject we will be creating a parent “Issue” list and a child “Time” list that has a lookup to Issue.

Create Parent/Child Lists

Add the Child list to the Parent item display form

Except for the SP 2013 ui enhancements, this process is exactly the same as SharePoint 2010

Set up Web Part Connection

Create script for the Parent’s display form and add script to the display form page

Now we are going to write a script and place it on the Parent list display form page. This script finds the “Add New Item” link on the Child list and hijacks the element’s href attribute to call the “NewItem2“ function (Same as SharePoint 2010). As part of the function call to NewItem2 we will pass in the the URL to the New Item form of our child list and pass the ID of the Parent item as a query string variable so that the child new item form can know the ID of the parent.  Here is the script’s we will be using:

<!--
    Name: dispParent.js
-->

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>


<script type="text/javascript">

jQuery(document).ready(function($) {

    //get the ID for the Issue from the Query String
    var issueID = getParameterByName("ID");

    //find the element with the "Add new item" link.
    //note that if you have more than one list on your page, this just finds the first one
    var anchorElement = $("a[title='Add a new item to this list or library.']");
    
    //modify the "Add new item" link to call the "NewItem2" function and pass in the Issue ID. 
   //Be sure to put the path to your site below. You can use relative URL to the web application or the FQDN
    $(anchorElement).attr("href","javascript:NewItem2(event,'http://<URL TO SITE>/Lists/Time/NewForm.aspx?IssueID="  + issueID + "');");
    //remove the "onclick" attribute from the anchor element as we aren't using it anymore
    $(anchorElement).removeAttr("onclick");

});

// The following function should really be put into a utility library
// with all of your commonly called functions
//
// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

And here’s the video for how to place the script on the parent’s display item form:

Add script to Parent Display Form

Create script for the Child’s new form and add script to the new form page

Now we need to create a script for the child’s new item form and place it on that form’s page. This script will read the ID of the parent from the Query String variable “IssueID” and then set the value of the Lookup field on the form to that value.  The script will then disable the lookup field to keep those pesky users from changing it. The below script also has a line that will hide the lookup field and label if you would prefer that approach:

<!--
    Name: newChild.js 
-->

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>

<script type="text/javascript">

jQuery(document).ready(function($) {

    //get the Issue ID from they query string
    var issueID = getParameterByName("IssueID");
    
    //Set the corresponding Lookup field value to the Issue ID
       $("select[title='Issue']").val(issueID);    
       //use this line to disable the lookup field selection
       $("select[title='Issue']").attr('disabled','disabled')
       //use this line to hide the lookup field and label entirely
       //$("select[title='Issue']").closest("tr").hide();    
    
});

// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>
Add script to new item form of child

Tada!

So yeah, that’s all there is to it. Pretty anticlimactic I know… The last thing I will note is that I’m referencing the jQuery library from Microsoft’s CDN (Content Delivery Network). I usually host my jQuery library locally, but I’m trying to alleviate the issues for those who just copy and paste the script and then can’t figure out why the jQuery library didn’t get loaded.

If this version of SharePoint is like the others, you probably have 3 or 4 ways of getting this same functionality working. If you know of a better way, please let us all know… If I find a better way I’ll be sure to post as well. However, once you get the hang of this method, it’s pretty simple. You are going to want to learn your jQuery for this version of SharePoint, I can promise you that much…

Thanks again for stopping by!

Comments

Good Post Mark!

I think this is becoming a tradition...

I especially like the reference to jQuery through the aspnet content delivery network. So, I went hunting for some documentation to see what else I could use that for and came across this documentation: http://msdn.microsoft.com/en-us/library/jj163201(v=office.15) Pretty handy!
 on 8/8/2012 5:01 PM

THANKS!

Your blog saved my behind and helped me sell a SharePoint solution to a very difficult audeince - Thanks :)
 on 10/14/2012 3:04 PM

What if we have multiple content types in Child list

If we have say 2 content types in the child list, than how do we add 2x New Item buttons on the Parent display form. This is easy to accomplish in SP 2007 by simply building the HTML link in the Dataviewer but can't seem to figure out in SP 2013.

Also, if we have, say 2 child lists displayed on the Parent Display form, the New Item button for second child list goes to the first child list NewItem form because we have hijacked the link using the javascript.

Any help is greatly appreciated.
thanks
 on 3/14/2013 7:25 PM

Do you know how can I do this when the child is a document library?

Hello Mark,
Thanks very much for the post. 
I my question is a way to do this when the child is a document library?

Thanks,
 on 3/27/2013 8:17 AM

For a Document Library

Jane,

Look at my latest post for how to do the same thing with a document library:

http://www.sharepointhillbilly.com/Lists/Posts/Post.aspx?ID=40

Good luck!
Mark
EMS\markrackleyNo presence information on 3/29/2013 5:25 PM

Only secure content is displayed

If you are running SharePoint in https / SSL you may have this error appear when the script is called, especially if you are on IE9

The cause was that in the first script dispParent.js the Microsoft CDN is referenced at "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"

To resolve this issue change the http:  to https:

This resolved the issue for me.

Thanks for these great posts.  I used your "second blog post" what must be 5+ years ago, and I just used this updated one today.
 on 6/17/2013 8:08 PM

Cannot get this to work

Hi Mark, your blog has been a great resource and I have followed your instructions closely for both the 2007 and 2010 version and they worked brilliantly. However, I cannot get this to work with SP2013. After applying the dispParent.js script on the display form for Issues, when I mouseover "new item" it does not show javascript in the bottom left like in time 4:01 of your third video. In other words, the new item element's href attribute was not successfully 'hijacked' by the javascript.

I was thinking that maybe my company blocks referencing an external jQuery library. However, the second part of the newChild.js script works to disable and hide lookup field selection.

I tried saving the jquery-1.7.2.min.js in my SiteAssets document library and referencing that instead but couldn't get that to work either. So my first line went:

<script type="text/javascript" src="https://<URL to site>/SiteAssets/jquery-1.7.2.min.js"></script>

I can't figure out what I'm doing wrong.

I am a sharepoint noob (apart from following your instructions for this in SP2007 and SP2010) and javascript illiterate (apart from replacing certain lines of code/URLs according to instructions). Any help will be greatly appreciated.
 on 10/9/2013 10:58 PM

try using cookies

Hey Joe, sorry you are having issues, try this link out instead. It's a cleaner implementation of the same functionality:

http://www.sharepointhillbilly.com/Lists/Posts/Post.aspx?ID=40 

EMS\markrackleyNo presence information on 10/11/2013 8:15 AM

SharePoint Architect, Developer, Blogger, Speaker, Author, and general pain in the butt.
 
Thoughts, views, and opinions expressed in this blog are solely my own.
 
Remember, the Impossible just takes longer.
​​​​
 

 Blogs you should be reading