3

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

2 Answers 2

4

By 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.

3

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>&nbsp;<IMG style=""vertical-align:middle"" alt=: src=""/_layouts/images/FullPathInLibraryBreadcrumb/breadcrumb_arrow.png""/>&nbsp;", 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): enter image description here

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): enter image description here

4
  • a) When you say add a webpart - what kind of webpart? A Content Query web part or something else? b) I tried using a content query web part, but I cannot figure out where to access the url to set the breadcrumb. I'm new to sharepoint, thrown into a situation where I am way over my head and any clues would be immensely helpful. Thank you
    – Patty
    Feb 21, 2012 at 19:12
  • Forgive me, I am brand new to the world of sharepoint. I see that you've written C# code for this (thank you!), but where do I access that C# code from within SharePoint? Thanks very much.
    – Patty
    Feb 21, 2012 at 20:26
  • @Patty FYI.. this is custom C# code that needs to be written and deployed to the SharePoint server. If you have never written a web part before, you should do a quick google for some tutorials because it is a relatively involved process. :)
    – Kit Menke
    Feb 22, 2012 at 0:57
  • @all: please check a CodePlex project I've created now on this subject.
    – Alex Boev
    Feb 22, 2012 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.