演练:使用 IntelliTrace 调试 SharePoint 应用程序

利用 IntelliTrace,您可以更轻松地调试应用程序,包括那些合并有网页的应用程序(如 SharePoint 解决方案)。传统的调试器只会为您提供应用程序当前时间的状态的快照。但是,您可以使用 IntelliTrace 来查看(甚至导航到)应用程序中过去发生的事件以及这些事件发生的上下文。

本演练演示如何使用 IntelliTrace 在 Visual Studio 中调试 SharePoint 项目。此项目合并了一个功能接收器,当激活功能时,该接收器将向任务列表添加一个任务并向公告列表添加一个公告。当停用功能时,相应的任务将会标记为已完成,并且会再次向公告列表添加一个公告。但是,这个过程包含一个逻辑错误,会阻止项目正确运行。您将利用 IntelliTrace 来查找并更正该错误。

本演练阐释了以下任务:

  • 创建功能和功能事件接收器。

  • 使用代码响应功能事件。

  • 使用代码引用任务和公告列表。

  • 使用代码查找并操作列表元素。

  • 使用 IntelliTrace 查找并更正代码错误。

  • 说明说明

    您的计算机上的某些用户界面元素的名称或位置可能会与本主题中的说明不同。这些元素会因您的设置和 Visual Studio 的版本而异。有关更多信息,请参见Visual Studio 设置

系统必备

您需要以下组件来完成本演练:

创建功能接收器

首先,创建一个包含功能接收器的空 SharePoint 项目。

创建功能接收器

  1. 使用**“以管理员身份运行”**选项启动 Visual Studio。

  2. 在菜单栏上,选择**“文件”“新建**、“项目”

    此时将出现“新建项目”对话框。

  3. 在对话框顶部,选择在 .NET Framework 的版本列表的 .NET Framework 3.5

  4. 在要使用的语言下,展开 SharePoint 节点,然后选择 2010 节点。

  5. 模板 窗格中,选择 SharePoint 2010 项目 模板,更改项目名称。IntelliTraceTest,然后选择 确定 按钮。

    将显示**“SharePoint 自定义向导”**,您可以在其中指定项目的 SharePoint 站点和解决方案的信任级别。

  6. 选择 部署为场解决方案 选项按钮,然后选择 完成 按钮。

    IntelliTrace 只可用于场解决方案。

  7. 解决方案资源管理器,打开 功能 节点的快捷菜单,然后选择 添加功能

    将显示 Feature1.feature。

  8. 从打开 Feature1.feature 的快捷菜单,然后选择 添加事件接收器 将代码模块添加到该功能。

向功能接收器中添加代码

接下来,向功能接收器中的以下两个方法添加代码:FeatureActivated 和 FeatureDeactivating。只要在 SharePoint 中激活或停用该功能,这两个方法就会分别触发。

向功能接收器中添加代码

  1. 在 Feature1EventReceiver 类的顶部,添加以下代码以声明用于指定 SharePoint 站点和子站点的变量:

    ' SharePoint site/subsite.
    Private siteUrl As String = "https://localhost"
    Private webUrl As String = "/"
    
    // SharePoint site/subsite.
    private string siteUrl = "https://localhost";
    private string webUrl = "/";
    
  2. 用下面的代码替换 FeatureActivated 方法:

    Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
        Try
            Using site As New SPSite(siteUrl)
                Using web As SPWeb = site.OpenWeb(webUrl)
                    ' Reference the lists.
                    Dim announcementsList As SPList = web.Lists("Announcements")
                    Dim taskList As SPList = web.Lists("Tasks")
    
                    ' Add a new announcement to the Announcements list.
                    Dim listItem As SPListItem = announcementsList.Items.Add()
                    listItem("Title") = "Activated Feature: " & Convert.ToString(properties.Definition.DisplayName)
                    listItem("Body") = Convert.ToString(properties.Definition.DisplayName) & " was activated on: " & DateTime.Now.ToString()
                    listItem.Update()
    
                    ' Add a to-do task to the Task list.
                    Dim newTask As SPListItem = taskList.Items.Add()
                    newTask("Title") = "Deactivate feature: " & Convert.ToString(properties.Definition.DisplayName)
                    newTask.Update()
                End Using
            End Using
    
        Catch e As Exception
            Console.WriteLine("Error: " & e.ToString())
        End Try
    
    End Sub
    
    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. 用下面的代码替换 FeatureDeactivating 方法:

    Public Overrides Sub FeatureDeactivating(ByVal properties As SPFeatureReceiverProperties)
        Try
            Using site As New SPSite(siteUrl)
                Using web As SPWeb = site.OpenWeb(webUrl)
                    ' Reference the lists
                    Dim taskList As SPList = web.Lists("Tasks")
                    Dim announcementsList As SPList = web.Lists("Announcements")
    
                    ' Add an announcement that the feature was deactivated.
                    Dim listItem As SPListItem = announcementsList.Items.Add()
                    listItem("Title") = "Deactivated Feature: " & Convert.ToString(properties.Definition.DisplayName)
                    listItem("Body") = Convert.ToString(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.
                    Dim qry As New SPQuery()
                    qry.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Deactive</Value></Contains></Where>"
                    Dim taskItems As SPListItemCollection = taskList.GetItems(qry)
    
                    For Each taskItem As SPListItem In taskItems
                        ' Mark the task as complete.
                        taskItem("PercentComplete") = 1
                        taskItem("Status") = "Completed"
                        taskItem.Update()
                    Next
                End Using
    
            End Using
    
        Catch e As Exception
            Console.WriteLine("Error: " & e.ToString())
        End Try
    
    
    End Sub
    
    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());
        }
    }
    

测试项目

现在已向功能接收器添加代码,请运行 SharePoint 应用程序以测试该代码是否可以正常工作。在本示例中,代码中包含一个小错误。发生该错误后,您将使用 IntelliTrace 跟踪问题。

测试项目

  1. 选择 F5 键运行项目。

    在部署之后,该功能会自动激活,从而导致其功能接收器添加公告和任务。

  2. 在 SharePoint 启动,在 快速启动 窗格中,选择 列表 链接,然后选择"公告"列表,稍后"任务"列表以查看其内容。

    在“公告”列表中,添加了名为**“Activated feature: IntelliTraceTest_Feature1”的新公告,并向“任务”列表中添加了名为“Deactivate feature: IntelliTraceTest_Feature1”**的新任务。该任务的状态为“未启动”。

  3. 通过打开 网站操作 菜单上,选择 站点设置,然后选择 管理网站功能 链接停用该功能在 网站操作下。在 IntelliTraceTest Feature1旁边的下 停用 按钮,然后在警告页面的 停用此功能 链接。

  4. 快速启动 窗格中,选择 任务 链接。

    任务的**“状态”值现在应为“完成”,其“完成百分比”**值应为“100%”。而这些值仍然保持为其默认值。代码中的错误阻止任务更新。

调试项目

使用 IntelliTrace 查找并修复代码中的问题。

调试项目

  1. 在 Visual Studio,在 FeatureDeactivating 方法,找到 SPQuery qry = new SPQuery(); 行 visual C# 或 Visual Basic Dim qry As New SPQuery() 行。选择 F9 键在该行中插入断点。

    因为问题是在停用功能时发生的,所以此行是开始调试的逻辑位置。

  2. 选择 F5 键再次运行程序,并通过重复步骤在“测试项目”激活然后再停用该功能本主题前面的。

    当在 FeatureDeactivating 中命中断点时,将显示 IntelliTrace 窗口并列出应用程序到目前为止已执行的所有步骤。

  3. IntelliTrace 窗格中,单击 实时事件 类别中,选择 局部变量 链接。

  4. 选择 F11 键单步执行代码。

    每次选择 F11 键,另一个“debugger:”行添加到调试历史记录的应用程序的 IntelliTrace。

  5. 选择 F11 键单步执行代码。

  6. 局部变量 窗格中,展开 查询 名称并注意 查询 值显示为红色。

    此差异指示用于查找任务的查询不正确地搜索“deactive”而不是“deactivate”。此错误意味着任务中的查询不会找到任务列表。

  7. 选择 F11 键单步执行代码。

  8. 局部变量 窗格中,展开 taskItems 名称,并注意 计数 值为零 (0)。

    此值指示 taskItems 的集合不包含任何 qry 项目。

  9. 选择 F5 键继续调试。

  10. 在过程完成后,选择" IntelliTrace 窗口的 全部中断 链接。

    您必须执行此步骤以保存 IntelliTrace 数据。

    您可以查看应用程序的调用信息。,在 Intellitrace 工具栏上,选择 打开 IntelliTrace 设置 链接,然后,在 选项 对话框中,选择 IntelliTrace 事件和调用信息 选项按钮。

  11. 在 IntelliTrace 工具栏上,选择 保存当前 IntelliTrace 会话 按钮保存调试数据。

    该文件将具有扩展名 .iTrace。

  12. 在 Visual Studio 菜单栏上,依次选择 调试停止调试

  13. 打开 .iTrace 文件。

    此步骤将打开 IntelliTrace 摘要页,提供调试信息 (如异常数据和线程列出用于程序。

  14. 外接 线程列表,选择 主线程,然后选择 启动调试 按钮。

    此步骤将使用 .iTrace 数据在 Visual Studio 中启动调试会话。若要查看某个事件的详细信息在 IntelliTrace 窗口中,选择事件。

  15. 选择在 调试程序: 步骤的 局部变量 链接 SPListItemCollection taskItems = taskList.GetItems(qry);的。

    将出现“局部变量”窗口。

  16. 在“局部变量”窗口中,展开变量列表,找到 qry.Query,然后验证用于查找任务的查询是否在错误地搜索“Deactive”而不是“Deactivate”。

重新测试项目

现在已使用 IntelliTrace 确定了问题,请更改错误,然后重新测试项目。

重新测试项目

  1. 关闭 Visual Studio 的调试会话,然后重新打开 IntelliTraceTest 项目(如果尚未打开它)。

  2. 在代码的查询字符串 (qry.Query = "<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Deactive</Value></Contains></Where>") 中,将值从 Deactive 更改为 Deactivate。

  3. 选择 F5 键再次运行项目。

  4. 打开任务列表,然后验证 Deactivate 任务的**“状态”值现在是否已正确设置为“完成”,并验证其“完成百分比”**的值是否设置为 100%。

请参见

概念

验证和调试 SharePoint 代码

使用 IntelliTrace 查看代码的历史记录来更快地调试代码

其他资源

演练:使用单元测试验证 SharePoint 代码