Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Posted by Mark Rackley on Geeks with Blogs See other posts from Geeks with Blogs or by Mark Rackley
Published on Fri, 17 Dec 2010 00:12:41 GMT Indexed on 2010/12/18 17:15 UTC
Read the original article Hit count: 655

Filed under:

Hey blog readers… It has been almost 2 years since I posted my most read blog on creating a Parent/Child list relationship in SharePoint 2007:

Creating a SharePoint List Parent / Child Relationship - Out of the Box

And then a year ago I improved on my method and redid the blog post… still for SharePoint 2007:

Creating a SharePoint List Parent/Child Relationship – VIDEO REMIX

Since then many of you have been asking me how to get this to work in SharePoint 2010, and frankly I have just not had time to look into it. I wish I could have jumped into this sooner, but have just recently began to look at it.

Well.. after all this time I have actually come up with two solutions that work, neither of them are as clean as I’d like them to be, but I wanted to get something in your hands that you can start using today. Hopefully in the coming weeks and months I’ll be able to improve upon this further and give you guys some better options.

For the most part, the process is identical to the 2007 process, but you have probably found out that the list view web parts in 2010 behave differently, and getting the Parent ID to your new child form can be a pain in the rear (at least that’s what I’ve discovered).

Anyway, like I said, I have found a couple of solutions that work. If you know of a better one, please let us know as it bugs me that this not as eloquent as my 2007 implementation.

Getting on the same page

First thing I’d recommend is recreating this blog: Creating a SharePoint List Parent/Child Relationship – VIDEO REMIX in SharePoint 2010… There are some vague differences, but it’s basically the same…  Here’s a quick video of me doing this in SP 2010:

Creating Lists necessary for this blog post

Now that you have the lists created, lets set up the New Time form to use a QueryString variable to populate the Parent ID field:

Creating parameters in Child’s new item form to set parent ID

Did I talk fast enough through both of those videos? Hopefully by now that stuff is old hat to you, but I wanted to make sure everyone could get on the same page.  Okay… let’s get started.

Solution 1 – XSLTListView with Javascript

This solution is the more elegant of the two, however it does require the use of a little javascript.  The other solution does not use javascript, but it also doesn’t use the pretty new SP 2010 pop-ups.  I’ll let you decide which you like better.

The basic steps of this solution are:

  1. Inserted a Related Item View
  2. Insert a ContentEditorWebPart
  3. Insert script in ContentEditorWebPart that pulls the ID from the Query string and calls the method to insert a new item on the child entry form
  4. Hide the toolbar from data view to remove “add new item” link.

Again, you don’t HAVE to use a CEWP, you could just put the javascript directly in the page using SPD.  Anyway, here is how I did it:

Using Related Item View / JavaScript

Here’s the JavaScript I used in my Content Editor Web Part:

<script type="text/javascript">

      function NewTime() {
        // Get the Query String values and split them out into the vals array
        var vals = new Object();
        var qs = location.search.substring(1, location.search.length);
        var args = qs.split("&");
        for (var i=0; i < args.length; i++) {
            var nameVal = args[i].split("=");
            var temp = unescape(nameVal[1]).split('+');
            nameVal[1] = temp.join(' ');
            vals[nameVal[0]] = nameVal[1];
        }
        var issueID = vals["ID"];
        //use this to bring up the pretty pop up
        NewItem2(event,"http://sp2010dev:1234/Lists/Time/NewForm.aspx?IssueID=" + issueID);
        //use this to open a new window
        //window.location="http://sp2010dev:1234/Lists/Time/NewForm.aspx?IssueID=" + issueID;
    }
</script>

Solution 2 – DataFormWebPart and exact same 2007 Process

This solution is a little more of a hack, but it also MUCH more close to the process we did in SP 2007. So, if you don’t mind not having the pretty pop-up and prefer the comforts of what you are used to, you can give this one a try.  The basics steps are:

  1. Insert a DataFormWebPart instead of the List Data View
  2. Create a Parameter on DataFormWebPart to store “ID” Query String Variable
  3. Filter DataFormWebPart using Parameter
  4. Insert a link at bottom of DataForm Web part that points to the Child’s new item form and passes in the Parent Id using the Parameter.

See.. like I told you, exact same process as in 2007 (except using the DataFormWeb Part). The DataFormWebPart also requires a lot more work to make it look “pretty” but it’s just table rows and cells, and can be configured pretty painlessly.  Here is that video:

Using DataForm Web Part

One quick update… if you change the link in this solution from:

<tr>
      <td><a href="http://sp2010dev:1234/Lists/Time/NewForm.aspx?IssueID={$IssueIDParam}">Click here to create new item...</a>
      </td>
</tr>

to:

<tr>
     <td>
          <a href="javascript:NewItem2(event,'http://sp2010dev:1234/Lists/Time/NewForm.aspx?IssueID={$IssueIDParam}');">Click here to create new item...</a>
     </td>
</tr>

It will open up in the pretty pop up and act the same as solution one… So… both Solutions will now behave the same to the end user. Just depends on which you want to implement.

That’s all for now…

Remember in both solutions when you have them working, you can make the “IssueID” invisible to users by using the “ms-hidden” class (it’s my previous blog post on the subject up there).

That’s basically all there is to it! No pithy or witty closing this time… I am sorry it took me so long to dive into this and I hope your questions are answered. As I become more polished myself I will try to come up with a cleaner solution that will make everyone happy… Smile

As always, thanks for taking the time to stop by.

© Geeks with Blogs or respective owner