Step by step SharePoint Development Study Notes Series 7, SharePoint EventHandler

Source: Internet
Author: User

 

Summary

 

The EventHandler of SharePoint mainly includes Web Level, List Level, List Item Level, and Email. The event handler of SharePoint inherits the SPWebEventReceiver, SPEmailEventReceiver, splisteventventer, and SPItemEventReceiver classes to implement corresponding methods to meet our needs.

 

 

 

Development and Design

 

Register events.

 

/// <Summary>

/// EventHandler

/// </Summary>

Class EventHandler

{

Private string _ assemblyFullName = string. Empty;

Public string AssemblyFullName

{

Get

{

Return _ assemblyFullName;

}

}

Private string _ className = string. Empty;

Public string ClassName

{

Get

{

Return _ className;

}

}

Private SPEventReceiverType _ eventType;

Private SPEventReceiverType EventType

{

Get

{

Return _ eventType;

}

}

Private string _ data = string. Empty;

Public string Data

{

Get

{

Return _ data;

}

}

Private bool _ deleteExistingEvents = false;

Public bool DeleteExistingEvents

{

Get

{

Return _ deleteExistingEvents;

}

}

 

/// <Summary>

/// EventHandler

/// </Summary>

/// <Param name = "handlerAssembly"> </param>

/// <Param name = "handlerClass"> </param>

/// <Param name = "eventType"> </param>

/// <Param name = "data"> </param>

/// <Param name = "deleteExisting"> </param>

Public EventHandler (string handlerAssembly, string handlerClass, SPEventReceiverType eventType, string data, bool deleteExisting)

{

This. _ eventType = eventType;

This. _ data = data;

This. _ assemblyFullName = handlerAssembly;

This. _ className = handlerClass;

This. _ deleteExistingEvents = deleteExisting;

}

/// <Summary>

/// Registers the event handler on the list with the specified name in the specified web.

/// The function will not throw errors, but will return a string with any error or success description (a log)

/// </Summary>

/// <Param name = "web"> The SPWeb object of the sharepoint web site containing the list to attach the event handler to </param>

/// <Param name = "listName"> The name of the list to attach the event to </param>

/// <Returns> A log of the event assignment </returns>

Public string RegisterEvent (SPWeb, SPList list)

{

StringBuilder sb = new StringBuilder (string. Empty );

Try

{

SPEventReceiverDefinition eventReceiver = list. EventReceivers. Add ();

Eventcycler. Name = list. Title + EventType;

Eventcycler. Type = this. EventType;

Eventcycler. Assembly = this. AssemblyFullName;

Eventcycler. Class = this. ClassName;

Eventcycler. Data = this. Data;

Eventcycler. Update ();

List. Update ();

}

Catch (Exception ex1)

{

Sb. AppendLine ("cocould not register the event on the list in this site:" + Environment. NewLine + ex1.Message );

}

Return sb. ToString ();

}

 

}

 

 

 

Event Handler:

 

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

 

Using Microsoft. SharePoint;

Using System. IO;

 

Namespace SharePointBlog

{

Public class testeven thandle: SPItemEventReceiver

{

Public override void ItemUpdated (SPItemEventProperties properties)

{

Try

{

SaveLog ();

 

}

Catch (Exception ex)

{

Properties. ErrorMessage = ex. Message;

Properties. Cancel = true;

}

}

 

Public void SaveLog ()

{

String path = @ "c: \ Log.txt ";

String text = "delete Item" + ":" + DateTime. Now. ToString ();

StreamWriter writer = new StreamWriter (path );

Writer. Write (text );

Writer. Close ();

}

 

}

}

 

 

 

Deploy Event Handler to SharePoint

 

Unlike WebPart, Event Handler's dll needs to be placed in GAC (Global Assembly Cache), rather than in the bin folder of the SharePoint website, therefore, the generated dll must be strongly signed, which is why the above key file is added.

 

The system path of GAC is C: \ WINDOWS \ assembly. Drag the generated Event Handler dll to this path.

 

 

 

 

The code is very easy. You can see it at a glance, find the executable file generated by the project, and run:

 

 

In this way, our Event Handler is successfully released! Now, the deployment of Event Handler is complete!

 

 

Finally, let's test and go to the document library and delete the word file of the "LINQ Chinese tutorial. Go to C: \ To see log.txt:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.