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.

    /// <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 web, SPList list)        {            StringBuilder sb = new StringBuilder(string.Empty);            try            {                SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();                eventReceiver.Name = list.Title + EventType;                eventReceiver.Type = this.EventType;                eventReceiver.Assembly = this.AssemblyFullName;                eventReceiver.Class = this.ClassName;                eventReceiver.Data = this.Data;                eventReceiver.Update();                list.Update();            }            catch (Exception ex1)            {                sb.AppendLine("Could 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 extends pointblog {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:

Author: Spring Yang

Source: http://www.cnblogs.com/springyangwc/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.