Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

.net - Attach EventReceiver to all lists of a sharepoint site

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  1. Tried to add EventReceiver to System (0x). Called method ContentType.Update(true,false) which updates all child elements - nor did System ContentType updated, nor childs. Turns out that you cannot modify those contentTypes with ReadOnly || Sealed attribute set to TRUE
  2. Tried to add EventReceiver to Item (0x01) ContentType. As seen from here, all ContentTypes 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 child ContentTypes (all content types except those ReadOnly or Sealed)

How to attach a global EventReceiver to all items

So a solution which would work would look like this:

  1. Iterate all lists, set Readonly or Sealed to false if you want those content types with you EventReceivers.
  2. Add your EventReceiver to existing content types in lists (SPList.ContentTypes)
  3. Add EventReceiver to ContentType (SPWeb.ContentTypes) with id 0x01 so new lists get EventReceiver added automatically. Newly created content types will also inherit EventReceivers. Also all subweb ContentTypes inherit EventReceivers.

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 SettingsAdministrator> $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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...