#41 Breadcrumb Control based on a SPList
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)
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
