using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
namespace TreeViewWebPart
{
class TreeViewToolPart : ToolPart
{
#region constant
public const string d_documentLibraryLabel = "Document Library";
public const string d_isFolderLabel = "Show folders only";
#endregion
#region Controls
DropDownList dropdownList;
CheckBox isFolderCheckBox;
#endregion
#region overriden methods
protected override void CreateChildControls()
{
base.CreateChildControls();
//Create Label
Label label = new Label();
label.ID = "label";
label.Text = d_documentLibraryLabel;
//Create dropdown
dropdownList = new DropDownList();
dropdownList.ID = "dropdownList";
//Getting the lists of the current web, and adding them in the dropdown-list.
SPListCollection docLibraryColl = SPContext.Current.Web.GetListsOfType(SPBaseType.DocumentLibrary);
foreach (SPList list in docLibraryColl)
{
dropdownList.Items.Add(list.Title);
}
//Add label and dropdown
Controls.Add(label);
Controls.Add(dropdownList);
label = new Label();
label.ID = "isFolderOnly";
label.Text = "</br>" + d_isFolderLabel;
//Create CheckBox
isFolderCheckBox = new CheckBox();
isFolderCheckBox.ID = "isFolderCheckBox";
Controls.Add(label);
Controls.Add(isFolderCheckBox);
TreeViewWebPart treeView = (TreeViewWebPart)this.ParentToolPane.SelectedWebPart;
//Assingning thr values to the toolpart
if (treeView != null)
{
this.dropdownList.SelectedValue = treeView.DocumentLibraryName;
this.isFolderCheckBox.Checked = treeView.IsFolderOnly;
}
}
#endregion
/// <summary>
/// Apply the Changes in the WebPart
/// Assign the SelectedValue as DocumentLibrary Name
/// </summary>
public override void ApplyChanges()
{
TreeViewWebPart treeView = (TreeViewWebPart)this.ParentToolPane.SelectedWebPart;
treeView.DocumentLibraryName = dropdownList.SelectedValue;
treeView.IsFolderOnly = isFolderCheckBox.Checked;
}
}
}
using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
namespace TreeViewWebPart
{
[ToolboxItemAttribute(false)]
public partial class TreeViewWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
//[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
TreeView treeView;
TreeNode rootNode;
#region member variable
private string m_ToolPartTitle = "Tree View Settings";
private string m_libraryName = String.Empty;
private Boolean m_isFolderOnly = false;
#endregion
#region constructor
public TreeViewWebPart()
{
}
#endregion
#region properties
/// <summary>
/// DocumentLibarary Name of Which the items has to appear
/// </summary>
[WebBrowsable(true)]
[Personalizable(PersonalizationScope.Shared)]
public string DocumentLibraryName
{
get
{
return m_libraryName;
}
set
{
m_libraryName = value;
}
}
/// <summary>
/// IsFolder property which signifies only folder to be shown while loading
/// </summary>
[WebBrowsable(true)]
[Personalizable(PersonalizationScope.Shared)]
public Boolean IsFolderOnly
{
get
{
return m_isFolderOnly;
}
set
{
m_isFolderOnly = value;
}
}
#endregion properties
/// <summary>
/// Oveeriden GetToolParts method
/// Adding the customToolPart for the Webaprt
/// </summary>
/// <returns></returns>
public override ToolPart[] GetToolParts()
{
ToolPart[] toolParts = new ToolPart[2];
WebPartToolPart standardToolParts = new WebPartToolPart();
TreeViewToolPart customToolParts = new TreeViewToolPart();
customToolParts.Title = m_ToolPartTitle;
toolParts[0] = standardToolParts;
toolParts[1] = customToolParts;
return toolParts;
}
/// <summary>
/// Overriden OnInit
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
/// <summary>
/// Overriden Page Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
///Overriden Render Contents
/// </summary>
/// <param name="writer"></param>
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
base.RenderContents(writer);
}
/// <summary>
/// Overriden Create Child Control
/// Creating a TreeView and adding items to it
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();
GetToolParts();
//Get the current site
SPSite currentSite = SPContext.Current.Site;
using (SPWeb currentWeb = currentSite.OpenWeb())
{
//Set the tree view properties
treeView = new System.Web.UI.WebControls.TreeView();
treeView.ShowLines = true; // show lines
treeView.ExpandDepth = 1; // expand first level
//Sanity check that DocumentLibraryName is set
if (!string.IsNullOrEmpty(DocumentLibraryName))
{
SPList list = SPContext.Current.Web.Lists[DocumentLibraryName];
// build the tree and adding the Document Library as the Root Node
rootNode = new System.Web.UI.WebControls.TreeNode(list.Title, "", "", list.RootFolder.ServerRelativeUrl.ToString(), "");
// Loop down the tree
GetAllFilesFolders(list.RootFolder, rootNode, true, IsFolderOnly);
// Add the root node to tree view
treeView.Nodes.Add(rootNode);
}
this.Controls.Add(treeView);
}
base.CreateChildControls();
}
/// <summary>
/// Gets All the File Folders Details recursively and add it to the tree
/// </summary>
/// <param name="spFolder"></param>
/// <param name="parentNode"></param>
/// <param name="IsCheckCurrentUser"></param>
/// <param name="IsOnlyFoldersNeedShow"></param>
protected void GetAllFilesFolders(SPFolder spFolder, TreeNode parentNode, bool isCurrentUser, bool isFolderOnly)
{
SPWeb spWeb = SPContext.Current.Web;
SPQuery spQuery = new SPQuery();
spQuery.Folder = spFolder;
string strCurrentUserName = spWeb.CurrentUser.Name.ToString();
//Display files and folders as isFolder is set to false
if (isFolderOnly != true)
{
SPListItemCollection spListItemCollection = spWeb.Lists[spFolder.ParentListId].GetItems(spQuery);
foreach (SPListItem spListItem in spListItemCollection)
{
// Checking whether the item is a file or a folder //
if (spListItem.Folder != null)
{
// Creating the node for the folder //
TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/images/folder.gif");
parentNode.ChildNodes.Add(treeNodeChildFolder);
// Calling the custom method to get all the subfolder and files within the folder //
GetAllFilesFolders(spListItem.Folder, treeNodeChildFolder, false, false);
}
else
{
// Creating and object of the file //
SPFile spFile = spListItem.File;
// Setting the display URL of the file //
string displayURL = spWeb.Url.Trim() + @"/" + spFile.Url.Trim();
// Setting the icon URL of the file //
string iconURL = spFile.IconUrl.Trim();
int y = iconURL.LastIndexOf("/") + 1;
iconURL = @"/_layouts/images/" + iconURL.Substring(y, iconURL.Length - y);
// Creating the node of the file //
TreeNode treeNodeChildFile = new TreeNode(spFile.Name.Trim(), spFile.Name.Trim(), iconURL.Trim(), displayURL.Trim(), "_blank");
parentNode.ChildNodes.Add(treeNodeChildFile);
}
}
}
//Display only folders as isFolder is set to true
else
{
SPListItemCollection spListItemCollection = spWeb.Lists[spFolder.ParentListId].GetItems(spQuery);
foreach (SPListItem spListItem in spListItemCollection)
{
// Checking whether the item is a file or a folder //
if (spListItem.Folder != null)
{
// Creating the node for the folder //
TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/images/folder.gif");
//Adding folder to the node
parentNode.ChildNodes.Add(treeNodeChildFolder);
// Calling the custom method to get all the subfolder and files within the folder //
GetAllFilesFolders(spListItem.Folder, treeNodeChildFolder, false, true);
}
}
}
}
}
}
Hope this helps and save your time. :-)