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

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.

posted @ Thursday, December 16, 2010 6:12 PM
Print

Comments on this entry:

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Dan Usher at 12/16/2010 6:41 PM
Gravatar
Very cool video! Definitely appreciate the live videos to give an idea step by step of how to pull something like this together quickly and easily.

If anything the only thing I would recommend is not to eat for 12 hours. :)

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Matt Bramer at 12/16/2010 7:43 PM
Gravatar
Thanks for putting this together. I use the 2007 method heavily and I am happy to see this working in 2010. I think with your 2nd method, you can create a custom edit form and redirect to that instead. Then during your PreSaveAction, you can auto close the page. Still not as good as other solutions, but we haven't said the magic words yet: jQuery ;-)

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mark at 12/16/2010 7:57 PM
Gravatar
Thanks Matt, and like I said in the little blurb after the second video, you can make it function just like the first solution if you call the same "NewItem2" function instead of doing a straight link...

And yes... all of this is before doing any jQuery... that opens up a whole other world of possibilities! (or can of worms, depending on how you look at it)

thanks again for the comments...
Mark

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Andy at 1/1/2011 1:18 PM
Gravatar
I've followed carefully your instructions but for some reason cannot get the IssueID to set to 1. Not sure if there is something obvious but I can use the ../lists/Time/NewForm.aspx?Title=1 to set the title field but if I use ../lists/Time/NewForm.aspx?IssueID=1 the field remains empty, likewise any other non "Title" field. - Any thoughts?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Les at 1/2/2011 12:21 PM
Gravatar
When I try to do this, and add a new item, I receive the following error.

Failed to get value of the "Attachments" column from the "Attachments" field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)..

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mark at 1/3/2011 9:29 AM
Gravatar
Les, I have not seen this issue before. At what point does it happen? Before or after you click the button to save the new item? Have you tried starting over from scratch in case you missed a step?

good luck!
Mark

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Tony at 1/5/2011 4:49 AM
Gravatar
Hi, I got the same error as Les. Failed to get value of the "Attachments" column ... It pops up in a window after I click the save button. Nothing is saved.

Very disappointing, I was so pleased that I was able to follow your instructions so successfully, and to have it fail on the last step. Your instructions are great, by the way. nice level of detail about doing something that's so useful.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Andy at 1/5/2011 1:51 PM
Gravatar
Les Tony Mark, I had this problem go into sharepoint designer under settings (bottom left) uncheck "Allow attachments" that cleared the problem for me.
Mark
Unfortunately I still cannot resolve my problem I've checked and the field is a textbox just cannot seem to set any field other than title which is really frustrating.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Tony at 1/6/2011 1:20 AM
Gravatar
I tried Andy's suggestion and it worked. Thank you.

Thanks again, Mark, for your videos and this blog. I think that was pretty sophisticated functionality that you were able to teach to a non-expert.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by JerryJ at 1/7/2011 12:34 PM
Gravatar
Great topic. I want to do the same thing (solution 1) but with a file upload. How would the solution differ? In other words I have a list library with a child relationship to a doc library, I am adding a doc, not an item. Thanks....

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by JerryJ at 1/10/2011 11:29 AM
Gravatar
Ok, I get stuck at the javascript link. I even did your tests list scenario and still get an object not defined error in IE. The receiving page variable checks out... /Lists/Time/NewForm.aspx?IssueID=1

Error:
Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; FDM; .NET4.0C; .NET4.0E)
Timestamp: Mon, 10 Jan 2011 17:27:51 UTC


Message: Object expected
Line: 1
Char: 1
Code: 0
URI: http://xxxxxx/sites/taxworkflow/Lists/Issue/DispForm.aspx?ID=1&Source=http%3A%2F%2Fslnsps001%2Fsites%2Ftaxworkflow%2FLists%2FIssue%2FAllItems%2Easpx&ContentTypeId=0x0100DDAE59D0AB50024A94FD027E3D4EBA68&IsDlg=1



# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by JerryJ at 1/10/2011 1:54 PM
Gravatar
Which java library should I use fro SP 2010?
Do I have then add a pointer tag inside your example?

(tell I'm not a coder)
Thanks for all your input

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by tyler at 1/19/2011 9:51 AM
Gravatar
For solution 1, how can I get this to work with the lookup column rather than the textbox?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by orlandp at 2/1/2011 8:33 PM
Gravatar
Followed instructions twice, including all naming conventions. It appears the javascript inside the Content Editor is not running the function. I can get an alert to display before the function code, but not within. If I copy the http address into the browser with a IssueID hardcoded, it fills in the related field. I tried hardcoding the IssueID in the function, but related textbox does not autofill. Any ideas?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by orlandp at 2/1/2011 10:09 PM
Gravatar
It works. Didn't realize the block of script I copied to the Content Editor did not have the link to the new form. Sorry for the trouble.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by orlandp at 2/2/2011 7:41 PM
Gravatar
New problem: I have tried to apply the videos to my own lists. If I Insert a Related Item View to the DispForm.aspx, I get an "unexpected" error message about "Web Parts Maintenance Page" when I click on the title. I then tried creating a new "custom" DispForm.aspx, but when I tried to Insert a Related Item View, there was NOTHING related. I copied the code (from when I inserted it into the default DispForm.aspx) into the custom Display form. There was no error, but the view displays all the items in the child list. I suppose because I copied the code into the Display form rather than inserting it. I would appreciate any HELP.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by orlandp at 2/4/2011 11:55 AM
Gravatar
Had problems when I created parent list from a content type based on Event. When I manually added all the same fields to a custom content type based on Item, it worked. The fields Reccurrence, All Day Event & Workspace were not added, so I don't know if these fields were the ones causing the problems.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Ronald at 3/4/2011 3:38 AM
Gravatar
When i try to do this for a documentlibrary i have the problem with that there is no newform. I have do al the staps for the edit form but this not work.

Please help me for a solution.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by vincent at 3/8/2011 8:40 AM
Gravatar
that was very helpfull. thank you very much.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Eric at 3/12/2011 10:48 PM
Gravatar
Great stuff.

Here's the bit of Javascript needed for the first example to have something to click on that kicks off the script. Add this to the end of the code sample provided above.

Click here to create new item...

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Eric at 3/12/2011 10:50 PM
Gravatar
Is there a way to have the Issue NewForm in the example above NOT have the "+ Add Item" link? There's already out custom link that brings forward the ID. I'd rather not have two.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Michal at 3/21/2011 9:38 AM
Gravatar
Thank you for very useful solution! It works fine, but I've experienced some strange problems with removing related list items.

I have two lists - hospitals (Issues in your sample) and hospital employees (Time in your sample). When I remove employee using context menu for employees list, then I'm redirected to the list of all hospitals (not back to the hospital item view) and this list is inside of a popup. Doing same thing from employee editing form redirects to the list of all hospitals, this time not in popup.

Both behaviours are not correct - I would like to go back to the hospital page. Any ideas how to solve this problem?

Thanks in advance!

Michal

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Henry at 3/28/2011 4:36 AM
Gravatar
Perfect solution for lists! I'm very happy with it (working with solution 1). Still have two questions which are also mentioned in the above posts. I hope you have the answer on one of the questions:

1) I would like to use this solution for document libraries but this doesn't work (like Ronald mentioned above).
2) Because it didn't work for document libraries I thought to be clever and created a list where people could upload documents in. However when 'Allow Attachments' is activated it doesn't work (like Andy mentioned above).

Thanks in advance!
Henry

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Warren elkins at 4/11/2011 10:21 AM
Gravatar
I like you solution but I can not get it to work. I have followed your instruction videos to the letter and still receive this error:
Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
Timestamp: Mon, 11 Apr 2011 15:13:15 UTC


Message: 'object' is undefined
Line: 969
Char: 4
Code: 0
URI: http://adwbewinser2008/Lists/Issue/DispForm.aspx?ID=1&Source=http%3A%2F%2Fadwbewinser2008%2FLists%2FIssue%2FAllItems%2Easpx&ContentTypeId=0x0100664DDE938D62A141B2DB5BAD5E72CD3B&IsDlg=1

Also when I add the Related Item View for Time it puts a line in that says "Could not open page javascript;."

Any ideas. i really need to get this to work for me. Please respond.

Thanks,
Warren

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Warren elkins at 4/11/2011 10:50 AM
Gravatar
Just figured the whole thing out by placing alerts in the code. I did not have a capital "O" on "object" in the new Object line.

I feel foolish. Love this solution. Just what I needed.

Warren

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Maaike at 5/9/2011 7:56 AM
Gravatar
Thank you very much for sharing, it is very helpful. Can you give an example of how to pass multiple parameters through the querystring in Javascript?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by kwasi at 5/26/2011 10:07 AM
Gravatar
I have created a similar solution following your steps. I now new to move the solution from Dev to Prod. saving them as .stp files. When I opened the solution in sharepoint Designer, the child is not showing. The web part doesn't show up either. Any idea to move the solution from one computer to another?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Michal M. at 6/7/2011 7:25 AM
Gravatar
The problem with attachements is that it gets in conflict with default page form (which is hidden). Delete it and everything works ok.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Michal M. at 6/7/2011 8:01 AM
Gravatar
Another little change is that I did not bother to add DataFormWebPart. Instead I just put link to a parent form with paramID={@ID}.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Michal M. at 6/7/2011 8:28 AM
Gravatar
Sorry for third comment in a row but I hope it will help. Someone has asked how to hide +Add Item link. You can do that by change:
<Toolbar Type="Standard"/>
to
<Toolbar Type="None"/>
In "Time" webpart code.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Shane R. at 6/10/2011 12:07 PM
Gravatar
Awesome guide and video. I'm using this for a project that has been troubling me for a while.

I'm trying to apply this concept to a document library with document or document set content types. I am getting the modified form to load correctly, but my values are not getting saved. Any ideas?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by gordon at 6/21/2011 4:06 AM
Gravatar
I try your method in my image library,but it's not ok.I feel something wrong with the textchanged event.
For Example:
I have a issue screen capture library,and i use the query string bind the issueid,i upload a picture in issue list,but the issueID in library always nothing,I open the editform.aspx of the library,i can see the query screen effect,because the textbox can receive the url parameter.I find something strange when i opened editform.aspx,I can change the value of issueid.
Maybe i use the editform.aspx,because i didn't find newform.aspx in library.

Do you have any good advice?

Sorry for my bad english.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mauricio Morgado at 7/21/2011 2:57 PM
Gravatar
Thank you so much! Specially for Michael M, as i been trying to find a way to get the attachments to work for 2 days now!

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Rovonda Fleming at 7/25/2011 10:50 AM
Gravatar
Can we do this for large lists? If so how would one go about doing so?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Jamie Carruthers at 7/26/2011 10:37 AM
Gravatar
This is exactly what I was after. Thanks very much. Works a treat

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Justin Vorous at 1/4/2012 10:38 AM
Gravatar
Your videos are great! Thanks alot! I do have one problem though -- the Related Item View is not filtering for the Time for the specific Issue selected. The Item View just shows ALL the items on the Time List.

your seemed to do it automatically, am I missing something?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Justin Vorous at 1/5/2012 1:31 PM
Gravatar
Thanks for your wonderful blog posts! I'd have not gotten anywhere without them. I have a question though: I can get this solution to work perfectly for my Projects and Tasks list -- when a new Task is created the Project ID autopopulates....but I also want it to autopopulate the Project Name (the "Title" field in the Project List).

How can I pass multiple parameters using the javascript in the CEWP? I've tried using &amp; %26 and some other methods but nothing works. Can we only pass one query string paramter with this method?

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Justin Vorous at 1/5/2012 3:22 PM
Gravatar
never mind -- I used your method to add the Parent item ID to the child list and then a workflow on the child list to populate the Parent Name that matches that ID. This removes the need to pass multiple parameters and works like a charm.

Thanks again! :)

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Kurt Henderson at 1/9/2012 1:50 PM
Gravatar
Hi Mark, I was finally able to get this to work but when I when to turn the tool bar off to get rid of the + Add new item, it actually changed the child web part to a "ListViewWebPart" which lost its related item settings and filter. After messing with it for a while, I was able to recreate the filter by adding the OrderID (link field between parent and child) to the view and recreating the filter. Although this works, it changed the view of the child items and added an icon which would allow the user to change the filter. Not a good idea. Any suggestions? Also, do you do consulting work where someone could pay you to do specific things like set up the parent/child web form? Thanks.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Venkat at 1/19/2012 12:02 AM
Gravatar
Mark at 1/3/2011 9:29 AM
Tony at 1/5/2011 4:49 AM
Andy

Hi Guys
I got the same " Attachments" error.

Andy's note: sharepoint designer under settings (bottom left) uncheck "Allow attachments" that cleared the problem for me.
Mark

I couldn't find the settings option. (I am using SharePoint 2010 and SPD2010).
Could you please elaborate the steps to find easily.

Cheers
Venkat

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mark at 1/19/2012 7:46 AM
Gravatar
Check out my updated blog on the subject where I accomplish the same functionality without using SPD Venkat. That should clear up your issue.

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

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

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Debi at 3/9/2012 12:17 PM
Gravatar
Same issue as Andy
cannot set IssueID to 1, I can set title but no other field

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mark at 3/9/2012 1:57 PM
Gravatar
Make sure that you are using {$IssueID} and NOT {@IssueID} when setting the field.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Craig at 4/16/2012 8:44 AM
Gravatar
Has anyone figured out a way to set up a parent/child list relationship as described with the above steps, but also was able to include attachments?

I am getting that attachments error as many have stated above, but I cannot turn this off as I need attachments as part of the lists.

Anyone successfully achieve this?

Thanks.

# re: Creating A SharePoint Parent/Child List Relationship– SharePoint 2010 Edition

Left by Mark Rackley at 4/16/2012 8:47 AM
Gravatar
If you go to this blog post, I duplicate the same functionality using only jQuery. This approach will not affect attachments in any way.

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

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789