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.