Sharepoint SIG

Thoughts and ideas from your SharePoint support team

Parent-Child relationship between two lists in SharePoint – Part 2

| 1 Comment

In a previous blog post “Parent-Child Relationship between two lists in SharePoint” , I listed the instructions to create a form that displays information from a parent record and allows you to view the list of child records attached to it.  For example, the parent record could be an invoice and the child records could be the items listed on the invoice.  These instructions work, but when you create a new child record, it does not automatically put the parent’s ID value into the child record.  This is complicated enough that I decided to write another blog post about the subject.

These instructions assume that you have already read the previous post and set up the Parent and Child lists according to those instructions.  You must have SharePoint Designer privileges to make this work; if you do not, please contact your site administrator.  I recommend that you have some experience with SharePoint Designer before attempting this as it is easy to cause enormous damage.

Step 1:  Decide whether you want the Child list to allow files to be attached to list items.  If you do not, disable attachments now (this will make your life easier later on).

  1. Go to the Child list
  2. Click on the List tab
  3. Click on the List Settings button at the right of the ribbon
  4. Click on the “Advanced Setting” link
  5. In the Attachments section, choose “Disabled”
  6. Click OK.

Step 2:  Set up the Child list

  1. Start up SharePoint Designer.
  2. Click on “Lists and Libraries”
  3. Click on your child list (e.g. Invoice Details).
  4. In the Forms area at the right, click on “NewForm.aspx”; the code for the form will be displayed.  If you do not see the code at the top and the design at the bottom, click on the icon “Split” in the View ribbon.
  5. Click on the default web part to select it (it will have a tag “WebPartPages:ListFormWebPart” at the top).
  6. In the Code window above, look for the line “<IsVisible>true</IsVisible>”; change true to false.
  7. Now create a new data entry form to replace the hidden one.  Click in the line below the web part.
  8. Click on the Insert tab in the ribbon.  From the SharePoint icon’s drop-down, choose “Custom List Form”.
  9. Choose the Child list as the list or library to use for the form.
  10. If there are multiple content types, choose the content type that displays the list of fields that you want to view.
  11. Leave the type of form as New and click OK.
  12. A new form will be inserted with a list of all of the fields in the Child record, including the Parent ID field.
  13. In the Options tab in the ribbon, click on the Parameters icon.
  14. Click “New Parameter”
  15. Type in a name for the parameter, such as “ThisParentID”.
  16. Change the Parameter Source from “None” to “Query String”.
  17. Type in a query string variable name, such as “ParentID”.  Remember this name for use when setting up the Parent form.
  18. Click OK.
  19. Click on the Parent ID field.  The field will be highlighted and there will be a box with a “>” in it to the right of the field.
  20. Click on the “>”.  From the pop-up, change the Format from “List Form Field” to “Text Box”.
  21. The code for the field will be highlighted in the code window.  Look for ‘text=”{@ParentID}”‘ (or whatever you called the parent ID field when you created it using the previous blog post’s instructions).
  22. Change the @ParentID to $ThisParentID; this will bring in the correct value from the parameter that you just created.
  23. If you don’t want the Parent ID row to appear in the form, locate the opening <tr> for the row and insert style=”display:none”  ( e.g. <tr style=”display:none”>).  The row will appear in SharePoint Designer but will be hidden when you are viewing the form in SharePoint.
  24. Save your changes.

Step 3:  Set up the Parent list

  1. Below this section of instructions there is some Javascript code.  Copy the code and paste it into a text editor such as Notepad.
  2. Change the text PATH_TO_CHILD_LIST to the URL for the child list.  To get the URL, navigate to the child list and copy the URL from the browser , but only copy up to the end of the name of the child list (e.g. don’t copy the “AllItems.aspx” portion of the URL).
  3. Change the text QUERY_STRING_VARIABLE to the name that you chose in step 17 of the previous list of instructions.
  4. If desired, change “Click here to create new item . . .” to the text that you want.
  5. Save the Javascript to a text file, using “.js” as the extension (e.g. NewItemScript.js).
  6. Return to your SharePoint site.
  7. Click on the “Site Actions” menu and choose “View All Site Content”.
  8. Click on “Site Assets”.
  9. Upload the Javascript file into the Site Assets library.
  10. Copy the URL to the Site Assets library, up to the text”Forms/AllItems.aspx”.
  11. Go to your Parent list.
  12. Click on the List tab.
  13. From the “Form Web Parts” drop-down, choose “Default Display Form”.  You should see the display form including the Related Items form for the Child record that you inserted in the previous blog post’s instructions.
  14. Click the box “Add a new Web Part”. A list of the categories will appear at the left
  15. Click on “Media and Content”, then on “Content Editor”.  Click the Add button.
  16. The Content Editor will appear at the top of the screen.  Drag it down below the Related Items.
  17. Place the mouse cursor on the Content Editor’s header.  You should see a black triangle appear at the right hand side of the Content Editor header.
  18. Click on the triangle and choose “Edit Web Part”.  The Content Editor’s properties will appear to the right.
  19. Paste the URL for the Site Assets library into the Content Link field.  Follow it with the name of the Javascript file including the “.js” extension.
  20. Click “Test Link” betow the field.  If the URL is correct, SharePoint will pop up a message asking if you want to save the file.  If it is not correct, you will see the standard “The webpage cannot be found” screen; go back and check the URL for mistakes.
  21. Click the Apply button and then the OK button.
  22. In the Related Items web part, click on the triangle to the right.
  23. Choose “Edit Web Part”.
  24. Change the Toolbar type from Full Toolbar to No Toolbar.  This hides the default “Add New Item” link, forcing people to use the new link in the Content Editor Web Part.
  25. Click on the Page tab in the ribbon and then on Stop Editing icon to save changes to the page.
  26. Go to your parent list and add a new parent record.
  27. Click on the new record’s name to open the display form and check that the new “Add new item” link works.

 

Javascript below:


<script type="text/javascript">


//Callback function to execute the Reload after the New Form has been closed
function MyCallback(dialogResult, returnValue)
{
document.location.reload(true);
}


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 ParentID = vals["ID"];


var options= {
url: 'PATH_TO_CHILD_FORM/NewForm.aspx?QUERY_STRING_VARIABLE=' + ParentID,
title: 'Add Issue',
dialogReturnValueCallback: MyCallback
};


//Use the ShowModalDialog method to display the New Form dialogue box
var dialogSP = SP.UI.ModalDialog.showModalDialog(options);
}</script><a href="javascript:NewTime()">Click here to create new item ...</a>

Step 4:  If you decided to allow files to be attached to list items, remove references to Attachments.  If you don’t, when you click the Save button to save a new item you will receive an error and you will not be able to save the new item.

  1. Start up SharePoint Designer by choosing “Edit in SharePoint Designer” from the Site Actions
  2. Click on “List and Libraries”
  3. Click on the name of the Child list
  4. In the Form section at the right, click on “NewForm.aspx”
  5. If you do not see the aspx code in a window at the top, click the View tab in the ribbon and then click either the Split or the Code icon at the left
  6. Click in the code window
  7. Press ctrl-F to open up the Find and Replace window.
  8. Search for the text “attach” and remove all references.
  9.  The first instance will be “@Attachments, Attachments;”; remove only that text and leave the rest of the line
  10. The next instance will be  “<SharePoint:AttachmentUpload runat=”server” ControlMode=”New”/>”; remove the entire line
  11. The last instance will be an entire Table Row; remove the whole section (see below).  When done, save the changes and exit SharePoint Designer.


<tr id="idAttachmentsRow">
<td nowrap="true" valign="top" width="20%">
<SharePoint:FieldLabel ControlMode="New" FieldName="Attachments" runat="server"/>
</td>
<td valign="top" width="80%">
<SharePoint:FormField runat="server" id="AttachmentsField" ControlMode="New" FieldName="Attachments" __designer:bind="{ddwrt:DataBind('i','AttachmentsField','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Attachments')}"/>
<script>
var elm = document.getElementById(&quot;idAttachmentsTable&quot;);
if (elm == null || elm.rows.length == 0)
document.getElementById(&quot;idAttachmentsRow&quot;).style.display=&apos;none&apos;;
</script>
</td>
</tr>

If you removed the Attachments code as detailed in step 4, note that even though there is an “Attach File” icon in the ribbon when you are creating a new Child item, it won’t work properly.  If you want to add an attachment to an item in the list, you have to create the item first and save it, then click its name in the list.  This opens up the Edit form rather than the New form, and the “Attach File” icon will work as you expect.

Sheila ffolliott

Analyst

University of Saskatchewan