Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
Muhammad Hasan Khan Avatar asked Aug 07 '09 07:08

Muhammad Hasan Khan


People also ask

How do I add an event receiver to a SharePoint list?

Select event receiver and enter the title of the event receiver. Click the Add button. Select List Item Events in the type of event receiver and select the custom list option from the event source. Then select "an item was added" in events and click Finish.

How do you implement a remote event receiver in SharePoint online?

To register a remote event receiverOn the menu bar, select Project > Add New Item. In the Installed Templates pane, select the Office/SharePoint node. In the Templates pane, select the Remote Event Receiver template. In the Name box, keep the default name (RemoteEventReceiver1), and then select Add.

What are the type of event receivers available in SharePoint list?

There are two types of Event Receiver in SharePoint, Synchronous Event Receiver. Asynchronous Event Receiver.

What is SharePoint event receiver?

In an SharePoint Add-in model scenario, event receivers are created outside of SharePoint inside a web service and registered with SharePoint. These are called as Remote Event Receivers (RER). In this scenario, the event receiver code runs on the web server where the web service is hosted.


1 Answers

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

like image 133
Janis Veinbergs Avatar answered Oct 20 '22 12:10

Janis Veinbergs