I have resolved the above issue and now i am able to bind my tree view control to xml data source. Below is the code:
private void BindTreeView() { SPListItemCollection itemColl = null; string strQuery = string.Empty; List<string> lstViewFieldColumns = new List<string>(); lstViewFieldColumns.Add("DocumentTitle"); lstViewFieldColumns.Add("CoverageGroupLookup"); lstViewFieldColumns.Add("SectorLookup"); lstViewFieldColumns.Add("KnowledgeTypeLookup"); strQuery = "<Where><Eq><FieldRef Name='KnowledgeTypeLookup' /><Value Type='Lookup'>Research Manuals</Value></Eq></Where>"; using (KnowledgeDocumentBLL knowledgeDocumentBLL = new KnowledgeDocumentBLL()) { itemColl = knowledgeDocumentBLL.GetKnowledgeDocumentCollection(strDocumentLibraryName, strQuery, lstViewFieldColumns); } List<string> distinctCoverageGrpList = (from SPListItem items in itemColl select items["CoverageGroupLookup"].ToString()).Distinct().ToList(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<KnowledgeType Name='CompanyProfile'></KnowledgeType>"); foreach (string coverageGrp in distinctCoverageGrpList) { SPFieldLookupValue coverageGrpLookup = new SPFieldLookupValue(Convert.ToString(coverageGrp)); XmlNode rootNode = doc.SelectSingleNode("KnowledgeType"); XmlNode parentNode = doc.CreateNode(XmlNodeType.Element, "CoverageGroupLookup", ""); XmlAttribute xmlAttributeName = doc.CreateAttribute("Name"); xmlAttributeName.Value = coverageGrpLookup.LookupValue; XmlAttribute xmlAttributeValue = doc.CreateAttribute("Value"); xmlAttributeValue.Value = coverageGrpLookup.LookupId.ToString(); parentNode.Attributes.Append(xmlAttributeName); parentNode.Attributes.Append(xmlAttributeValue); ////Append Sector node to CoverageGroup List<string> distinctSectorList = (from SPListItem items in itemColl where items["CoverageGroupLookup"].ToString() == coverageGrp select items["SectorLookup"].ToString()).Distinct().ToList(); foreach (string sector in distinctSectorList) { SPFieldLookupValue sectorLookup = new SPFieldLookupValue(Convert.ToString(sector)); XmlNode childNode = doc.CreateNode(XmlNodeType.Element, "SectorLookup", ""); XmlAttribute xmlChildAttributeName = doc.CreateAttribute("Name"); xmlChildAttributeName.Value = sectorLookup.LookupValue; XmlAttribute xmlChildAttributeValue = doc.CreateAttribute("Value"); xmlChildAttributeValue.Value = sectorLookup.LookupId.ToString(); childNode.Attributes.Append(xmlChildAttributeName); childNode.Attributes.Append(xmlChildAttributeValue); parentNode.AppendChild(childNode); } rootNode.AppendChild(parentNode); } XMLDataSource1.Data = doc.InnerXml; XMLDataSource1.DataBind(); tvKnowledgeTypeDetail.DataBind(); }
and source code to bind it is as below:
<asp:TreeView ID="tvKnowledgeTypeDetail" runat="server" ShowLines="true" DataSourceID="XMLDataSource1" OnSelectedNodeChanged="tvKnowledgeTypeDetail_SelectedNodeChanged" OnTreeNodeDataBound="tvKnowledgeTypeDetail_TreeNodeDataBound" > <HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" /> <NodeStyle Font-Names="Verdana" Font-Size="12pt" ForeColor="Black" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="5px"></NodeStyle> <ParentNodeStyle Font-Bold="False" /> <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor="#DD5555" /> <DataBindings> <asp:TreeNodeBinding DataMember="CoverageGroupLookup" ValueField="Value" TextField="Name" Depth="1" /> <asp:TreeNodeBinding DataMember="SectorLookup" ValueField="Value" TextField="Name" Depth="2" /> </DataBindings> </asp:TreeView> <asp:XmlDataSource ID="XMLDataSource1" runat="server" EnableCaching="false"/>
Now, one more issue is that how can i make a tree node clickable at runt time only if it has no child node (if the node is leaf node). I have tried with the below code to test with the tool tip attribute and its working fine:
protected void TreeNode_Clickable(TreeNode node) { if (node.ChildNodes.Count == 0) { node.ToolTip = "Leaf Node"; } else { node.ToolTip = "Parent Node"; } }
I want to make a node clickable if its a leaf node only otherwise not clickable. If anyone has did it before, then please suggest.