0

I have a scenario where I have a SPListItemCollection of SharePoint 2013 and I have to bind tree view with XMLDataSource. I have to add nodes to XML and iterate to add child nodes also but all these things has to be performed on run time only. Below is my .ascx code where I have both the controls( tree view and XMLDataSource):

<asp:TreeView ID="tvKnowledgeTypeDetail" runat="server" ShowLines="true" DataSourceID="XMLDataSource1"> </asp:TreeView> <asp:XmlDataSource ID="XMLDataSource1" runat="server" EnableCaching="false" /> 

and now the code to get collection of documents from a document library:

SPListItemCollection itemColl = null; string strQuery = string.Empty; List<string> lstViewFieldColumns = new List<string>(); 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> distinctCoverageGrp = (from SPListItem items in itemColl select items["CoverageGroupLookup"].ToString()).Distinct().ToList(); foreach (string coverageGrp in distinctCoverageGrp) { //add to XML (How?) } 

Now, I have a generic list where I have distinct "CoverageGroup" and I have to add it to XML within a foreach loop and then get distinct of "Sectors" and add sectors to specific Coverage group parent node in XML.If any one have any idea that how to add nodes to XML and then bind it then please provide me the steps as I am new for tree view.

    1 Answer 1

    0

    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.