Can someone tell me how to add a breadcrumb for a document library to a site page so that it will show the nested folder structure as a user navigate up and down the directory structure.
Example structure:
Document Library > Folder1 > Folder2
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityBy default there are no OOTB functionality that brings breadcrumb for a document library.
To get the nested folder structure of the Document Library, you will have to decode the URL of your site, when you click on each folder. There is an SPContext.Current.RootFolderUrl
which will bring you the exact path of the Folder structure of a document library.
You need to add this into a webpart, edit the document library and add the webpart above the document library to view as a bread crumb.
UPDATE: Finally I've created a project on CodePlex: http://fullpathbreadcrumb.codeplex.com. It implements the functionality described in this post.
Although there's a "Best answer" marked answer already I've shared my custom webpart code for the same task - it modifies library breadcrumb and add a full folder path to it:
[ToolboxItemAttribute(false)]
public class FullPathInLibraryBreadcrumbWebPart : WebPart
{
protected override void CreateChildControls()
{
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string path = Context.Request.QueryString["RootFolder"];
if (String.IsNullOrEmpty(path))
{
return;
}
SPWeb web = SPContext.Current.Web;
SPFolder currentFolder = web.GetFolder(path);
Control c = FindRecourcive(Page.Controls, ctrl => ctrl is ListProperty && ((ListProperty)ctrl).Property == "TitleBreadcrumb");
if (c == null)
return;
var parent = c.Parent;
var index = parent.Controls.IndexOf(c);
parent.Controls.RemoveAt(index);
parent.Controls.AddAt(index, new LiteralControl { Text = GetNewBreadCrumbOutput (currentFolder)});
}
private string GetNewBreadCrumbOutput(SPFolder folder)
{
List<BreadcrumbNodeData> nodes = new List<BreadcrumbNodeData>();
SPFolder nodeFolder = folder;
while (nodeFolder != null)
{
nodes.Add(new BreadcrumbNodeData { Title = string.IsNullOrEmpty(nodeFolder.ParentFolder.Url) ? nodeFolder.DocumentLibrary.Title : nodeFolder.Name, Url = nodeFolder.ServerRelativeUrl });
nodeFolder = string.IsNullOrEmpty(nodeFolder.ParentFolder.Url) ? null : nodeFolder.ParentFolder;
}
nodes.Reverse();
string htmlOutput = String.Empty;
foreach (var node in nodes)
{
if (node != nodes.Last())
htmlOutput +=
String.Format(
@"<A href=""{0}"">{1}</A> <IMG style=""vertical-align:middle"" alt=: src=""/_layouts/images/FullPathInLibraryBreadcrumb/breadcrumb_arrow.png""/> ", node.Url, node.Title);
else
{
htmlOutput += node.Title;
}
}
return htmlOutput;
}
private Control FindRecourcive(ControlCollection controls, Func<Control, bool > criteria)
{
foreach (Control control in controls)
{
if (criteria(control))
return control;
var innerControl = FindRecourcive(control.Controls, criteria);
if (innerControl != null)
return innerControl;
}
return null;
}
}
public class BreadcrumbNodeData
{
public string Title { get; set; }
public string Url { get; set; }
}
And the result (http://gyazo.com/981e78fb2266b0cd27221545c65a44cd.png):
As for your question where to put the code - you need to create SharePoint 2010 project and add new project item of WebPart type (note - you don't need a visual web part for this task as you don't need the UI at all):