Tuesday, May 5, 2009

Cascading with Parent Child Relation

Cascading with Parent Child Relation
Problem Scenario:-
1) Create Two Custom Cascading List (Parent and Child)
2) In Parent List Add one Column Called Father









Add few Items to this Parent List






3) Now Create Child List and Add lookup Fathers Name Field from Parent List.



4) Add Parent and Child List Web Parts to main Page of you Site


5) Make connection of Parent List with Child List


6) Now if you select One of the Parent then it will show you respective Child

If we Select Amitabh then it will show his Children’s and Vice versa.



7) Click on New Item of Child List

.


8) It should select Fathers Name By Default instead of None




Solution:-

Create & Install the feature

Open the \TEMPLATE\FEATURES folder under the 12 hive
Create a folder called NewChildListMenuAction
In that folder, create 2 text files, one called feature.xml and the second elements.xml
In your editor of choice (notepad is fine) open the feature.xml file and paste into it the following:
9) feature.xml


<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="769826dd-9dd2-11db-96ca-005056c00008"
Title="Create Child List"
Description="This feature adds a Create Child in the New menu for Windows
SharePoint Services lists."
Version="1.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location=" elements.xml" />

</ElementManifests>

</Feature>


10) elements.xml

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/"&gt;
<!-- Add the action to the List Toolbar New Menu Dropdown
-->
<CustomAction
Id="SPSTIPS.
NewChildListActionsToolbar"


RegistrationType="List"


GroupId="NewMenu"


Location="Microsoft.SharePoint.StandardMenu"


Sequence="1000"


Title="New Child">


<UrlAction
Url="javascript:window.location=
'{SiteUrl}//Lists/child/NewForm.aspx?PageUrl=' + window.location
"/>


</CustomAction>


</Elements>


11) Install and Activate the feature
stsadm -o installfeature -name NewChildListMenuAction –force

stsadm -o activatefeature -name NewChildListMenuAction -url [your site url here] –force

12) Add Content Editor Web Part to NewForm.aspx

13) Note:-

· By default you don’t have option of modify webpart so use below command in url.

NewForm.aspx?PageView=Shared&ToolPaneView=2

14) Paste Following code in content editor WebPart.


<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
var vals = new Object();
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];
}
setLookupFromFieldName("Father", vals["SelectedID"]);
//field name and query string parameter
switchback("Father"); //field name to lock
}

function setLookupFromFieldName(fieldName, value) {
var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
if (theSelect == null) {
var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
ShowDropdown(theInput.id);

var opt=document.getElementById(theInput.opt);
setSelectedOption(opt, value);
OptLoseFocus(opt);
} else {
setSelectedOption(theSelect, value);
}
}

function setSelectedOption(select, value) {
var opts = select.options;
var l = opts.length;
if (select == null) return;
for (var i=0; i < l; i++) {
if (opts[i].value == value) {
select.selectedIndex = i;
return true;
}
}
return false;
}

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" tempString.indexOf
(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}

function getDropDownImg () {
var downArrow=document.getElementsByTagName('img')
for(var i=0; i < downArrow.length; i++) {
if(downArrow[i].alt ==('Display lookup values')) {
downArrow[i].onclick = fillDefaultValues;
}
}
}

function switchback(fieldName){
var switchSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
if (switchSelect == null) {
var switchInput = getTagFromIdentifierAndTitle("input","",fieldName);
switchInput.onmouseup = fillDefaultValues;
getDropDownImg();
} else {
switchSelect.onchange = fillDefaultValues;
}
}
</script>

15) Now you will get Create New Child menu
It will get selected id from Url and pass it to NewForm.aspx


16) On Newform.aspx we have added script in Content Editor Web Part which will bind proper Parent’s Lookup Value to the Dropdown.













No comments:

Post a Comment