Debugging an SPItemEventReceiver

This QuickStart demonstrates how to debug an SPItemEventReceiver that was created with Visual Studio extensions for Windows SharePoint Services version 1.3. The SharePoint solution contains a site definition that defines a sample content type, a list definition, and a list instance with an item event receiver. The SPGDebuggingQuickStart list contains two custom fields named MyDateTimeField1 and MyDateTimeField2. The MyDateTimeField2 field is hidden. It is populated by adding one day to the MyDateTimeField1 field.

Building and Running the QuickStart

The QuickStart ships as source code. This means you must first compile it before you can run it.

To build and run the Debugging an SPItemEventReceiver QuickStart

  1. In Visual Studio, open the SPGDebuggingQuickStart.sln solution file. For information about how to manually debug a SharePoint application and how to attach to the w3wp process, see How to: Debug SharePoint Applications.

  2. On the Build menu, click Rebuild Solution.

  3. Right-click SPGDebuggingQuickStart, and then click Deploy

    Note

    The deployment process may fail if the Visual Studio extensions for Windows SharePoint Services application pool identity does not have permissions to deploy to https://localhost. To correct this, add the Visual Studio extensions for Windows SharePoint Services application pool identity as a full control user on the https://localhost site collection. After you do this, repeat step 3.

The next procedure demonstrates how to use the QuickStart.

To debug an SPItemEventReceiver

  1. When you start the QuickStart, you should see the SharePoint site. If the browser does not automatically open, browse to https://localhost.

  2. Click the Site Actions tab, and then click Create (or click Create Site if the localhost is a Team Site). The Create page appears. In the Web Pages column, click the Sites and Workspaces link. The following illustration shows the Create page.

    Sites and workspaces page

    Ff650393.489e5545-1ee5-44ff-957c-73501d73ae1a(en-us,PandP.10).png

  3. In the Title box, type Debugging QuickStart. In the URL name box, type spgdebuggingquickstart. Under Select a template, click the Development tab, click SPGDebuggingQuickStart, and then click Create. The following illustration shows the site page.

    SPGDebugging configuration

    Ff650393.d0000012-1c28-43cc-85c7-f3a5599a214b(en-us,PandP.10).png

  4. After the site is created, click the Debugging QuickStart tab. In the Quick Launch, click SPGDebuggingQuickStartList, and then click New on the toolbar. Provide values for the Title and MyDateTimeField 1 boxes. Click OK. Notice that the MyDateTimeField 2 field is not shown because it is unpopulated. The following illustration shows the list form.

    New list item form

    Ff650393.82f58936-61bc-4a82-a0d2-685e7f0be56f(en-us,PandP.10).png

Debugging Techniques

The following code demonstrates the incorrect way to access and set a DateTime field from the AfterProperties collection.

public override void ItemAdding(SPItemEventProperties properties)
{
    DateTime date1 = (DateTime)properties.AfterProperties["MyDateTimeField1"];
    properties.AfterProperties["MyDateTimeField2"] = date1.AddDays(1);
}

To debug the list

  1. In Visual Studio, place a breakpoint in the ItemAdding method, click Attach to process on the Tools menu.

  2. Attach to all the W3wp.exe processes.

  3. Navigate to the SPGDebuggingQuickStartList through the link on the QuickStart, and then add a new item to the list. The following illustration shows the exception.

    Debugging information

    Ff650393.b5b7842c-723f-4eb9-a1e6-f4db2a1d87a4(en-us,PandP.10).png

The following code demonstrates one way to correct the exception.

public override void ItemAdding(SPItemEventProperties properties)
{
  DateTime date1 = new DateTime();
  if (DateTime.TryParse((string)properties.AfterProperties["MyDateTimeField1"], out date1))
  {
    properties.AfterProperties["MyDateTimeField2"] = date1.ToUniversalTime().AddDays(1).ToString("yyyy-MM-dd");
  }
  else
  {
    properties.Cancel = true;
    properties.ErrorMessage = "An error occurred.";
  }
}

For more information about debugging, see How to: Debug SharePoint Applications.

Home page on MSDN | Community site