Welcome to my blog on all things SharePoint. I have a range of articles that will interest you if you've made it as far as visiting my blog. I was awarded as an SharePoint MVP by Microsoft in July 2010. I currently live in New York and am an Enterprise Architect at AvePoint Inc.. I co founded www.NothingButSharePoint.com with Mark Miller in 2010.

MVP AwardJeremy Thake Profile Photo

Whitepapers

NBSP

Check out my articles on NothingButSharePoint.com

Solution Development in SharePoint 2007

This series was inspired by the chatter amongst SharePoint blogs on the best ways to approach customisations in SharePoint using Solutions.

Part 1 - Part 2 - Part 3 - Part 4 - Part 5 - Part 6 - Part 7 - Part 8

Leveraging the SharePoint Platform

This series was inspired by a discussion had with Andrew Coates at a Perth SharePoint User Group meeting. This then turned into a 6 part series on Arno Nell's SharePointMagazine.net web site.

Initial post - Part 1 - Part 2 - Part 3 - Part 4 - Part 5 - Part 6

Webcasts

I have recorded various web casts that I present at User Groups or just on a specific topic by request:
How ASP.NET Developers can leverage SharePoint webcast
SPSource Webcast: Reverse engineer Lists to ListTemplates and much more
SharePoint Development with Unit Testing webcast
Perth SharePoint UG Web Cast on approaches to deploying artefacts (SPSource)
More...


Podcasts

I have been interviewed about Leveraging the SharePoint Platform by the SharePoint Pod Show: listen here .

RSS Feed Feed your read!

Archives

November 2012 (6)
October 2012 (8)
September 2012 (4)
August 2012 (7)
July 2012 (13)
June 2012 (4)
March 2012 (1)
February 2012 (1)
January 2012 (5)
September 2011 (2)
August 2011 (1)
July 2011 (3)
June 2011 (7)
May 2011 (3)
April 2011 (3)
March 2011 (3)
February 2011 (2)
January 2011 (1)
December 2010 (4)
September 2010 (4)
July 2010 (5)
June 2010 (4)
May 2010 (6)
April 2010 (7)
March 2010 (5)
February 2010 (7)
January 2010 (3)
December 2009 (1)
November 2009 (6)
October 2009 (9)
September 2009 (7)
August 2009 (6)
July 2009 (13)
June 2009 (4)
May 2009 (12)
April 2009 (4)
March 2009 (4)
February 2009 (13)
January 2009 (4)
December 2008 (4)
November 2008 (11)
October 2008 (16)
September 2008 (4)
August 2008 (5)
July 2008 (4)
June 2008 (8)
May 2008 (5)
April 2008 (9)
March 2008 (5)
February 2008 (6)
January 2008 (1)
November 2007 (11)
October 2007 (8)
September 2007 (24)
August 2007 (5)
July 2007 (2)
May 2007 (1)
April 2007 (1)
March 2007 (1)
February 2007 (3)
January 2007 (4)
November 2006 (7)
October 2006 (7)
September 2006 (18)
August 2006 (14)
June 2006 (3)
May 2006 (8)
April 2006 (4)
March 2006 (38)
February 2006 (30)
January 2006 (2)
December 2005 (3)
November 2005 (28)
May 2005 (1)
April 2005 (5)
March 2005 (1)
November 2004 (1)
August 2004 (11)
July 2004 (1)
Failed to render control: An error occurred during a call to extension function 'createMonthUrl'. See InnerException for a complete description of the error.

Links

Tag Cloud

Ajax, Apple, DotNetNuke, Enterprise Content Management, Error Resolution, Gadgets, General, Governance, Microsoft .Net Development, Mobile, SharePoint, Sharepoint Business Forms, Sharepoint Business Intelligence, Sharepoint Collaboration, SharePoint Development, Sharepoint Enterprise Content Management, Sharepoint Enterprise Search, Sharepoint Portal, US Migration, Web 2.0, Workflow

Solution Development for SharePoint 2007 Part 7   

Tags: SharePoint
Technorati Tags:

Part 1 - Part 2 - Part 3 - Part 4 - Part 5 - Part 6 - Part 7 - Part 8

"Event Receivers and Page Creation"

In the last week or so I've been doing a lot of work with creating pages with content on it automatically based on event receivers firing when items are created on Lists. To follow on with the TV show Schedule scenario when a new schedule is created, I want to create a page in the Pages Library on the Site itself and add a Content By Query Web Part similar to the one on the homepage of the Site, but with a filter on the Show itself. So each episode will have a page and it will link to all other episodes created.

This is done in two steps, firstly adding the Event Receiver to the Schedule List and then creating the Event Receiver.

I added the Event Receiver code to the Web scoped Feature Receiver FeatureActivated method. This can also be done in the definition of the ListInstance, but as I discussed before, because I will be cleaning up after myself I keep the adds and deletes in the same place for consistency. For the purposes of this tutorial I'm not doing anything but an ItemAdded, but you could have triggers on editing the Schedule or deleting it too to clean up pages created etc.

web.Lists["Schedule"].EventReceivers.Add(
  SPEventReceiverType.ItemAdded,
  "jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a",
  "jeremythake.tvshowschedulesite.ScheduleListEventReceiver");

I added the delete code to the FeatureDeactivating method as below. Now there is a risk that other Event Receivers could be added here so you may wish to check you delete the correct ones you've added.

for(int i=0; i<web.Lists["Schedule"].EventReceivers.Count; i++)
    web.Lists["Schedule"].EventReceivers[i].Delete();

The actual Event Receiver code is a simple C# class (.cs) in the root of the STSDEV project. The base of this is:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Publishing.WebControls;
using System.Globalization;
 
namespace jeremythake.tvshowschedulesite
{
    public class ScheduleListEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPListItem currentListItem = properties.ListItem;
            using (SPWeb web = properties.OpenWeb())
            {
 
            }
        }
    }
}
 

From here you can start doing the work to create the new Page. There were some teething problems in getting the Page title to appear correctly in the navigation and getting access to what is easy in the Interface as the Page Title by going to the Page Settings page:

image

Because the Page is in the Pages List, you have to access the List Item Properties to change this...setting the PublishingPage Title Property doesn't set it. This took a little time to fathom out!

PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);
 
//Get List Template - used below
SPListTemplateCollection listTemplates = web.Site.RootWeb.ListTemplates;
SPListTemplate scheduleListTemplate = listTemplates[scheduleListTemplateName];
 
// Create the new page in the PublishingWeb.
PublishingPageCollection pages = pw.GetPublishingPages();
PageLayout[] pageLayouts = pw.GetAvailablePageLayouts();
PageLayout blankWebPartPageLayout = null;
foreach (PageLayout pageLayout in pageLayouts)
    if (pageLayout.Title == "Blank Web Part Page")
        blankWebPartPageLayout = pageLayout;
PublishingPage newPage = pages.Add(scheduleName + ".aspx", blankWebPartPageLayout);
string checkInComment = "Page created";
//Title modified here
newPage.ListItem["Title"] = scheduleName;
newPage.ListItem.Update();
newPage.CheckIn(checkInComment);

and then putting the web part on the page:

SPFile categoryPageFile = web.GetFile("Pages/" + scheduleName + ".aspx");
if (categoryPageFile.Exists)
{
    categoryPageFile.CheckOut();
    using (SPLimitedWebPartManager webPartManager = categoryPageFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        ContentByQueryWebPart contentByQueryWebPart = new ContentByQueryWebPart();
        contentByQueryWebPart.Title = "Related Schedules";
        contentByQueryWebPart.WebUrl = web.Url;
        contentByQueryWebPart.BaseType = string.Empty;
        contentByQueryWebPart.ServerTemplate = Convert.ToString((int)scheduleListTemplate.Type, CultureInfo.InvariantCulture);
        contentByQueryWebPart.CommonViewFields = "Air_x0020_Date,AirDate;Episode_x0020_Number,Episode;Season_x0020_Number,Season";
        contentByQueryWebPart.ItemStyle = itemStyle;
        //filter by schedule
        contentByQueryWebPart.FilterField1 = web.Lists[listName].Fields["Title"].Id.ToString("B");
        contentByQueryWebPart.Filter1ChainingOperator = ContentByQueryWebPart.FilterChainingOperator.Or;
        contentByQueryWebPart.FilterOperator1 = ContentByQueryWebPart.FilterFieldQueryOperator.Contains;
        contentByQueryWebPart.Filter2ChainingOperator = ContentByQueryWebPart.FilterChainingOperator.And;
        contentByQueryWebPart.FilterValue1 = scheduleTttle;
        contentByQueryWebPart.FilterType1 = "Text";
        contentByQueryWebPart.ShowUntargetedItems = false;
        webPartManager.AddWebPart(contentByQueryWebPart, "Right", 0);
    }
    categoryPageFile.CheckIn("Added Page");
    categoryPageFile.Publish("Published");
}

Again, this can be extended to start showing further web parts as they are developed such as Character guides or RSS feeds from fan sites etc.

I've uploaded the zipped project again for you guys to have a look at here.

 
Posted by  Jeremy Thake  on  6/7/2008
1  Comment  |  Trackback Url  | 1  Links to this post | Bookmark this post with:        
 

Links to this post

Comments


  commented on  Friday, February 20, 2009  8:31 AM 

blog comments powered by Disqus