Monday 17 December 2012

Show SharePoint 2010 document library hierarchy in a tree view

I have followed this link. but having one issue.
So I have eliminated the issue. Now the code will look like as follows:

private SPTreeView myTreeView;
        private SPHierarchyDataSourceControl myDataSource;
        protected override void CreateChildControls()
        {
            try
            {
                SPDocumentLibrary docLib = GetDocLib(SPContext.Current.Web.Url, "Test Library");
             
                myDataSource = new SPHierarchyDataSourceControl();
                myDataSource.ID = "myTreeViewDataSource";
                myDataSource.RootContextObject = "Web";
                myDataSource.IncludeDiscussionFolders = false;
                myDataSource.ShowDocLibChildren = true;
                myDataSource.ShowFolderChildren = true;
                myDataSource.ShowListChildren = true;
                myDataSource.ShowWebChildren = false;              
                myDataSource.Web = SPContext.Current.Web;
                myDataSource.RootWebId = SPContext.Current.Web.ID.ToString();
                myDataSource.RootContextObject = null;
                myDataSource.RootListId = docLib.ID.ToString();
                Controls.Add(myDataSource);

                myTreeView = new SPTreeView();
                myTreeView.ID = "myTreeView";
                myTreeView.DataSourceID = myDataSource.ID;
                myTreeView.EnableClientScript = true;
                myTreeView.EnableViewState = true;
                myTreeView.NodeIndent = 12;
                myTreeView.NodeStyle.CssClass = "ms-navitem";
                myTreeView.NodeStyle.HorizontalPadding = 2;
                myTreeView.SelectedNodeStyle.CssClass = "ms-tvselected";
                myTreeView.ExpandDepth = docLib.Folders.Count;
                myTreeView.ExpandImageUrl =
                SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/tvplus.gif");

                myTreeView.CollapseImageUrl =
                SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/ tvminus.gif");

                myTreeView.NoExpandImageUrl =
               SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/ tvblank.gif");

                myTreeView.SkipLinkText = "";
                myTreeView.ShowLines = true;
                Controls.Add(myTreeView);
            }
            catch
            {

            }
        }

Now its working fine.

No comments :

Post a Comment