Step by step SharePoint Development Learning notes Series 8 and SharePoint EventHandler Development

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.

 

View sourceprint? 01 /// <summary>

 

02 // EventHandler

 

03 // </summary>

 

04 class EventHandler

 

05 {

 

06 private string _ assemblyFullName = string. Empty;

 

07 public string AssemblyFullName

 

08 {

 

09 get

 

10 {

 

11 return _ assemblyFullName;

 

12}

 

13}

 

14 private string _ className = string. Empty;

 

15 public string ClassName

 

16 {

 

17 get

 

18 {

 

19 return _ className;

 

20}

 

21}

 

22 private SPEventReceiverType _ eventType;

 

23 private SPEventReceiverType EventType

 

24 {

 

25 get

 

26 {

 

27 return _ eventType;

 

28}

 

29}

 

30 private string _ data = string. Empty;

 

31 public string Data

 

32 {

 

33 get

 

34 {

 

35 return _ data;

 

36}

 

37}

 

38 private bool _ deleteExistingEvents = false;

 

39 public bool DeleteExistingEvents

 

40 {

 

41 get

 

42 {

 

43 return _ deleteExistingEvents;

 

44}

 

45}

 

46

 

47 // <summary>

 

48 // EventHandler

 

49 // </summary>

 

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

 

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

 

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

 

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

 

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

 

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

 

56 {

 

57 this. _ eventType = eventType;

 

58 this. _ data = data;

 

59 this. _ assemblyFullName = handlerAssembly;

 

60 this. _ className = handlerClass;

 

61 this. _ deleteExistingEvents = deleteExisting;

 

62}

 

63 // <summary>

 

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

 

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

 

66 /// </summary>

 

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

 

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

 

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

 

70 public string RegisterEvent (SPWeb, SPList list)

 

71 {

 

72 StringBuilder sb = new StringBuilder (string. Empty );

 

73 try

 

74 {

 

75 SPEventReceiverDefinition eventReceiver = list. EventReceivers. Add ();

 

76 eventcycler. Name = list. Title + EventType;

 

77 eventcycler. Type = this. EventType;

 

78 eventcycler. Assembly = this. AssemblyFullName;

 

79 eventcycler. Class = this. ClassName;

 

80 eventcycler. Data = this. Data;

 

81 eventReceiver. Update ();

 

82 list. Update ();

 

83}

 

84 catch (Exception ex1)

 

85 {

 

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

 

87}

 

88 return sb. ToString ();

 

89}

 

90

 

91}

 

Event Handler:

 

View sourceprint? 01 using System;

 

02 using System. Collections. Generic;

 

03 using System. Linq;

 

04 using System. Text;

 

05

 

06 using Microsoft. SharePoint;

 

07 using System. IO;

 

08

 

09 namespace SharePointBlog

 

10 {

 

11 public class testeven thandle: SPItemEventReceiver

 

12 {

 

13 public override void ItemUpdated (SPItemEventProperties properties)

 

14 {

 

15 try

 

16 {

 

17 SaveLog ();

 

18

 

19}

 

20 catch (Exception ex)

 

21 {

 

22 properties. ErrorMessage = ex. Message;

 

23 properties. Cancel = true;

 

24}

 

25}

 

26

 

27 public void SaveLog ()

 

28 {

 

29 string path = @ "c: \ Log.txt ";

 

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

 

31 StreamWriter writer = new StreamWriter (path );

 

32 writer. Write (text );

 

33 writer. Close ();

 

34}

 

35

 

36}

 

37}

 

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.