This post wraps up my event receiver posts. I just finished documenting the gl-addeventreceiver
and gl-deleteeventreceiver
commands and this final command would be gl-enumeventreceivers
.
This command is the simplest of the three as it’s just looping through all the event receivers belonging to the specified target and dumping the results out as XML. I’ve tried to structure the XML so that it conforms to the CAML schema therefore allowing the results to be put into a Feature if desired.
1/// <summary>
2/// Gets the XML.
3/// </summary>
4/// <param name="url">The URL.</param>
5/// <param name="contentTypeName">Name of the content type.</param>
6/// <param name="target">The target.</param>
7/// <returns></returns>
8public static string GetXml(string url, string contentTypeName, TargetEnum target)
9{
10 using (SPSite site = new SPSite(url))
11 using (SPWeb web = site.OpenWeb())
12 {
13 SPEventReceiverDefinitionCollection eventReceivers;
14 if (target == TargetEnum.List)
15 {
16 SPList list = Utilities.GetListFromViewUrl(web, url);
17
18 if (list == null)
19 {
20 throw new Exception("List not found.");
21 }
22 eventReceivers = list.EventReceivers;
23 }
24 else if (target == TargetEnum.Site)
25 eventReceivers = web.EventReceivers;
26 else
27 {
28 SPContentType contentType = null;
29 try
30 {
31 contentType = web.AvailableContentTypes[contentTypeName];
32 }
33 catch (ArgumentException)
34 {
35 }
36 if (contentType == null)
37 throw new SPSyntaxException("The specified content type could not be found.");
38
39 eventReceivers = contentType.EventReceivers;
40 }
41 StringBuilder sb = new StringBuilder();
42 XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(sb));
43 xmlWriter.Formatting = Formatting.Indented;
44
45 xmlWriter.WriteStartElement("Receivers");
46 foreach (SPEventReceiverDefinition erd in eventReceivers)
47 {
48 xmlWriter.WriteStartElement("Receiver");
49
50 xmlWriter.WriteElementString("Name", erd.Name);
51 xmlWriter.WriteElementString("Type", erd.Type.ToString());
52 xmlWriter.WriteElementString("Assembly", erd.Assembly);
53 xmlWriter.WriteElementString("Class", erd.Class);
54 xmlWriter.WriteElementString("SequenceNumber", erd.SequenceNumber.ToString());
55
56 xmlWriter.WriteEndElement();
57 }
58 xmlWriter.WriteEndElement();
59
60 return sb.ToString();
61 }
62}
The help for the command is shown below:
C:\>stsadm -help gl-enumeventreceivers
stsadm -o gl-enumeventreceivers
Enumerates all event receivers associated with the specified target object and outputs the list of receivers as XML.
Parameters:
-url <web or list URL>
-target <site | list | contenttype>
[-contenttype <content type name if target is ContentType>]
The following table summarizes the command and its various parameters:
Command Name | Availability | Build Date |
---|---|---|
gl-enumeventreceivers | WSS v3, MOSS 2007 | Released: 9/13/2008 |
Parameter Name | Short Form | Required | Description | Example Usage |
---|---|---|---|---|
url | Yes | The URL to the web or list to display the event receivers. | -url http://portal/pages | |
target | No | The target object to display the event receivers from. Must be either “list”, “site”, or “contenttype”. If omitted defaults to “list”. | -target list | |
contenttype | ct | No, unless target is contenttype | The name of the content type to to get the event receivers from. | -contenttype "Page" , -ct "Page" |
The following is an example of how to display all the event receivers associated with a pages library:
stsadm -o gl-enumeventreceivers -url http://portal/pages -target list
The following is an example output from running the above command:
1<Receivers>
2 <Receiver>
3 <Name />
4 <Type>ItemUpdating</Type>
5 <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
6 <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
7 <SequenceNumber>10000</SequenceNumber>
8 </Receiver>
9 <Receiver>
10 <Name>PagesListEventReceiverName</Name>
11 <Type>ItemDeleting</Type>
12 <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
13 <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
14 <SequenceNumber>1005</SequenceNumber>
15 </Receiver>
16 <Receiver>
17 <Name>PagesListEventReceiverName</Name>
18 <Type>ItemAdded</Type>
19 <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
20 <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
21 <SequenceNumber>1004</SequenceNumber>
22 </Receiver>
23 <Receiver>
24 <Name>PagesListEventReceiverName</Name>
25 <Type>ItemUpdated</Type>
26 <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
27 <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
28 <SequenceNumber>1002</SequenceNumber>
29 </Receiver>
30 <Receiver>
31 <Name />
32 <Type>ItemUpdated</Type>
33 <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
34 <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
35 <SequenceNumber>10000</SequenceNumber>
36 </Receiver>
37 <Receiver>
38 <Name>PagesListEventReceiverName</Name>
39 <Type>ItemDeleted</Type>
40 <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
41 <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
42 <SequenceNumber>1006</SequenceNumber>
43 </Receiver>
44 <Receiver>
45 <Name>PagesListEventReceiverName</Name>
46 <Type>ItemCheckedIn</Type>
47 <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
48 <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
49 <SequenceNumber>1003</SequenceNumber>
50 </Receiver>
51 <Receiver>
52 <Name />
53 <Type>ItemCheckedIn</Type>
54 <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
55 <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
56 <SequenceNumber>10000</SequenceNumber>
57 </Receiver>
58</Receivers>