Integration Points

Extending Corporate Data …

#41 Breadcrumb Control based on a SPList

leave a comment »

Based on the prior post #40 You can plumb in a SPList to feed the urls to match on and decide to ignore, replace or substitute 2 nodes for a directory path.  For help on SPMetal & SP Linq see: http://socialsp.com/2009/12/11/having-fun-with-the-new-linq-to-sharepoint-on-sharepoint-2010-sp2010/

SP List to Feed URL matching (done using a simple switch() in the prior post)

Breadcrumb List

using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;

namespace SPBreadCrumb.ControlTemplates.SPBreadCrumb
{

    public partial class BWBreadcrumb : UserControl
    {

        private const string siteURL = @"http://Sharepoint/Site";
        protected SPMetalDataContext TT = new SPMetalDataContext(siteURL);

        StringBuilder sbResult = new StringBuilder();				// Holds the Breadcrumb HTML.
        StringBuilder sbBcUrl = new StringBuilder();				// Holds the URL of the breadcrumb.
        // Dir are appended in succession to the root.
        private HybridDictionary labels = new HybridDictionary();	// Holds the "friendly" directory names.

        private string Separator = "> ";

        protected void Page_Load(object sender, EventArgs e)
        {
            string strHostUrl = "http://" + Page.Request.ServerVariables["HTTP_HOST"] + "/";
            sbBcUrl.Append(strHostUrl);

            string scriptName = Page.Request.ServerVariables["SCRIPT_NAME"];
            string strScriptDir = Path.GetDirectoryName(scriptName);
            bool bHasExtension = Path.HasExtension(scriptName);

            strScriptDir = strScriptDir.Substring(1);
            string[] strDirs = strScriptDir.Split('\\');
            int nNumDirs = strDirs.Length;

            // Node setup on Breadcrumb
            bool FirstNode = true;
            foreach (string strDirName in strDirs)
            {
                sbBcUrl.Append(strDirName + "/");  // URL SETUP
                sbResult.Append(buildLinkNode(FirstNode, sbBcUrl.ToString(), strDirName));
                FirstNode = false;
            }

            sbResult.Append(Path.GetFileName(scriptName));  // Show File Name
            lblBC.Text = sbResult.ToString();
        }

        private string buildLinkNode(bool FirstNode, string URL, string DirName)
        {

            string sResults = "";
            try
            {
		                   // Run SPMetal utility to get object to reference site
                using (SPMetalDataContext ctx = new SPMetalDataContext(SPContext.Current.Web.Url))
                {
                    ctx.ObjectTrackingEnabled = false;
		                                               // SPList Reference Here<>
                    EntityList<BWInternal_BreadcrumbItem> BC = ctx.GetList<Internal_BreadcrumbItem>("BWInternal_Breadcrumb");
                    var query = from c in BC.ToList()
                                select c;

                    foreach (var bc in query)
                    {

                        if (bc.ReadURL.ToString().ToLower() == URL.ToLower())
                            sResults = setupBCNode(FirstNode, bc.Node1URL, bc.Node1Label, bc.Node2URL, bc.Node2Label);

                    }

                }
            }
            catch {}

            return(sResults);

        }

        private string setupBCNode(bool FirstNode, string URL1, string Label1, string URL2, string Label2)
        {
            string s = "";
            if (!FirstNode)
                s += Separator;
             s = "<a href='" + URL1 + @"' > " + Label1 + @"</a> ";

            if (URL2 == null || URL2.Trim().Length > 0)
                s += Separator + " <a href='" + URL2 + @"' > " + Label2 + @"</a> ";
            return (s);
        }

    }
}

Advertisement

Written by dmgorman

August 9, 2010 at 10:00 pm

Posted in Linq, Sharepoint

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.