Export (0) Print
Expand All

Walkthrough: Debug a SharePoint Application by Using IntelliTrace

By using IntelliTrace, you can more easily debug applications, including those that incorporate web pages, such as SharePoint solutions. Traditional debuggers give you only a snapshot of the state of your application at the current moment. However, you can use IntelliTrace to review, and even navigate to, past events that occurred in your application and the context in which the events occurred.

This walkthrough demonstrates how to use IntelliTrace to debug a SharePoint project in Visual Studio 2010. This project incorporates a feature receiver that, when the feature is activated, adds a task to the Task list and an announcement to the Announcements list. When the feature is deactivated, the task is marked as completed, and a second announcement is added to the Announcements list. However, the procedure contains a logical error that prevents the project from running correctly. By using IntelliTrace, you will locate and correct the error.

This walkthrough illustrates the following tasks:

  • Creating a feature and a feature event receiver.

  • Responding to feature events by using code.

  • Referencing the Task and Announcement lists by using code.

  • Finding and manipulating list elements by using code.

  • Using IntelliTrace to locate and correct a code error.

  • NoteNote

    Your computer might differ from the instructions in this topic in terms of names or locations for some elements of the user interface. These elements vary based on your settings and your edition of Visual Studio. For more information, see Working with Settings.

You need the following components to complete this walkthrough:

First, you create an empty SharePoint project with a feature receiver.

To create a feature receiver

  1. Start Visual Studio by using the Run as Administrator option.

  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  3. At the top of the dialog box, click .NET Framework 3.5 in the list if it is not already selected.

  4. Under the language that you want to use, expand the SharePoint node, and then click the 2010 node.

  5. In the Templates pane, click Empty SharePoint Project, change the project name to IntelliTraceTest, and then click OK.

    The SharePoint Customization Wizard appears, in which you can specify both the SharePoint site for your project and the trust level of the solution.

  6. Click Deploy as a farm solution, and then click Finish.

    IntelliTrace operates only on farm solutions.

  7. In Solution Explorer, right-click the Features node, and then click Add Feature.

    Feature1.feature appears.

  8. Right-click Feature1.feature, and then click Add Event Receiver to add a code module to the feature.

Next, add code to two methods in the feature receiver: FeatureActivated and FeatureDeactivating. These methods trigger whenever a feature is activated or deactivated in SharePoint, respectively.

To add code to the feature receiver

  1. At the top of the Feature1.EventReceiver class, add the following code to declare variables that specify the SharePoint site and subsite:

    
    // SharePoint site/subsite.
    private string siteUrl = "http://localhost";
    private string webUrl = "/";
    
    
    
  2. Replace the FeatureActivated method with the following code:

    
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        try
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    // Reference the lists.
                    SPList announcementsList = web.Lists["Announcements"];
                    SPList taskList = web.Lists["Tasks"];
    
                    // Add a new announcement to the Announcements list.
                    SPListItem listItem = announcementsList.Items.Add();
                    listItem["Title"] = "Activated Feature: " + properties.Definition.DisplayName;
                    listItem["Body"] = properties.Definition.DisplayName + " was activated on: " + DateTime.Now.ToString();
                    listItem.Update();
    
                    // Add a to-do task to the Task list.
                    SPListItem newTask = taskList.Items.Add();
                    newTask["Title"] = "Deactivate feature: " + properties.Definition.DisplayName;
                    newTask.Update();
                }
            }
        }
    
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.ToString());
        }
    
    }
    
    
    
  3. Replace the FeatureDeactivating method with the following code:

    
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        try
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    // Reference the lists
                    SPList taskList = web.Lists["Tasks"];
                    SPList announcementsList = web.Lists["Announcements"];
    
                    // Add an announcement that the feature was deactivated.
                    SPListItem listItem = announcementsList.Items.Add();
                    listItem["Title"] = "Deactivated Feature: " + properties.Definition.DisplayName;
                    listItem["Body"] = properties.Definition.DisplayName + " was deactivated on: " + DateTime.Now.ToString();
                    listItem.Update();
    
                    // Find the task the feature receiver added to the Task list when the
                    // feature was activated.
                    SPQuery qry = new SPQuery();
                    qry.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Deactive</Value></Contains></Where>";
                    SPListItemCollection taskItems = taskList.GetItems(qry);
    
                    foreach (SPListItem taskItem in taskItems)
                    {
                        // Mark the task as complete.
                        taskItem["PercentComplete"] = 1;
                        taskItem["Status"] = "Completed";
                        taskItem.Update();
                    }
                }
            }
    
        }
    
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.ToString());
        }
    }
    
    
    

Now that the code is added to the feature receiver, run the SharePoint application to test whether it works correctly. For this example, a small error is included in the code. After the error occurs, you will use IntelliTrace to track down the problem.

To test the project

  1. Press F5 to run the project.

    Upon deployment, the feature automatically activates, causing its feature receiver to add an announcement and a task.

  2. After SharePoint starts, click Lists in the Navigation pane, and then click both the Announcements list and the Tasks list to view their contents.

    In the Announcements list, a new announcement named Activated feature: IntelliTraceTest_Feature1 was added, and a new task named Deactivate feature: IntelliTraceTest_Feature1 was added to the Tasks list. The task’s status is “Not Started.”

  3. Deactivate the feature by clicking Manage site features under Site Actions, clicking Deactivate next to IntelliTraceTest Feature1, and then clicking the Deactivate this feature link in the Warning page.

    The task’s Status value should now be “Completed,” and its % Complete value should be “100%.” Instead, the values are still at their default settings. An error in the code prevents the task from being updated.

Use IntelliTrace to locate and fix the problem in the code.

To debug the project

  1. In the FeatureDeactivating method, locate the SPQuery qry = new SPQuery(); line, and press F9 to insert a breakpoint on that line.

    Because the problem occurs when the feature is deactivated, this line is a logical place to start debugging.

  2. Press F5 to run the program again, and activate and then deactivate the feature by repeating the steps in “To test the project” earlier in this topic.

    When the breakpoint is hit in FeatureActivated, the IntelliTrace window appears and lists all of the steps that the application has taken so far.

  3. Press F11 to step through each line of code until the program completes.

    Each time that you press F11, another “Debugger:” line is added to the application’s IntelliTrace debugging history.

  4. After the program completes, click the Break All link in the IntelliTrace window.

    You must take this step to save the IntelliTrace data.

    Optionally, to view call information for the application, click Open IntelliTrace Settings on the IntelliTrace toolbar, and then click IntelliTrace events and call information.

  5. Save the debugging data by clicking Save the current IntelliTrace session on the IntelliTrace toolbar.

    The file will have an .iTrace extension.

  6. In Visual Studio, click Stop Debugging.

  7. Open the .iTrace file.

    This step opens another instance of Visual Studio showing the IntelliTrace Summary page, which provides debugging information such as exception data and a list of threads that are used in the program.

  8. Expand the Threads List, click Main Thread, and then click Start Debugging.

    This step starts a debugging session in Visual Studio using the .iTrace data. To view additional details about an event in the IntelliTrace window, click it.

  9. Because we suspect the error occurs in the FeatureDeactivating method, click the Locals link in the Debugger: step for SPListItemCollection taskItems = taskList.GetItems(qry);.

    The Locals window appears.

  10. In the Locals window, expand the variable list, locate qry.Query, and verify that the query for locating the task incorrectly searches for “Deactive” rather than “Deactivate.”

    This error means that the task is never found in the query of the Tasks list.

Now that you have identified the problem by using IntelliTrace, correct the error, and then retest the project.

To retest the project

  1. Close the debug session of Visual Studio, and then reopen the IntelliTraceTest project if it is not already open.

  2. In the code’s query string (qry.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Deactive</Value></Contains></Where>"), change the value from Deactive to Deactivate.

  3. Press F5 to run the project again.

  4. Open the Task list, and verify that the Deactivate task’s Status value is now correctly set to “Completed” and its % Complete value is 100%.

Community Additions

ADD
Show:
© 2014 Microsoft