Developing and Deploying WSS 3.0 Events


Introduction

Windows SharePoint Services 2.0 was very limited when we talked about the event handlers.  Event hander will capture various things happening on sites, lists, document libraries, like creating a site, deleting a site, adding list item, deleting list item, document checked out and checked in etc.  WSS 2.0 only support Events for Document libraries.  No support was available for lists and sites.  WSS 3.0 now adds much of the functionality into the events.  WSS 2.0 includes asynchronous events only which means that they were fired after something is happened and there is no way of stopping it.  WSS 3.0 now includes asynchronous event but also provide synchronous events as well.  Events like ItemDeleting, SiteDeleting, ListColumnAdding etc will provide a great infrastructure for developer to create great application on Windows SharePoint Services 3.0. 

Creating Event Handlers

Creating event hander is also very easy.  Events can be create by using Visual Studio.NET 2005 class library and inheriting from WSS receiver class and overriding the methods to handle WSS events.  In this scenario I will demonstrate a small logging application that logs what is currently going on inside a document library. 

Technologies Used

1.       .NET Framework 2.0

2.       Visual Studio.NET 2005

3.       SQL  Server 2000

4.       C#.NET

5.       Web Parts

 

Getting Ready for the Session

Description

Screenshot

Ø  Create a New WSS Web Site

Ø  Click Start

Ø  Microsoft Office Server

Ø  SharePoint 3.0 Central Administration

Ø  On Central Administration Page Click on Application Management

Ø  Under SharePoint Site Management Click Create a Site Collection

Ø  On create site collection page

Ø  Select the web application where you wanted to create a Site Collection using web application dropdown

Ø  Enter the required name in the Title Text box

Ø  Type description if required

Ø  Select the Sites Directory in the URL path and enter the site URL name

Ø  Select Blank site

Ø  Click OK

Ø  Enter primary and secondary site collection administrator and Click OK

Ø  Wait for the progress page

Ø  When you see site collection creation success page.  Click on the link to visit the site.

Ø  Blank site will look like this

 

Ø  On the site collection page click on the Site Actions and Create

Ø  On the create page under the libraries section click Document Library

 

Ø  Enter Document Library name (Secret Docs in this case)

Ø  Type description

Ø  Select Yes to Show in Quick Launch

Ø  No for Create Version on each edit

Ø  Select Microsoft Office Word if you have Office 2003 Select Office Word 97/2000 format

Ø  Click OK

Create a Custom List

 

Ø  On the site collection page click on the Site Actions and Create

Ø  On the create page under the Custom Lists section click Custom List

Ø  Enter List Name as CEO Docs Access Logs

Ø  Type Description

Ø  Yes to Show on Quick Launch

Ø  Click Create

Ø  Add a new column to custom list and name it as Event Date

 

Ø  Adding Both Library and List as web parts on the home page

 

Ø  On Site Actions

Ø  Click Edit Page

Ø  Page will look like this

Ø  Click on Add a Web Part

Ø  On Add web part to left page

Ø  Select both document library and Lists

Ø  Click Add

 

Ø  Click on the CEO Document Library Header Drag it to on top of List to move it above the list.

Ø  Click Exit Mode

Ø  Page will Look like this

Developing Event Handler

Description

Screenshot

Ø  Start Visual Studio.NET 2005

 

Ø  Create a C# Class Library Project

Ø  Add a Reference to Microsoft.SharePoint.dll

Ø  In Visual Studio Click Project

Ø  Add Reference

Ø  Scroll Down to see Windows SharePoint Services.dll File

Ø  Select the File and Click OK or Double Click on the file

Ø  Import the Microsoft SharePoint by using “using” keyword

Ø  Rename the class file to CEODocEventReceiver.cs

Ø  The code segment will look like this

Ø  Inherits this class from SPListItemEventReceiver Class in SharePoint

Ø  Now we need to test methods of SPListItemEventReceiver class.  Lets override them. 

Ø  Go to the class definition and add

o   ItemAdding

o   ItemAdded

o   ItemUpdating

o   ItemUpdated

Ø  You can see that SharePoint now support both types of event handlers synchronous and asynchronous. 

Ø  Your class will look like this.

Ø  Lets add code to item adding

Ø  Create a Method to populate the Log list create above

Ø  This method will receive the message from the various events of the lsits

private void AddLogEntry(string strMessage)

        {

            //Collection Site collection using Spsite

            SPSite siteCollection = new SPSite("http://jerry/sites/WSSEvents");

            //Collection the web

            SPWeb webSite = siteCollection.RootWeb;

 

            //Get the log lists

            SPList ItemList = webSite.Lists["CEO Docs Access Log"];

            //add a new time

            SPListItem Item = ItemList.Items.Add();

            // Set properits

            Item["Title"] = strMessage;

            Item["Event Date"] = DateTime.Now.TimeOfDay.ToString();

            Item.Update();

            webSite.Dispose();

        }

Ø  Lets write the code in ItemAdding Event to log the creation of an item to the list

public override void ItemAdded(SPItemEventProperties properties)

        {

            // Collect the SPlistitem from the EventProperties

            SPListItem anItem = properties.ListItem;

            // Create a String to write an entry

            string strMessage = "An Item is Added: " + anItem.Title.ToString() + ":" + anItem["Name"].ToString();

            // Call the Log method create above.

            AddLogEntry(strMessage);

        }

Ø  Now add the code to others events

For this example I am have used the same code but changed the string text.

Ø  Sign the Assembly

Ø  Goto solution properties in Visual Studio 2005

Ø  Click Signing and Check Sign the assembly checkbox

Ø  Select New from the drop down and enter a name

Ø  Uncheck password protected and click OK

Ø  Build the DLL and put the solution DLL into Global Assembly Cache

Registering a Event Handler

There are two way to register the event handlers

1.       SP Object Model

2.       Using Features

1.       Registering events using SharePoint Object Model

In this method we use the SharePoint object model to register the event.  We collection the SharePoint site and Web and get the list where we want to bind the events. 

2.       Registering events using Features

Features are package of Windows SharePoint Services elements to do some sort of action like installing any application or program at the root level and enabling its activation or deactivation on root site or any other site we want. 

Registering Event using SharePoint Object Model

1.       Things required to register the event

2.       SPList Object

3.       SPWebObject

4.       SPListObject

5.       AssemblyName

6.       Class Name

1.       Create a new Console Based Application and name it as you want

2.       Every List has a property EventReceivers

3.       EventReceivers object Requires

a.       EventType = Enumeration

b.      Assembly Name

c.       Class Name

4.       Go to main of the console application and add the following code

SPSite siteCollection = new SPSite("http://jerry/sites/WSSEvents");

            //Collection the web

SPWeb webSite = siteCollection.RootWeb;

 

SPList CEOSecretDocs = webSite.Lists["Secret Docs"];

 

string strAssemblyName = "CEOLibraryEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=de977b68a8e516e5";

string strClassName = "CEOLibraryEvents.CEOEvents";

 

CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemAdded, strAssemblyName, strClassName);

            CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemAdding, strAssemblyName, strClassName);

CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemUpdatedstrAssemblyName, strClassName);

            CEOSecretDocs.EventReceivers.Add(SPEventReceiverType.ItemUpdating, strAssemblyName, strClassName);

Run the Console Application to install the event handlers

Hit F5 in Visual Studio.

Test the Events

To Test the event go to the site

1.       Click New Item on CEO Docs Document library

2.       Save the Item

3.       A task will be added to the list

4.       Now Open the item again and Edit it and save it again

5.       Task for Updating and Updated will be added to the log list

 

Events are key to transforming Microsoft Windows SharePoint Services into a true developer platform. Events enable first and third parties to hook into Windows SharePoint Services behaviors and override default behaviors.

Events fall into two major categories:

·         List events   Core events, including changes, additions, and removals of list items and list columns (schema changes)

·         Simple site events   Deletion of sites and site collections

Events are either synchronous "before" events, denoted by the "XYZing" name format, or asynchronous “after” events, denoted by the "ABCed" name format.

·         Event receivers can be registered, through the "Features" feature, with an Item, a List, a Web, or a Content Type. For example, an event receiver that ensures a document always has a copyright in the footer can be associated with a central Content Type and pushed down to all document libraries associated with that type.

·         As they do in Microsoft Windows SharePoint Services (version 2), developers can override and extend the event handler method. A new sequence number parameter allows for control of the firing order of multiple events.

I have used SharePoint Object Model to deploy the Event handlers.  In the next example I will show how to install them using Features.  So stay tuned.

Regards,

JERRY

 

Developing and Using Feaures in SharePoint Version 3.0


Microsoft introduced another innovation in SharePoint products and technologies 2007.  This innovation is called Features.  Using Features developer can easily add or update any events handler, chunk of code or functionality without any complexity.

Introduction

Features reduce the complexity involved in making simple site customizations, and are robust when upgrades are applied to a deployment. Features eliminate the need to copy large chunks of code to change simple functionality. Features thus reduce versioning and inconsistency issues that may arise among front-end Web servers. Features make it easier to activate or deactivate functionality in the course of a deployment, and administrators can easily transform the template or definition of a site by simply toggling a particular Feature on or off in the user interface. Features provide the following capabilities:

·         Scoping semantics for determining where custom code runs

·         Pluggable behavior for installing or uninstalling Features within a deployment

·         Pluggable behavior for activating or deactivating Features at a given scope

·         A scoped property bag for storing data required by a Feature within its scope

·         The basis of a unified framework for distributed deployment of Windows SharePoint Services solutions

Feature Components

A Feature in SharePoint 2007 is an XML file that include the description of the functionality we want to include into our site.  All the installed features resides into Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. 

To create a new feature we need to create a new folder with the feature name and add the required XML file.  There are two XML Files which are required.

Feature.XML

This file will include Feature tags like title, Version, hidden, Description, ImageURL, RequiredResources, Scope.  Title is the name of feature that will be visible in Site Collection feature page, Hidden is property to show or hide the feature, Scope is Site or Site Collection.  Feature also have  ElementFile, ElementManifest attributes that defines it manifest or target to any other file name.

Elements.XML

Contains references to element manifests and element files that contain definitions for the Feature elements.

In this article we will create a custom menu into the site action drop down in our MOSS site collection.

Development

Ø  Start Visual Studio.NET 2005

Ø  Create a new Project and Name it XavorPortalLinkFeature

Ø  Click OK to Create the project

When Visual Studio opens the project.  The first thing we need to do is to close the Class1.cs file opened.

Creating Folders for the feature.xml

Ø  Navigate the Solution Explorer and right click on project name and Click Add -> New Folder

Ø  Name the Folder as XAVOR

Ø  Right click on Folder Created above and Click Add -> New Folder  and change its name to Xavor PortalLinkFeature

Ø  Now right click on the XavorPortalLinkFeature folder and Click Add -> New Item -> XML File.

Ø  Rename this file to Feature.xml

Ø  Remove the default line from the feature .xml

It will Look like this

Adding Feature.XML Schema File

Ø  Click inside the Feature.xml file and from Properties click on the … button on the right side the schema

Ø  Click the Add Button and browse

Adding Feature.XML Schema File

Ø  Browse to the Following Location.

Ø  Select WSS.xml if you have Beta II TR and WSS12.xml if you have Beta II only.

Ø  Click OK in Browse page

Ø  You will see WSS.xml as select schema in the list.

Ø  Click OK to Close it

Adding Elements in Feature.xml

Ø  Previous step will enable full intellisense for the development Feature.xml

Ø  The Element will be the Feature TAG.

Ø  Add ID Tag inside the feature tags and use Visual Studio.NET 2005 GUID creator to create a new GUID.

Ø  Tools – > Create GUID

Ø  Add Title inside the feature tag and give it a title.

Ø  Add Description and type some description

Ø  Add Scope and Select your scope from

o   Farm

o   Web Application

o   Site

o   Web

Ø  XML will look like this

<Feature xmlns="http://schemas.microsoft.com/sharepoint/&quot;

Id ="9ADA7D91-009E-4385-A07A-8DE5C7B754AF"

Title="XAVOR Portal Link Feature"

Description ="This Feature will add Xavor Portal Link under the Site Action dropdown"

Scope="Site"

Hidden="FALSE"

The next step is to copy this feature to the WSS Feature Directory on file system to do so

Ø  Navigate to Project Directory

Copy the STSADM.EXE to C:\STSAdmin Folder

Navigate to WSS Features (WSS stores all the isntalled features in)

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

 

My project feature path is

Ø  D:\Jerry’s Documents\My Documents\Visual Studio 2005\Projects\XavorPortalLinkFeature\XavorPortalLinkFeature\Xavor\

Ø  Copy XavorPortalLinkFeature folder which contains the feature.xml to the WSS Feature store.

tsadm –o installfeature –name XavorPortalLinkFeature

Ø  In Start -> Click Run and Click CMD to run Command prompt

Ø  Write CD\ and click enter

Ø  Write CD C:\STSAdmin and press enter

Ø  Run this command to register the feature.  You can also use object model to install the feature

Ø  When you see success the feature is installed.

Ø  Go to top level site and click on Site Settings

Ø  Click Modify all site settings

Ø  Click Site Collection Features under the Site Collection Administration

Ø  You will see your feature visible there you can activate and de-active the feature you want.

Ø  Test by Activating or de-activating the feature

 

We have create a basic feature.  Lets add the links to Xavor portal features

Ø  Add a new element under the feature.xml called ElementMenifest.  These tags include the core features provided by a Feature.

Ø  Add another XML file under the Feature Folder and name it as Elements.xml

Ø  Assign the same schema used in the previous file

 

<ElementManifests >

           <ElementFile Location ="Elements.xml"/>

</ElementManifests>

WSS 3.0 Schema will be available at

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML

Ø  Remove the default element of the Element.xml file

Ø  Using schema intellisense this time add Element Tag

Ø  As we need to add a custom URL link to the Site Actions we need to add the following code

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt;

  <CustomAction

    Id="SiteActioinToolbar"

    GroupId="SiteActions"

    Location="Microsoft.SharePoint.StandardMenu"

    Sequence="1000"

    Title="Xavor Employee Portal">

    <UrlAction Url="http://portal.xavor.com"/>   

  </CustomAction>

</Elements>

Ø  Copy the feature folder again to the WSS Feature Directory

Ø  In the Run enter IISReset to restart IIS

Ø  Re-Install the feature using stsadm

stsadm -o installfeature -name XavorPortalLinkFeature – force

-force will reinstall the feature

 

Using the feature

 

Ø  Go to top level site and click on Site Settings

Ø  Click Modify all site settings

Ø  Click Site Collection Features under the Site Collection Administration

Ø  You will see your feature visible there you can activate and de-active the feature you want.

Ø  Activate the Feature

Ø  Come to the Home page

Ø  Click the Drop Down Link near the site actions.

 

Activate the Feature   

Click drop down under the site actions and see the link added to the settings.

Click on the link to verify.

 

 

A Feature could provide, for example, a "My Favorite Items" functionality that includes the following elements:

·         A custom list that stores, per user, a list of favorite items, which is created as a single hidden list per workspace when the Feature is enabled.

·         A custom menu item that is attached to all lists, called "Add to Favorites," which adds an item to the Favorites list.

·         A Web Part that implements usage and link tracking to display the user’s top 10 favorites at the top.

Each element of the Feature, by itself, may not be very useful, but when you enable the Feature on a site, all these elements add up to a complete solution. (WSS SDK)

Regards,

JERRY

SharePoint Products and Technologies 2007 Training


From last few days I was not feeling good about my postings.  I was feeling bad that I was not posting anything for a long time.  This is because I have taken few initiatives which need to be done before I do anything else, and I tell you the things that I was doing were great.  I finished them up last night.  Yes so I am ready to post some stuff that I was working on.  But what are the initiatives that I am talking about.  Without any wait.

1. SharePoint Products and Technologies 2007 Training at Recognized software company in Lahore

2. SharePoint Products and Technologies 2007 at my current Employer Xavor. 

Yes I was training almost 15 professional .NET developers on the next release of Microsoft server products.  You know the response was even great.  I was not sure how these training will go but they went great.  The response was great.  Each and every developer sitting there was happy.  Some of the guest trainees were executives who liked SharePoint Server for Excel and form services.  The training duration was 2 hours daily for 5 days.  6 hours on Saturday and 2 hours on Monday

  1. Web Content Management in MOSS 2007
  2. Managing Layouts in WCM
  3. Designing Custom Master pages
  4. Managing CSS Files
  5. Creating Custom pages in MOSS

  1. SharePoint Security Model
  2. Site Architecture
  3. General Question on WCM

Day Hours Session Demo
Monday          2.5 Introduction to Microsoft Office SharePoint Server 2007 Navigation, UI, Sites, Lists, MySite, Administration, Object Model, Web Services
Tuesday         2.5 Introduction to Excel Services, Forms Services, Content Types, Site Columns Excel Services, Forms Services, Content Types, Site Columns
Wednesday 2.5 Installing and Configuring SharePoint 2007 WSS and MOSS, Installation, Configuration, creating Shared Services, Creating Web Application and Site Collection
Thursday 2.5 SharePoint Object Model, Creating Lists and managing lists using Custom Application, using excel Web Services, Business Data Catalog, SharePoint Search Creating Lists and managing lists using Custom Application, using excel Web Services, Business Data Catalog, SharePoint Search
Saturday 6   All
Monday 3   Security
Total Time   20 Hours Approximated  

Woooof It was not possible for me to do demos but when I started everything goes great I was able to demonstrate each and every demo that I want. Thanks to Microsoft for developing video content of TECH-ED, SharePoint and Microsoft Office Developer Conference, as well as the bloggers out there who wrote tons of stuff on SharePoint which makes very easy for me to grasp the ideas and started working as a trainer. This was my first ever training to professional sitting at a company. Of course I taught SharePoint to developers and student on request but the venues are always private. I am very happy to say that everything finished so good. I have tried to share all my experience as much I can regard SharePoint development.

Now I am hoping to get more assignment like the above in coming future. Let’s hope for the best.

I am also preparing for a 5 hours training at XAVOR. The content of the training will WSS and MOSS 2007 Introduction and Developers Opportunity in SharePoint Products and technologies 2007. This training will also help me improving my training skills. MY Current MCT application is cancelled because of some reasons (I don’t have documents related to experience yet). So I am a little disappointed but any way.

Once again thanks to everyone at SharePoint community for their sharing of tons and tons of material about SharePoint which helps guys like us to do thing we never thought.

Regards,

JERRY