Debugging an SPItemEventReceiver
Collapse the table of content
Expand the table of content

Debugging an SPItemEventReceiver

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This QuickStart demonstrates how to debug an SPItemEventReceiver that was created with Visual Studio extensions for Windows SharePoint Services. 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. Press F5 to run the QuickStart.

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 http://localhost.
  2. Click the Site Actions tab, and then click Create. The Create page appears. In the Web Pages column, click the Sites and Workspaces link. Figure 1 illustrates the Create page.

    Ff647521.root_create(en-us,PandP.10).png

    Figure 1
    Sites and workspaces page

  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. Figure 2 illustrates the Sites page.

    Ff647521.root_debuggingtemplate(en-us,PandP.10).png

    Figure 2
    SPGDebugging configuration

  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. Figure 3 illustrates the list form.

    Ff647521.spgdebugging_newitem(en-us,PandP.10).png

    Figure 3
    New list item form

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);
}

In Visual Studio, place a breakpoint in the ItemAdding method, and then press F5. Navigate to the SPGDebuggingQuickStartList through the link on the QuickStart, and then add a new item to the list.

Figure 4 illustrates the exception.

Ff647521.spgdebugging_exception(en-us,PandP.10).png

Figure 4
Debugging information

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 in Development Activities.


Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Show:
© 2016 Microsoft