问题
Can we write an eventreceiver that gets fired when any list is updated. The listtemplateid that we have to specify for an eventreceiver makes our code specefic to one breed of lists. What if we want to have the code execute for events on all lists of site?
回答1:
I have the same exact requirement.
Maybe it is possible to attach event receiver to System ContentType
(all content types inherit that one, with id 0x)
I`d check if that's possible by creating a Feature with a FeatureReceiver and programmatically adding it to System Content Type. Some details here.
My path on finding a solution
Allright, I did some testing with console app.
- Tried to add
EventReceiver
to System (0x). Called method ContentType.Update(true,false) which updates all child elements - nor did SystemContentType
updated, nor childs. Turns out that you cannot modify those contentTypes withReadOnly
||Sealed
attribute set toTRUE
- Tried to add EventReceiver to Item (0x01)
ContentType
. As seen from here, allContentTypes
still inherit from that one (see ContentType hierarchy). There may be custom deployed content types that inherit from System, but not those made in GUI. Updating this content type truly updated all childContentTypes
(all content types except thoseReadOnly
orSealed
)
How to attach a global EventReceiver to all items
So a solution which would work would look like this:
- Iterate all lists, set
Readonly
orSealed
to false if you want those content types with youEventReceivers
. - Add your
EventReceiver
to existing content types in lists (SPList.ContentTypes
) - Add EventReceiver to ContentType (
SPWeb.ContentTypes
) with id 0x01 so new lists get EventReceiver added automatically. Newly created content types will also inheritEventReceivers
. Also all subwebContentTypes
inheritEventReceivers
.
All three steps could be a console application or powershell script. Or a site-collection wide feature with FeatureReceiver
The result
PS C:\Documents and Settings\Administrator> $web.contentTypes |ft id, name, EventReceivers, readonly, sealed
Id Name EventReceivers ReadOnly Sealed
-- ---- -------------- -------- ------
0x Sistēma {} False True
0x007F1DD730DB144C84... Darba kārtības vēsture {} True True
0x01 Ieraksts {, , , ...} False False
0x01003420C661289843... Darba kārtības ieraksts {, , , ...} False False
0x0101 Dokuments {, , , ...} False False
0x010100629D00608F81... Office datu savienoj... {} True False
0x010100B4CBD48E029A... Universālais datu sa... {} True False
0x010101 Veidlapa {, , , ...} False False
0x010102 Attēls {, , , ...} False False
0x010104 Nezināms dokumenta tips {} True False
0x010105 Lapu šablons {, , , ...} False False
0x010107 Lietotāja darbplūsma... {, , , ...} False False
0x010108 Wiki lapa {, , , ...} False False
0x010109 Pamatlapa {, , , ...} False False
0x01010901 Web daļu lapa {, , , ...} False False
0x01010A Saistīt ar dokumentu {, , , ...} False False
0x01010B Dublinas pamata kolo... {, , , ...} False False
0x0102 Notikums {, , , ...} False False
0x0103 Diskutējamais jautājums {, , , ...} False False
0x0104 Paziņojums {, , , ...} False False
0x0105 Saite {, , , ...} False False
0x0106 Kontaktpersona {, , , ...} False False
0x0107 Ziņojums {, , , ...} False False
0x0108 Uzdevums {, , , ...} False False
0x0108007122AD6D76CD... Darba kārtības uzdevums {, , , ...} False False
0x010801 Darbplūsmas uzdevums {, , , ...} False False
0x010802 Administratīvs uzdevums {, , , ...} False False
0x0109 Darbplūsmas vēsture {, , , ...} False False
0x010A Person {, , , ...} False False
0x010B SharePointGroup {, , , ...} False False
0x010C DomainGroup {, , , ...} False False
0x0110 Ziņa {, , , ...} False False
0x0111 Komentārs {, , , ...} False False
0x0116 Tālo Austrumu līgums {, , , ...} False False
0x0120 Mape {} False True
0x012001 RootOfList {} False True
0x012002 Diskusija {, , , ...} False False
Sorry, my WSS is localized, but {, , , ...} means I added several eventreceivers to content types. As you can see those with Readonly
or Sealed
attribute False are untouched.
来源:https://stackoverflow.com/questions/1243406/attach-eventreceiver-to-all-lists-of-a-sharepoint-site