Wednesday, June 4, 2008

SharePoint SilverLight Navigation

I recently have been playing around with some SilverLight stuff for SharePoint and came across the Silverlight for SharePoint Blueprint.


I installed the Silverlight Navigation Controls, which by the way need a lot of additional work to get working correctly. See http://www.wssdemo.com/Blog/archive/2008/03/24/installing-the-sharepoint-blueprint-for-silverlight-silverlightpart.aspx and http://www.u2u.info/Blogs/Patrick/Lists/Categories/Category.aspx?Name=Silverlight%20BluePrint for additional information.


Anyway, after getting this to work I found that it only picks up if you are using a Publishing Web and only picks up Publishing Pages. What I wanted to see is the actual SharePoint Navigation in Silverlight. So this is where the fun began.



After diving into the code I found a couple of things.


First, the SiteMapProvider that it is using is the CurrentNavSiteMapProvider which is an instance of the PortalSiteMapProvider. This SiteMapProvider retrieves the links from the QucikLaunch SPNavigation collection.


Secondly, I found the following code that was limiting the Navigation to Publishing Webs.


PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Web);

PublishingPage pubPage = pubWeb.GetPublishingPages()[pageNode.Url];


What to Do

So the first thing to do is replace the SiteMapProvider with on the will return the TopNavigationBar. I choose to use the CombinedSiteMapProvider since it acts exactly like the GlobalSiteMapProvider when the Web site in question is set not to inherit its global navigation.


Secondly, comment out the 2 lines of code mentioned above:



// -- get the sites
try
{
//PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Web);
SiteMapNode startNode = portalSiteMapProvider.CurrentNode;
if (!startNode.HasChildNodes)
startNode = portalSiteMapProvider.CurrentNode.ParentNode;
foreach (SiteMapNode pageNode in startNode.ChildNodes)
{
// -- get reference to publishing page to retrieve description and image
// PublishingPage pubPage = pubWeb.GetPublishingPages()[pageNode.Url];



next change the references to the pubPage variable to pageNode since we just removed pubPage. Also you will need to change the properties that are being returned by the pageNode.



if (pageNode != null)
{
// -- add the page node with title and url
XmlNode xmlnode = xmlDoc.CreateElement("Page");
XmlAttribute titleAttribute = xmlDoc.CreateAttribute("Title");
titleAttribute.Value = pageNode.Title;
xmlnode.Attributes.Append(titleAttribute);
XmlAttribute urlAttribute = xmlDoc.CreateAttribute("Url");
urlAttribute.Value = siteUrl + pageNode.Url;
xmlnode.Attributes.Append(urlAttribute);
siteNode.AppendChild(xmlnode);
if (pageNode != null)
{
XmlAttribute descriptionAttribute = xmlDoc.CreateAttribute("Description");
if (pageNode.Description != null)
descriptionAttribute.Value = pageNode.Description.ToString();
else
descriptionAttribute.Value = "";
xmlnode.Attributes.Append(descriptionAttribute);
XmlAttribute imageAttribute = xmlDoc.CreateAttribute("Image");


Next I commented out the code applying the page image to the menu items. (NOTE: Still trying to figure out how to get the site image to load here)



/* if (mysite.SiteLogoUrl.ToString() != null)
{
string imageHTML = mysite.SiteLogoUrl.ToString();
XmlDocument imdoc = new XmlDocument();
imdoc.LoadXml(imageHTML + "");
if (imdoc.DocumentElement.Attributes.GetNamedItem("src") != null
&& imdoc.DocumentElement.Attributes.GetNamedItem("src").Value != null)
imageAttribute.Value = siteUrl + imdoc.DocumentElement.Attributes.GetNamedItem("src").Value;
} */
/* else if (pubPage.ListItem["PublishingRollupImage"] != null)
{
string imageHTML = pubPage.ListItem["PublishingRollupImage"].ToString();
XmlDocument imdoc = new XmlDocument();
imdoc.LoadXml(imageHTML + "");
if (imdoc.DocumentElement.Attributes.GetNamedItem("src") != null
&& imdoc.DocumentElement.Attributes.GetNamedItem("src").Value != null)
imageAttribute.Value = siteUrl + imdoc.DocumentElement.Attributes.GetNamedItem("src").Value;
}*/


That's it. The Silverlight Navigation Bar will now show you the Site Navigation rather then just publishing pages.






No comments: