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

Deleting an Event Receiver using STSADM

I had some time so I decided to add the counterparts to the gl-addeventreceiver command that I just published.  I created two new commands, gl-deleteeventreceiver and gl-enumeventreceivers.  This post will cover the delete command and I'll go over the enum command in my next post.

From a code perspective the delete command is very similar to the add command.  The main difference (aside from the obvious call to Delete instead of Add) is that I've made the assembly, class, and type parameters optional (though at least one must be provided) so the code to get the event receiver to delete needed to be expanded so that it now evaluates each receiver and checks to see if a valid match is found.  Once all the receivers have been identified then we simply delete them all.

   1: /// <summary>
   2: /// Deletes an event receiver from the specified target
   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: /// <param name="assembly">The assembly.</param>
   8: /// <param name="className">Name of the class.</param>
   9: /// <param name="type">The type.</param>
  10: /// <param name="typeNotProvided">if set to <c>true</c> [type not provided].</param>
  11: public static void Delete(string url, string contentTypeName, TargetEnum target, string assembly, string className, SPEventReceiverType type, bool typeNotProvided)
  12: {
  13:     using (SPSite site = new SPSite(url))
  14:     using (SPWeb web = site.OpenWeb())
  15:     {
  16:         SPEventReceiverDefinitionCollection eventReceivers;
  17:         if (target == TargetEnum.List)
  18:         {
  19:             SPList list = Utilities.GetListFromViewUrl(web, url);
  20:  
  21:             if (list == null)
  22:             {
  23:                 throw new Exception("List not found.");
  24:             }
  25:             eventReceivers = list.EventReceivers;
  26:         }
  27:         else if (target == TargetEnum.Site)
  28:             eventReceivers = web.EventReceivers;
  29:         else
  30:         {
  31:             SPContentType contentType = null;
  32:             try
  33:             {
  34:                 contentType = web.AvailableContentTypes[contentTypeName];
  35:             }
  36:             catch (ArgumentException)
  37:             {
  38:             }
  39:             if (contentType == null)
  40:                 throw new SPSyntaxException("The specified content type could not be found.");
  41:  
  42:             eventReceivers = contentType.EventReceivers;
  43:         }
  44:         Delete(eventReceivers, type, assembly, className, typeNotProvided);
  45:     }
  46: }
  47:  
  48: /// <summary>
  49: /// Deletes the event receiver matching the provided values from the passed in collection.
  50: /// </summary>
  51: /// <param name="eventReceivers">The event receivers.</param>
  52: /// <param name="eventReceiverType">Type of the event receiver.</param>
  53: /// <param name="assembly">The assembly.</param>
  54: /// <param name="className">Name of the class.</param>
  55: /// <param name="typeNotProvided">if set to <c>true</c> [type not provided].</param>
  56: private static void Delete(SPEventReceiverDefinitionCollection eventReceivers, SPEventReceiverType eventReceiverType, string assembly, string className, bool typeNotProvided)
  57: {
  58:     List<SPEventReceiverDefinition> toDelete = new List<SPEventReceiverDefinition>();
  59:     foreach (SPEventReceiverDefinition erd in eventReceivers)
  60:     {
  61:         if ((erd.Assembly == assembly || assembly == null) && 
  62:             (erd.Class == className || className == null) && 
  63:             ((erd.Type == eventReceiverType && !typeNotProvided) || typeNotProvided))
  64:         {
  65:             toDelete.Add(erd);
  66:         }
  67:     }
  68:     foreach (SPEventReceiverDefinition erd in toDelete)
  69:     {
  70:         Log("Deleting the {0} event for class {1} in assembly {2}.", erd.Type.ToString(), erd.Class,
  71:             erd.Assembly);
  72:  
  73:         erd.Delete();
  74:     }
  75: }

The help for the command is shown below:

C:\>stsadm -help gl-deleteeventreceiver

stsadm -o gl-deleteeventreceiver


Deletes an event receiver from a list, web, or content type.

Parameters:
        -url <web or list URL>
        -target <site | list | contenttype>
        [-assembly <assembly>]
        [-class <class name>]
        [-type <itemadding | itemupdating | itemdeleting | itemcheckingin | itemcheckingout | itemuncheckingout | itemattachmentadding | itemattachmentdeleting | itemfilemoving | fieldadding | fieldupdating | fielddeleting | sitedeleting | webdeleting | webmoving | itemadded | itemupdated | itemdeleted | itemcheckedin | itemcheckedout | itemuncheckedout | itemattachmentadded | itemattachmentdeleted | itemfilemoved | itemfileconverted | fieldadded | fieldupdated | fielddeleted | sitedeleted | webdeleted | webmoved | emailreceived | contextevent | invalidreceiver>]
        [-contenttype <content type name if target is ContentType>]

The following table summarizes the command and its various parameters:

Command Name Availability Build Date
gl-deleteeventreceiver 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 remove the event receiver from. -url http://portal/pages
assembly a No The fully qualified assembly name containing the event receiver class to delete from the target. -assembly "Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08"

-a "Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08"
class c No The fully qualified class name of the event receiver to delete from the target. -class Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver

-c Lapointe.WebPartPageHistory.ListEventReceivers.SourceListEventReceiver
type   No The event type to delete. -type itemupdated
target   No The target type of which the receiver will be deleted.  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 remove the event receiver from if the target is contenttype. -contenttype "Page"

-ct "Page"

The following is an example of how to delete all the event receivers belonging to the web part page history assembly:

stsadm -o gl-deleteeventreceiver -url http://portal/pages -assembly "Lapointe.WebPartPageHistory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3216c23aba16db08" -target list

Note that you should be particularly careful when deleting event receivers and not specifying the assembly and class attributes as you could inadvertently delete event receivers that are critical to the normal functioning of the list.

No comments: