Archive

Archive for the ‘Web Part’ Category

Site Navigation with a Content Query Web Part in a SharePoint Page Layout

November 1, 2011 Leave a comment

A brief on one of my current projects was to create left hand navigation for an new Intranet which was built up from the Pages library.  As is usual with Intranets, the content was spread across multiple publishing sites under a single Site Collection, hence each site had it’s own Pages library.  It’s a simple task to set this up for each page using the Content Query Web Part (CQWP) but ideally this should be part of a Page Layout that can be used on every site.  

The other requirement was that the Title of the Web Part should be that of the current site, so Information Technology if the user was view pages from that site, Operations for the Operations site etc.

Out of the box, this would not be possible as the CQWP is linked to a single library and it’s Title is set from the Web Part Properties. You obviously could add a CQWP to each page but this is inefficient and beyond most content editing users. So for a more elegant solution you’ll need to use Visual Studio to create your own Web Part, however the code is extremely straightforward and is derived from ContentByQueryWebPart. You will need to include a reference to Microsoft.SharePoint.Publishing.WebControls in your project which is in the Microsoft.SharePoint.Publishing namespace and the code below should be enough to get you up and running.

public class ContentNavigation : ContentByQueryWebPart
{
 protected override void CreateChildControls()
 {
   SPWeb web = SPContext.Current.Web;
   SPList list = web.GetList(web.Url + "/Pages");
   this.ListGuid = list.ID.ToString();
   this.ListName = list.Title;
   base.CreateChildControls();
 }

 protected override void OnPreRender(EventArgs e)
 {
   this.Title = SPContext.Current.Web.Title;
   base.OnPreRender(e);
 }
}

Once packaged and deployed, it’s then straightforward to create a Page Layout containing this web part.

Create a blank publishing page in SharePoint 2010 for a print view or hosting in an iFrame

October 11, 2011 Leave a comment

There are a few occassions when a page’s content could be useful without the associated branding and navigation elements being visible.  For example if you want to create a print view or host SharePoint content in an iFrame.  In my case I wanted to host the Organization Browser web part within an iFrame using a Content Editor Web Part.  This would enable me to access the same page on multiple sites, getting different content by setting querystring values in the CEWP and overcoming the issue of adding querystrings to URLs breaking SharePoint navigation.

First you need to create a new masterpage using SharePoint Designer, to do this take a copy of your existing custom masterpage or default.master and name it blank.master. Edit the file in Advanced Mode and find the main table which starts with <table class=”ms-main”.  Now add this style=”display:none” tag to the end.  It should look something like this:

<table cellpadding=”0″ cellspacing=”0″ border=”0″ width=”100%” height=”100%” style=”display:none”>

Next find this content holder <asp:ContentPlaceHolder id=”PlaceHolderMain” runat=”server”></asp:ContentPlaceHolder> and copy this to the clipboard, delete it from it’s original location and place it under the closing tag for the main table.  Also, to avoid scrollbars appearing in my iFrame, I amended to the body tag to set scroll=”no”.

Checkin, publish and approve your new masterpage.  Now if you haven’t already, create the content which you wish to host in the blank master page, then return to SharePoint Designer.  Next we need to detach the content page from it’s Page Layout, so open the library which contains your content, select the file, right click  and choose Detach from Page Layout then click Yes to the warning.  If the library is under source control, click Yes to check it out.  Now right click again and choose Edit file in Advanced Mode.

Now replace this line:

<%@ Page language=”C#” Inherits=”Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c” meta:progid=”SharePoint.WebPartPage.Document” meta:webpartpageexpansion=”full” %>

with this:

<%@ Page language=”C#” MasterPageFile=”~/_catalogs/masterpage/blank.master” Inherits=”Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c” meta:progid=”SharePoint.WebPartPage.Document” meta:webpartpageexpansion=”full” %>

Save the file and if all is well, you’ll see the content without branding and navigation.

As you can see from the changes, this content now inherits from Microsoft.SharePoint.WebPartPages.WebPartPage which enables the MasterPage to be set.  With Microsoft.SharePoint.Publishing.PublishingLayoutPage this isn’t possible without custom code overriding OnPreInit.

SharePoint London Underground Tube Status Web Part

September 21, 2011 2 comments

This is a small web part I developed which displays the status of the tube lines which make up the Transport for London Underground network.   It could be a handy web part to fill in an area of a corporate Intranet, if of course the company happens to be based in London!

This consumes a TfL datafeed so your server will need Internet access and the solution needs to be deployed at the farm level.  I’m not going to post how you can deploy this web part but a generic farm solution deployment guide can be found here.  I’ve tested this on SharePoint 2010 only but should work fine on MOSS too.

The code doesn’t use Ajax and there is no automatic refresh.  More information about lines with problems will appears on a mouse over on the status.  I’ve not included the source but happy to post if it helps anybody, and I am considering writing a tube departure board web part showing the trains from a configurable station, so if this is of any interest then please leave a comment.

Download Tube Status Web Part

Categories: SharePoint, Web Part
%d bloggers like this: