Sharepoint Project : An EventHandler to fire when a new wiki page is created


Posted Date:     Total Responses: 0    Posted By: shabana    Member Level: Gold   Points/Cash: 100   


Problem


This project creates a Eventhandler in .net to fire on sharepoint site when we create a wiki page on the site.
That Eventhanlder has to create two folders on that site with the name of the page
1.under shared documents
2.under Images (if exists) ..picture library.












Requirements:


Development Machine Requirements


1. Windows Server 2003 OS
2. WSS 3.0 or MOSS.
3. Dotnet installed on the system VS2005 or above The Downloadable version comes for VS2008

Client Side Requirements


1. Dotnet Framework 2.0 or above in which the application is made
2. WSS3.0 or MOSS

Solution


Now lets start developing the application
1. Create a class library project from VS 2005/2008 with the name "WikiEventHanlder". You can even use any name of your choice I am using this for my example.

2. Rename the class as "WikiEvent.cs" .
3. Add sharepoint refernce to the project (Microsoft.Sharepoint)
4. Add following code to ."WikiEvent.cs". in this we inherit the class with "IListEventSink" class


using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Security.Principal;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;

namespace WikiEventHanlder
{
///
/// Summary description for Class1.
///


public class WikiEvent: IListEventSink
{


#region IListEventSink Members

public void OnEvent(SPListEvent listEvent)
{
//Open the current context site
SPWeb myWeb = listEvent.Site.OpenWeb();
//Get the target Event hanlder source name as file name
SPFile fileItem = myWeb.GetFile(listEvent.UrlAfter);

//get the folder structure of the source
SPFolder folderItem = myWeb.GetFolder(listEvent.UrlAfter);

//assign a delimiter to get the target url to add the folders
string delimiter = "/";
//get the collection atleast 10 of folder names under the parent folder of the source of the event i.e wiki pages library
string[] subFolder = folderItem.ParentFolder.Url.Split(delimiter.ToCharArray(), 10);
//get all folders of the site
SPFolderCollection folders= myWeb.Folders;
SPFolder targetfolder=null,targetFolder1=null;
//Get the page name of the created page
string pageName = fileItem.Name;
//remove .aspx from the name
int index=pageName.IndexOf(".aspx");
pageName = pageName.Remove(index, 5);
pageName = pageName.Trim();

//traverse to Shared documents folder
foreach (SPFolder folder in folders)
{
if (folder.Name == "Shared Documents")
{
targetfolder = folder;
break;
}
}
//traverse to Images folder
foreach (SPFolder folder in folders)
{
if (folder.Name == "Images1")
{
targetFolder1 = folder;
break;
}
}
//create the folders under them
switch(listEvent.Type.ToString())
{
case "Update":
if (subFolder.Length < 2)
{
create_folders(listEvent, myWeb, targetfolder, pageName);
create_folders(listEvent, myWeb, targetFolder1, pageName);
}
break;

default: break;
}


}

#endregion
/* creates the folder for the
* list Event on the current myWeb and under folderItem with name pageName*/
private void create_folders(SPListEvent listEvent,
SPWeb myWeb,
SPFolder folderItem ,
string pageName)
{
SPFolder tempFolder=folderItem.SubFolders.Add(pageName);
}

}
}


5. Strong name the project by going to project properties->signing

6. Compile the project .

7. Add the dll to GAC.
* By dragging and dropping the dll to C:\WINDOWS\assembly
(or)
* using the command at Visual studio command prompt
GACUTIL.exe /i [path]/WikiEventHanlder.dll /f .

8. Go to Sharepoint Central Adminsitration(start->settings->control panel->adminstrative tools) and
* goto Application Management > Web Application General Settings
* select the site collection you require and godown to Backward- Compatible Event Handlerssection and make it "on"
* Refer figure1 and Figure2 for help.
9. Browse to your site and goto the WikiPage site.

10. Goto the Page library named as "Wiki Pages",click on Settings then move to Document Library settings
in that goto Advanced settings section and move to Backward-Compatible Event Handler section .

11. In this page add details of the assembly inthe assembly name text box as
WikiEventHanlder,Version=1.0.0.0,Culture=neutral,PublicKeyToken=5b283fb82a61470c

In the above line take the public key token value from GAC by right clicking on the assembly or dll(browse to C:\WINDOWS\assembly for the dll)

12. In the class name box enter,
WikiEventHanlder.WikiEvent

Refer figure3 for the above steps 11,12

13. click on OK button,if your assembly has any errors then error will be given otherwise you will be moved to the settings page back.

14. Now goto the Wiki Pages and create a page with any name(lets say samplePage) this will automatically creates one folder under Shared Documents with name "samplePage" another under Images(if exists) with same name as "samplePage".

15. Goto Shared Documents, Images and check the folders created.

Hope this helps someone
Happy Coding..
Shabana

download the code attached.

Attachments




figure1



figure2



figure3
  • WikiEvent.cs (581-19713-packageDocumentHandler.cs.txt)
  • WikiEvent.cs (581-19719-WikiEvent.cs.txt)

  • Project Feedbacks

    Author: Member Level: BronzeRevenue Score: 1 out of 5
    good article

    but this can be done in a simpler way after writing code





    Author: Member Level: GoldRevenue Score: 2 out of 52 out of 5
    Hi,
    Its really nice. I would like learn such type of things.

    It will be greatful for me to send the files.

    Thanks
    Siva Sreenath


    Post Feedback
    You must Sign In to post a feedback.
    Next Project: OFFICE AUTOMATION SYSTEM
    Previous Project: Service Handler ver 1.0

    Return to Project Index

    Post New Project


    Related Projects


    Top Contributors
    Today
      Last 7 Daysmore...

      Awards & Gifts

      Online Members

      More...
       
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India