locked
Build XML for Custom Nested Accordian (like Tree View Structure) for SharePoint List Data RRS feed

  • Question


  • Expected output in Xml:

    <?xml version="1.0" encoding="utf-8" ?>
    - <TopRoot>
    - <Root id="1" Name="Department">
    - <Type id="2" Name="IT">
    - <SubType id="3" Name="Technology">
      <SubSubType id="4" Name="Sharepoint" />
      <SubSubType id="5" Name="ASP.NET" />
      <SubSubType id="6" Name="HTML 5" />
      </SubType>
      </Type>
    -
    </Root>
    -
    </TopRoot>

    List Details:

    list details for storing category / sub category data and code to build tree structure for the same.

    1.Create Custom List named “CategoryDetails”:
    2.Create Column “Category Name” of type single line of text. Make it as required field and check Yes for Enforce Unique values.
    3.Create column “Parent Category” of type lookup. under Additional Column Settings.
    Get information dropdown, select “CategoryDetails”.
    4.Choice column ["SRTypeName"] 1.Root,2.SRTYPE,3.SubSRTYPE, 4.SUBSUBSRTYPE
    In this column dropdown, select “Category Name”:  

    Referance:
    http://www.codeproject.com/Tips/627580/Build-Tree-View-Structure-for-SharePoint-List-Data    -fine but don't want tree view just generate xml string
    i just follwed above link it work perferfectly fine for building tree view but i don't want server control.

    Expected Result:
    My ultimate goal is to generate xml string like above format without building tree view.
    I want to generate xml using web service and using xml i could convert into nested Tree View Accordian in html.
    I developed some code but its not working to generate xml /string.


    My modified Code:
    public const string DYNAMIC_CAML_QUERY =
            "<Where><IsNull><FieldRef Name='{0}' /></IsNull></Where>";

            public const string DYNAMIC_CAML_QUERY_GET_CHILD_NODE =
            "<Where><Eq><FieldRef Name='{0}' /><Value Type='LookupMulti'>{1}</Value></Eq></Where>";

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                 string TreeViewStr= BuildTree();

                 Literal1.Text = TreeViewStr;
                }
            }

            StringBuilder sbRoot= new StringBuilder();
            protected string BuildTree()
            {
                SPList TasksList;
                SPQuery objSPQuery;
                StringBuilder Query = new StringBuilder();
                SPListItemCollection objItems;
                string DisplayColumn = string.Empty;
                string Title = string.Empty;
                string[] valueArray = null;
               
                try
                {
                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {

                            TasksList = SPContext.Current.Web.Lists["Service"];
                            if (TasksList != null)
                            {

                                objSPQuery = new SPQuery();
                                Query.Append(String.Format(DYNAMIC_CAML_QUERY, "Parent_x0020_Service_x0020_Id"));
                                objSPQuery.Query = Query.ToString();
                                objItems = TasksList.GetItems(objSPQuery);
                                if (objItems != null && objItems.Count > 0)
                                {
                                
                                    foreach (SPListItem objItem in objItems)
                                    {
                                        DisplayColumn = Convert.ToString(objItem["Title"]);
                                        Title = Convert.ToString(objItem["Title"]);
                                        int rootId=objItem["ID"].ToString();
                                        sbRoot.Append("<Root id="+rootId+" Name="+Title+">");
                                        string SRAndSUBSRTpe = CreateTree(Title, valueArray, null, DisplayColumn, objItem["ID"].ToString());
                                        sbRoot.Append(SRAndSUBSRTpe);
                                        SRType.Clear();//make SRType Empty
                                        strhtml.Clear();
                                    }
                                    SRType.Append("</Root>");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return SRType.ToString();
            }
            
             StringBuilder strhtml = new StringBuilder();
            private string CreateTree(string RootNode, string[] valueArray,
          List<SPListItem> objNodeCollection, string DisplayValue, string KeyValue)
            {
               
                try
                {
                   
                    strhtml.Appends(GetSRType(KeyValue, valueArray, objNodeCollection);
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return strhtml;
            }

            StringBuilder SRType = new StringBuilder();
            private string GetSRType(string RootNode,
            string[] valueArray, List<SPListItem> objListItemColn)
            {
              
                SPQuery objSPQuery;
                SPListItemCollection objItems = null;
                List<SPListItem> objNodeListItems = new List<SPListItem>();
                objSPQuery = new SPQuery();
                string objNodeTitle = string.Empty;
                string objLookupColumn = string.Empty;
                StringBuilder Query = new StringBuilder();
                SPList objTaskList;
                SPField spField;
                string objKeyColumn;
                string SrTypeCategory;
                try
                {
                    objTaskList = SPContext.Current.Web.Lists["Service"];
                    objLookupColumn = "Parent_x0020_Service_x0020_Id";//objTreeViewControlField.ParentLookup;
                    Query.Append(String.Format
                    (DYNAMIC_CAML_QUERY_GET_CHILD_NODE, objLookupColumn, RootNode));
                    objSPQuery.Query = Query.ToString();
                    objItems = objTaskList.GetItems(objSPQuery);
                    foreach (SPListItem objItem in objItems)
                    {
                        objNodeListItems.Add(objItem);
                    }

                    if (objNodeListItems != null && objNodeListItems.Count > 0)
                    {
                       
                        foreach (SPListItem objItem in objNodeListItems)
                        {               
                            RootNode = Convert.ToString(objItem["Title"]);
                            objKeyColumn = Convert.ToString(objItem["ID"]);
                            objNodeTitle = Convert.ToString(objItem["Title"]);
                            SrTypeCategory= Convert.ToString(objItem["SRTypeName"]);

                           if(SrTypeCategory =="SRtYpe")
                           {

                              SRType.Append("<Type  id="+objKeyColumn+" Name="+RootNode+ ">");  
                             if (!String.IsNullOrEmpty(objNodeTitle))
                            {
                              SRType.Append(GetSRType(objKeyColumn, valueArray, objListItemColn));
                         
                             }
                          }

                          if(SrTypeCategory =="SRSubTYpe")
                           {

                              SRType.Append("<SRSubType  id="+objKeyColumn+" Name="+RootNode+ ">");  
                             if (!String.IsNullOrEmpty(objNodeTitle))
                            {
                              SRType.Append(GetSRType(objKeyColumn, valueArray, objListItemColn));
                         
                             }
                          }

                          if(SrTypeCategory =="SubSubTYpe")
                           {

                              SRType.Append("<SubSubType  id="+objKeyColumn+" Name="+RootNode +" ></SubSubType");  
                           }                     
                

                         }
                        SRType.Append("</SubType>");
                        SRType.Append("</Type>");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return SRType.ToString();
                // Call method again (recursion) to get the child items
            }

    Wednesday, August 27, 2014 4:23 AM