MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Saturday, September 13, 2008

Listing Event Receivers using STSADM

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>
   8: public 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:

<Receivers>
  <Receiver>
    <Name />
    <Type>ItemUpdating</Type>
    <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
    <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name>PagesListEventReceiverName</Name>
    <Type>ItemDeleting</Type>
    <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
    <SequenceNumber>1005</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name>PagesListEventReceiverName</Name>
    <Type>ItemAdded</Type>
    <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
    <SequenceNumber>1004</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name>PagesListEventReceiverName</Name>
    <Type>ItemUpdated</Type>
    <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
    <SequenceNumber>1002</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name />
    <Type>ItemUpdated</Type>
    <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
    <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name>PagesListEventReceiverName</Name>
    <Type>ItemDeleted</Type>
    <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
    <SequenceNumber>1006</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name>PagesListEventReceiverName</Name>
    <Type>ItemCheckedIn</Type>
    <Assembly>Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <Class>Microsoft.SharePoint.Publishing.PagesListCPVEventReceiver</Class>
    <SequenceNumber>1003</SequenceNumber>
  </Receiver>
  <Receiver>
    <Name />
    <Type>ItemCheckedIn</Type>
    <Assembly>Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08</Assembly>
    <Class>Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>
</Receivers>

No comments: