A simple document library event handler

Source: Internet
Author: User
This is a simple eventhandler, that is, when an item is inserted and updated in document lirary, the value of a field is changed to the URL of the file.
Eventhandler for compiling doucment library must inherit the ilisteventsink Interface
Code As follows:

1 Public   Class Documentlibraryevent: ilisteventsink
2 {
3 Ilisteventsink members # Region Ilisteventsink members
4
5 Public   Void Onevent (splistevent listevent)
6 {
7 If (Listevent. Type = Splisteventtype. insert | Listevent. Type = Splisteventtype. Update)
8 {
9 Spweb = Listevent. Site. openweb ();
10 If (Web. Lists [listevent. listid]. Fields. containsfield ( " Selected " ))
11 {
12 // String colname = web. Lists [listevent. listid]. Fields ["selected"]. internalname;
13
14 String Urlval = Listevent. weburl +   " / "   + Listevent. urlafter;
15 // Listevent. propertiesafter [colname] = urlval;
16 Spfile File = Web. GetFile (listevent. urlafter );
17
18 While (File. checkoutstatus ! = Spfile. spcheckoutstatus. None)
19 {< br> 20 system. threading. thread. sleep ( 1000 );
21 file = Web. getFile (listevent.
22 }
23
24 File. item [ " Selected " ] = Urlval;
25 File. item. Update ();
26 }
27 }
28 }
29
30 # Endregion
31 }

This code is easy to understand. It determines that it is insert and update and contains this field, and then obtains the URL to modify the field value.
The following code is used to modify the value of the selected field when creating a document and throw the checkout exception.
This code is from Jan tielens 'bloggings

1
2 While (File. checkoutstatus ! = Spfile. spcheckoutstatus. None)
3 {< br> 4 system. threading. thread. sleep ( 1000 );
5 file = Web. getFile (listevent.
6 }

This code can meet the requirements, but it is always not beautiful.
It seems that it should be modified like this:
Listevent. propertiesafter [colname] = urlval;
But I tried it and it didn't work!

Deployment:
1) First, enventhandler of the document library should be opened, and in central administration-> Application Management
-> Web Application General settings
Find the web application you want to change and set

2) Put the compiled DLL into GAC
3) add an event to a document library> Settings> advanced settings.ProgramSet and class

In this way, the event will be executed.

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.