演练:为 SharePoint 项目创建自定义部署步骤

当您部署 SharePoint 项目时,Visual Studio 2010 将按特定顺序执行一系列部署步骤。 Visual Studio 包含许多内置部署步骤,但您也可以创建自己的部署步骤。

在本演练中,您将创建一个自定义部署步骤以升级 SharePoint Server 上的解决方案。 Visual Studio 包含针对许多任务的内置部署步骤(如收回或添加解决方案),但不包含用于升级解决方案的部署步骤。 默认情况下,当您部署一个 SharePoint 解决方案时,Visual Studio 会先收回该解决方案(如果已部署),然后重新部署整个解决方案。 有关内置部署步骤的更多信息,请参见部署 SharePoint 解决方案包

本演练将演示以下任务:

  • 创建一个执行以下两大任务的 Visual Studio 扩展:

    • 此扩展定义一个自定义部署步骤以升级 SharePoint 解决方案。

    • 此扩展创建一个定义新的部署配置的项目扩展。 部署配置是针对给定项目执行的一组部署步骤。 新的部署配置包含自定义部署步骤和若干内置部署步骤。

  • 创建由扩展程序集调用的两个自定义 SharePoint 命令。 SharePoint 命令是一些方法,扩展程序集可调用这些方法以使用 SharePoint 服务器对象模型中的 API。 有关更多信息,请参见调入 SharePoint 对象模型

  • 生成 Visual Studio 扩展 (VSIX) 包以部署这两个程序集。

  • 测试新的部署步骤。

系统必备

您需要在开发计算机上安装以下组件才能完成本演练:

了解以下概念很有用,但对于完成本演练并不是必需的:

创建项目

若要完成本演练,您需要创建以下三个项目:

  • 一个用于创建 VSIX 包以部署扩展的 VSIX 项目。

  • 一个用于实现扩展的类库项目。 此项目必须面向 .NET Framework 4。

  • 一个用于定义自定义 SharePoint 命令的类库项目。 此项目必须面向 .NET Framework 3.5。

从创建项目开始本演练。

创建 VSIX 项目

  1. 启动 Visual Studio。

  2. 在**“文件”菜单上指向“新建”,再单击“项目”**。

  3. 在**“新建项目”对话框中,展开“Visual C#”“Visual Basic”节点,然后单击“扩展性”**。

    提示

    只有在安装 Visual Studio 2010 SDK 之后,“扩展性”节点才可用。 有关更多信息,请参见前面的系统必备部分。

  4. 在对话框顶部的组合框中,选择**“.NET Framework 4”**。

  5. 单击**“VSIX 项目”**模板。

  6. 在**“名称”**框中键入 UpgradeDeploymentStep。

  7. 单击**“确定”**。

    Visual Studio 会将**“UpgradeDeploymentStep”项目添加到“解决方案资源管理器”**中。

创建扩展项目

  1. 在**“解决方案资源管理器”中,右击解决方案节点,单击“添加”,再单击“新建项目”**。

    提示

    在 Visual Basic 项目中,仅当在“选项”对话框 ->“项目和解决方案”->“常规”中选中“总是显示解决方案”复选框时,解决方案节点才会出现在“解决方案资源管理器”中。

  2. 在**“新建项目”对话框中,展开“Visual C#”“Visual Basic”节点,然后单击“Windows”**。

  3. 在对话框顶部的组合框中,选择**“.NET Framework 4”**。

  4. 选择**“类库”**项目模板。

  5. 在**“名称”**框中键入 DeploymentStepExtension。

  6. 单击**“确定”**。

    Visual Studio 将**“DeploymentStepExtension”**项目添加到解决方案中,并打开默认的 Class1 代码文件。

  7. 从项目中删除 Class1 代码文件。

创建 SharePoint 命令项目

  1. 在**“解决方案资源管理器”中,右击解决方案节点,指向“添加”,然后单击“新建项目”**。

    提示

    在 Visual Basic 项目中,仅当在“选项”对话框 ->“项目和解决方案”->“常规”中选中“总是显示解决方案”复选框时,解决方案节点才会出现在“解决方案资源管理器”中。

  2. 在**“添加新项目”对话框中,展开“Visual C#”“Visual Basic”,然后单击“Windows”**。

  3. 单击**“类库”**项目模板。

  4. 在对话框顶部的组合框中,选择**“.NET Framework 3.5”**。

  5. 在**“名称”**框中键入 SharePointCommands。

  6. 单击**“确定”**。

    Visual Studio 会将**“SharePointCommands”**项目添加到解决方案中并打开默认的 Class1 代码文件。

  7. 从项目中删除 Class1 代码文件。

配置项目

在编写代码以创建自定义部署步骤之前,您必须先添加代码文件和程序集引用并配置项目。

配置 DeploymentStepExtension 项目

  1. 在**“DeploymentStepExtension”**项目中,添加具有下列名称的两个代码文件:

    • UpgradeStep

    • DeploymentConfigurationExtension

  2. 在**“项目”菜单上,单击“添加引用”**。

  3. 在**“.NET”选项卡上,按住 Ctrl 的同时选择下列程序集,然后单击“确定”**:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

配置 SharePointCommands 项目

  1. 在**“SharePointCommands”**项目中添加名为 Commands 的代码文件。

  2. 在**“解决方案资源管理器”中,单击“SharePointCommands”**项目节点。

  3. 在**“项目”菜单上,单击“添加引用”**。

  4. 在**“.NET”选项卡上,按住 Ctrl 的同时选择下列程序集,然后单击“确定”**:

    • Microsoft.SharePoint

    • Microsoft.VisualStudio.SharePoint.Commands

定义自定义部署步骤

创建一个定义升级部署步骤的类。 若要定义部署步骤,此类应实现 IDeploymentStep 接口。 每当需要定义自定义部署步骤时,就要实现此接口。

定义自定义部署步骤

  1. 在**“DeploymentStepExtension”**项目中,双击 UpgradeStep 代码文件。

  2. 将下面的代码粘贴到此文件中。

    提示

    添加此代码后,项目将会出现一些编译错误。 在添加后面的步骤中的代码之后,这些错误将消失。

    Imports System
    Imports Microsoft.VisualStudio.SharePoint
    Imports Microsoft.VisualStudio.SharePoint.Deployment
    Imports System.ComponentModel.Composition
    
    Namespace Contoso.DeploymentSteps.Upgrade
    
        ' Export attribute: Enables Visual Studio to discover and load this deployment step.
        ' DeploymentStep attribute: Specifies the ID for this new deployment step.
        ' UpgradeStep class: Defines a new deployment step that can be used to upgrade a solution 
        '     on a SharePoint site.
        <Export(GetType(IDeploymentStep))> _
        <DeploymentStep("Contoso.DeploymentSteps.UpgradeSolution")> _
        Friend Class UpgradeStep
            Implements IDeploymentStep
    
            Private solutionName As String
            Private solutionFullPath As String
    
            Private Sub Initialize(ByVal stepInfo As IDeploymentStepInfo) _
                Implements IDeploymentStep.Initialize
                stepInfo.Name = "Upgrade solution"
                stepInfo.StatusBarMessage = "Upgrading solution..."
                stepInfo.Description = "Upgrades the solution on the local machine."
            End Sub
    
            ' Specifies whether the solution can be upgraded.
            Private Function CanExecute(ByVal context As IDeploymentContext) As Boolean _
                Implements IDeploymentStep.CanExecute
    
                ' SharePoint returns all the installed solutions names in lower case.
                solutionName = (context.Project.Package.Model.Name & ".wsp").ToLower()
                solutionFullPath = context.Project.Package.OutputPath
                Dim solutionExists As Boolean = _
                    context.Project.SharePointConnection.ExecuteCommand(Of String, Boolean)(
                    "Contoso.Commands.IsSolutionDeployed", solutionName)
    
                ' Throw exceptions in error cases because deployment cannot proceed.
                If context.Project.IsSandboxedSolution = True Then
                    Dim sandboxMessage As String = "Cannot upgrade the solution. Upgrade deployment " & _
                        "configuration does not support Sandboxed solutions."
                    context.Logger.WriteLine(sandboxMessage, LogCategory.Error)
                    Throw New InvalidOperationException()
                ElseIf solutionExists = False Then
                    Dim notDeployedMessage As String = String.Format("Cannot upgrade the solution. The IsSolutionDeployed " & _
                        "command cannot find the following solution on the SharePoint site: {0}.", solutionName)
                    context.Logger.WriteLine(notDeployedMessage, LogCategory.Error)
                    Throw New InvalidOperationException(notDeployedMessage)
                End If
    
                ' Execute step and continue with deployment.
                Return True
            End Function
    
            Private Sub Execute(ByVal context As IDeploymentContext) _
                Implements IDeploymentStep.Execute
                context.Logger.WriteLine("Upgrading solution: " & solutionName, LogCategory.Status)
                context.Project.SharePointConnection.ExecuteCommand("Contoso.Commands.UpgradeSolution", _
                    solutionFullPath)
            End Sub
    
        End Class
    End Namespace
    
    using System;
    using Microsoft.VisualStudio.SharePoint;
    using Microsoft.VisualStudio.SharePoint.Deployment;
    using System.ComponentModel.Composition;
    
    namespace Contoso.DeploymentSteps.Upgrade
    {
        // Enables Visual Studio to discover and load this deployment step.
        [Export(typeof(IDeploymentStep))]
    
        // Specifies the ID for this new deployment step.
        [DeploymentStep("Contoso.DeploymentSteps.UpgradeSolution")]
    
        // Defines a new deployment step that can be used to upgrade a solution on a SharePoint site.
        internal class UpgradeStep : IDeploymentStep
        {
            private string solutionName;
            private string solutionFullPath;
    
            // Implements IDeploymentStep.Initialize.
            public void Initialize(IDeploymentStepInfo stepInfo)
            {
                stepInfo.Name = "Upgrade solution";
                stepInfo.StatusBarMessage = "Upgrading solution...";
                stepInfo.Description = "Upgrades the solution on the local machine.";
            }
    
            // Implements IDeploymentStep.CanExecute. Specifies whether the solution can be upgraded.
            public bool CanExecute(IDeploymentContext context)
            {
                // SharePoint returns all the installed solutions names in lower case.
                solutionName = (context.Project.Package.Model.Name + ".wsp").ToLower();
                solutionFullPath = context.Project.Package.OutputPath;
                bool solutionExists = context.Project.SharePointConnection.ExecuteCommand<string, bool>(
                    "Contoso.Commands.IsSolutionDeployed", solutionName);
    
                // Throw exceptions in error cases because deployment cannot proceed.
                if (context.Project.IsSandboxedSolution)
                {
                    string sandboxMessage = "Cannot upgrade the solution. The upgrade deployment configuration " +
                        "does not support Sandboxed solutions.";
                    context.Logger.WriteLine(sandboxMessage, LogCategory.Error);
                    throw new InvalidOperationException(sandboxMessage);
                }
                else if (!solutionExists)
                {
                    string notDeployedMessage = string.Format("Cannot upgrade the solution. The IsSolutionDeployed " +
                        "command cannot find the following solution: {0}.", solutionName);
                    context.Logger.WriteLine(notDeployedMessage, LogCategory.Error);
                    throw new InvalidOperationException(notDeployedMessage);
                }
    
                // Execute step and continue with deployment.
                return true;
            }
    
            // Implements IDeploymentStep.Execute.
            public void Execute(IDeploymentContext context)
            {
                context.Logger.WriteLine("Upgrading solution: " + solutionName, LogCategory.Status);
                context.Project.SharePointConnection.ExecuteCommand("Contoso.Commands.UpgradeSolution",
                    solutionFullPath);
            }
        }
    }
    

创建包含自定义部署步骤的部署配置

为新部署配置创建项目扩展。 此部署配置包含若干内置部署步骤和新的升级部署步骤。 利用此部署配置,SharePoint 开发人员可以轻松地在 SharePoint 项目中使用升级部署步骤。

若要创建部署配置,此类应实现 ISharePointProjectExtension 接口。 每当需要创建 SharePoint 项目扩展时,就要实现此接口。

创建部署配置

  1. 在**“DeploymentStepExtension”**项目中,双击 DeploymentConfigurationExtension 代码文件。

  2. 将下面的代码粘贴到此文件中。

    Imports Microsoft.VisualStudio.SharePoint
    Imports Microsoft.VisualStudio.SharePoint.Deployment
    Imports System.ComponentModel.Composition
    
    Namespace Contoso.DeploymentSteps.Upgrade
    
        ' Export attribute: Enables Visual Studio to discover and load this project-level extension.
        ' DeploymentConfigurationExtension class: Defines a project-level extension. The extension creates 
        '     a new deployment configuration that includes the upgrade deployment step.
        <Export(GetType(ISharePointProjectExtension))> _
        Friend Class DeploymentConfigurationExtension
            Implements ISharePointProjectExtension
    
            Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
                Implements ISharePointProjectExtension.Initialize
                AddHandler projectService.ProjectInitialized, AddressOf ProjectInitialized
            End Sub
    
            ' Creates the new deployment configuration.
            Private Sub ProjectInitialized(ByVal Sender As Object, ByVal e As SharePointProjectEventArgs)
                Dim deploymentSteps As String() = New String() _
                {
                    DeploymentStepIds.PreDeploymentCommand, _
                    DeploymentStepIds.RecycleApplicationPool, _
                    "Contoso.DeploymentSteps.UpgradeSolution", _
                    DeploymentStepIds.PostDeploymentCommand _
                }
                Dim retractionSteps As String() = New String() _
                {
                    DeploymentStepIds.RecycleApplicationPool, _
                    DeploymentStepIds.RetractSolution
                }
                Dim configuration As IDeploymentConfiguration = e.Project.DeploymentConfigurations.Add( _
                    "Upgrade", deploymentSteps, retractionSteps)
                configuration.Description = "This is the upgrade deployment configuration"
            End Sub
    
        End Class
    End Namespace
    
    using Microsoft.VisualStudio.SharePoint;
    using Microsoft.VisualStudio.SharePoint.Deployment;
    using System.ComponentModel.Composition;
    
    namespace Contoso.DeploymentSteps.Upgrade
    {
        // Enables Visual Studio to discover and load this project-level extension.
        [Export(typeof(ISharePointProjectExtension))]
    
        // Defines a project-level extension. The extension creates a new deployment configuration that includes the
        // upgrade deployment step.
        internal class DeploymentConfigurationExtension : ISharePointProjectExtension
        {
            // Implements ISharePointProjectExtension.Initialize.
            public void Initialize(ISharePointProjectService projectService)
            {
                projectService.ProjectInitialized += ProjectInitialized;
            }
    
            // Creates the new deployment configuration.
            private void ProjectInitialized(object sender, SharePointProjectEventArgs e)
            {
                string[] deploymentSteps = new string[] 
                {
                    DeploymentStepIds.PreDeploymentCommand,
                    DeploymentStepIds.RecycleApplicationPool,
                    "Contoso.DeploymentSteps.UpgradeSolution",
                    DeploymentStepIds.PostDeploymentCommand 
                };
    
                string[] retractionSteps = new string[] 
                {
                    DeploymentStepIds.RecycleApplicationPool,
                    DeploymentStepIds.RetractSolution                
                };
    
                IDeploymentConfiguration configuration = e.Project.DeploymentConfigurations.Add(
                    "Upgrade", deploymentSteps, retractionSteps);
                configuration.Description = "This is the upgrade deployment configuration";
            }
        }
    }
    

创建自定义 SharePoint 命令

创建可调入 SharePoint 服务器对象模型的两个自定义命令。 一个命令用于确定是否已部署解决方案;另一个命令用于升级解决方案。

定义 SharePoint 命令

  1. 在**“SharePointCommands”**项目中,双击 Commands 代码文件。

  2. 将下面的代码粘贴到此文件中。

    Imports System
    Imports System.IO
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Administration
    Imports Microsoft.VisualStudio.SharePoint.Commands
    
    Namespace Contoso.DeploymentSteps.Upgrade
    
        Friend Class Commands
    
            ' Determines whether the specified solution has been deployed to the local SharePoint server.
            <SharePointCommand("Contoso.Commands.IsSolutionDeployed")> _
            Private Function IsSolutionDeployed(ByVal context As ISharePointCommandContext, ByVal solutionName As String) As Boolean
                Dim solution As SPSolution = SPFarm.Local.Solutions(solutionName)
                Return solution IsNot Nothing
            End Function
    
            ' Upgrades the specified solution to the local SharePoint server.
            <SharePointCommand("Contoso.Commands.UpgradeSolution")> _
            Private Sub UpgradeSolution(ByVal context As ISharePointCommandContext, ByVal fullWspPath As String)
                Dim solution As SPSolution = SPFarm.Local.Solutions(Path.GetFileName(fullWspPath))
                If solution Is Nothing Then
                    Throw New InvalidOperationException("The solution has not been deployed.")
                End If
                solution.Upgrade(fullWspPath)
            End Sub
    
        End Class
    End Namespace
    
    using System;
    using System.IO;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.VisualStudio.SharePoint.Commands;
    
    namespace Contoso.DeploymentSteps.Upgrade
    {
        internal class Commands
        {
            // Determines whether the specified solution has been deployed to the local SharePoint server.
            [SharePointCommand("Contoso.Commands.IsSolutionDeployed")]
            private bool IsSolutionDeployed(ISharePointCommandContext context, string solutionName)
            {
                SPSolution solution = SPFarm.Local.Solutions[solutionName];
                return solution != null;
            }
    
            // Upgrades the specified solution to the local SharePoint server.
            [SharePointCommand("Contoso.Commands.UpgradeSolution")]
            private void UpgradeSolution(ISharePointCommandContext context, string fullWspPath)
            {
                SPSolution solution = SPFarm.Local.Solutions[Path.GetFileName(fullWspPath)];
    
                if (solution == null)
                {
                    throw new InvalidOperationException("The solution has not been deployed.");
                }
    
                solution.Upgrade(fullWspPath);
            }
        }
    }
    

检查点

演练进行到此时,自定义部署步骤和 SharePoint 命令的所有代码都位于项目中。 生成解决方案以确保项目在编译时不会出错。

生成解决方案

  • 在**“生成”菜单上,选择“生成解决方案”**。

创建 VSIX 包以部署扩展

若要部署扩展,请使用解决方案中的 VSIX 项目来创建 VSIX 包。 首先,通过修改 VSIX 项目中包含的 source.extension.vsixmanifest 文件来配置 VSIX 包。 然后,通过生成解决方案来创建 VSIX 包。

配置并创建 VSIX 包

  1. 在**“解决方案资源管理器”中,在“UpgradeDeploymentStep”项目下双击“source.extension.vsixmanifest”**文件。

    Visual Studio 将在清单编辑器中打开该文件。 source.extension.vsixmanifest 文件是所有 VSIX 包必需的 extension.vsixmanifest 文件的基础。 有关此文件的更多信息,请参见VSIX Extension Schema Reference

  2. 在**“产品名称”**框中,键入“SharePoint 项目的升级部署步骤”。

  3. 在**“作者”**框中键入 Contoso。

  4. 在**“说明”**框中,键入“提供可在 SharePoint 项目中使用的自定义升级部署步骤”。

  5. 在编辑器的**“内容”部分中,单击“添加内容”**按钮。

  6. 在**“添加内容”对话框的“选择内容类型”列表框中,选择“MEF 组件”**。

    提示

    此值对应于 extension.vsixmanifest 文件中的 MefComponent 元素。 此元素指定 VSIX 包中的扩展程序集的名称。 有关更多信息,请参见 MEFComponent Element (VSX Schema)

  7. 在**“选择源”下,单击“项目”单选按钮,并在其旁边的列表框中选择“DeploymentStepExtension”**。

  8. 单击**“确定”**。

  9. 在清单编辑器中,再次单击**“添加内容”**按钮。

  10. 在**“添加内容”对话框的“选择内容类型”列表框中,选择“自定义扩展类型”**。

    提示

    此值对应于 extension.vsixmanifest 文件中的 CustomExtension 元素。 此元素指定要包括在 Visual Studio 扩展中的自定义扩展。 有关更多信息,请参见 CustomExtension Element (VSX Schema)

  11. 在**“类型”**文本框中,键入 SharePoint.Commands.v4。

    提示

    此值对应于 extension.vsixmanifest 文件中的 CustomExtension 元素的 Type 特性。 值 Sharepoint.Commands.v4 是包含自定义 SharePoint 命令的所有自定义扩展程序集所必需的。

  12. 在**“选择源”下,单击“项目”单选按钮,并在其旁边的列表框中选择“SharePointCommands”**。

  13. 单击**“确定”**。

  14. 在**“生成”菜单上,单击“生成解决方案”**。 确保编译解决方案时不会出错。

  15. 打开 UpgradeDeploymentStep 项目的生成输出文件夹。 确保此文件夹此时包含 UpgradeDeploymentStep.vsix 文件。

    默认情况下,生成输出文件夹为 包含项目文件的文件夹下的 ..\bin\Debug 文件夹。

准备测试升级部署步骤

若要测试升级部署步骤,您必须先将示例解决方案部署到 SharePoint 网站中。 首先在 Visual Studio 的实验实例中调试扩展。 然后,创建要用于测试部署步骤的列表定义和列表实例,再将它们部署到 SharePoint 网站中。 紧接着,将修改列表定义和列表实例并对其进行重新部署,以查看默认部署过程如何覆盖 SharePoint 网站上的解决方案。

在本演练后面的部分中,您将修改列表定义和列表实例,然后在 SharePoint 网站上对它们进行升级。

开始调试扩展

  1. 利用管理员特权重新启动 Visual Studio,并打开 UpgradeDeploymentStep 解决方案。

  2. 在 DeploymentStepExtension 项目中,打开 UpgradeStep 代码文件,并向 CanExecute 和 Execute 方法的第一个代码行中添加一个断点。

  3. F5 开始调试。

  4. Visual Studio 将扩展安装到 %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Upgrade Deployment Step for SharePoint Projects\1.0 中,并启动 Visual Studio 的实验实例。 您将在此 Visual Studio 实例中测试升级部署步骤。

创建列表定义和列表实例

  1. 在 Visual Studio 的实验实例中,在**“文件”菜单上指向“新建”,然后单击“项目”**。

  2. 在**“新建项目”对话框中,展开“Visual C#”,再展开“SharePoint”,然后单击“2010”**。

  3. 在对话框顶部的组合框中,确保选定**“.NET Framework 3.5”**。 针对 Microsoft SharePoint Foundation 2010 和 Microsoft SharePoint Server 2010 的项目需要此版本的 .NET Framework。

  4. 在项目模板的列表中,单击**“列表定义”**。

  5. 在**“名称”**框中键入 EmployeesListDefinition。

  6. 单击**“确定”**。

  7. 在**“SharePoint 自定义向导”**中,键入要用于调试的网站的 URL。

  8. 在**“此 SharePoint 解决方案的信任级别是什么?”下,单击“部署为场解决方案”**。

    提示

    升级部署步骤不支持沙盒解决方案。

  9. 单击**“下一步”**。

  10. 在**“选择列表定义设置”页中的“列表定义的显示名称是什么?”**下,键入“雇员列表”。

  11. 在**“列表定义的类型是什么?”下选择“联系人”**。

  12. 确保选中**“为此列表定义添加列表实例”**复选框。

  13. 单击**“完成”**。

    Visual Studio 将创建项目,并在编辑器中打开列表定义的 Elements.xml 文件。

  14. 在**“解决方案资源管理器”中,右击“EmployeesListDefinition”项目节点,再单击“属性”**。

  15. 在项目属性的**“SharePoint”选项卡上,清除“Auto-retract after debugging”(调试后自动收回)**复选框。

  16. 在**“解决方案资源管理器”中,展开“ListDefinition1”节点,再展开“ListInstance1”**子节点。

  17. 双击作为**“ListInstance1”**节点的子级的 Elements.xml 文件。 将在编辑器中打开此 Elements.xml 文件。

  18. 将此文件中的默认 XML 替换为以下 XML。 此 XML 会将列表的名称更改为**“Employees”(雇员)**。 它还会为名为 Jim Hance 的员工添加一个项。

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
      <ListInstance Title="Employees"
                    OnQuickLaunch="TRUE"
                    TemplateType="10000"
                    Url="Lists/Employees"
                    Description="Simple list to test upgrade deployment step">
        <Data>
          <Rows>
            <Row>
              <Field Name="Title">Hance</Field>
              <Field Name="FirstName">Jim</Field>
              <Field Name="Company">Contoso</Field>
            </Row>
          </Rows>
        </Data>
      </ListInstance>
    </Elements>
    

部署列表定义和列表实例

  1. 在**“解决方案资源管理器”中,单击“EmployeesListDefinition”**项目节点。

  2. 在**“属性”窗口中,确保将“活动部署配置”属性设置为“默认”**。

  3. 按 F5。

  4. 验证项目是否成功生成、SharePoint 网站是否已打开并显示新的**“Employees”(雇员)**列表以及该列表是否包含与 Jim Hance 对应的项。

  5. 关闭 Web 浏览器。

修改列表定义和列表实例并对其进行重新部署

  1. 在 EmployeesListDefinition 项目中,双击作为**“ListInstance1”**项目项的子级的 Elements.xml 文件。

  2. 移除 Data 元素及其子级,以便从列表中移除与 Jim Hance 对应的项。 完成此操作后,该文件应包含以下 XML。

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
      <ListInstance Title="Employees"
                    OnQuickLaunch="TRUE"
                    TemplateType="10000"
                    Url="Lists/Employees"
                    Description="Simple list to test upgrade deployment step">
      </ListInstance>
    </Elements>
    
  3. 双击作为**“ListDefinition1”**项目项的子级的 Schema.xml 文件。

  4. 在此文件中搜索字符串 BaseViewID="1"。 该字符串是 View 元素的一个特性,您将在后续步骤中对其进行修改。

  5. 在此 View 元素下,找到子 ViewFields 元素。

  6. 在 ViewFields 元素下,移除其 Name 特性分别设置为 Attachments 和 HomePhone 的两个子 FieldRef 元素。 此操作将从 SharePoint 网站上的**“Employees”(雇员)**列表的默认视图中移除这些字段。 完成此操作后,ViewFields 元素应具有以下内容。

    <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="FirstName" />
      <FieldRef Name="Company" />
      <FieldRef Name="WorkPhone" />
      <FieldRef Name="Email" />
    </ViewFields>
    
  7. 按 F5。 验证**“部署冲突”**对话框是否出现。 当 Visual Studio 尝试将一个解决方案(列表实例)部署到已部署该解决方案的 SharePoint 网站时,将出现此对话框。

    在本演练后面的部分中,您会发现在执行升级部署步骤时,此对话框不会出现。

  8. 单击**“自动解决”。 Visual Studio 将删除 SharePoint 网站上的列表实例,在项目中部署列表项,然后打开 SharePoint 网站并显示“Employees”(雇员)**列表。

  9. 验证以下详细信息:

    • **“Attachments”(附件)“Home Phone”(家庭电话)**列未显示在此列表视图中。

    • 此列表此时为空。 之前使用**“默认”部署配置重新部署解决方案时,“Employees”(雇员)**列表已替换为项目中的新的空列表。

测试部署步骤

现在您可以对升级部署步骤进行测试了。 首先,向 SharePoint 中的列表实例添加一个项。 然后,更改升级列表定义和列表实例,并在 SharePoint 网站上对其进行升级以确认升级部署步骤不会覆盖新项。

手动向列表添加项

  1. 在 SharePoint 网站上的功能区中,单击**“项”**选项卡。

  2. 在**“新建”组中单击“新建项”**。

  3. 在**“Last Name”(姓氏)**字段中键入 Ruth。

  4. 在**“First Name”(名字)**字段中键入 Andy。

  5. 在**“Company”(公司)**字段中键入 Contoso。

  6. 单击**“保存”**。 验证新项是否会在列表中出现。 在本演练后面的部分中,您将使用此项来确认升级部署步骤不会覆盖此列表的内容。

  7. 关闭 Web 浏览器。

测试升级部署步骤

  1. 在 Visual Studio 的实验实例中,在**“解决方案资源管理器”中单击“EmployeesListDefinition”**项目节点。

  2. 在**“属性”窗口中,将“活动部署配置”属性设置为“升级”**。 这是包含新的升级部署步骤的自定义部署配置。

  3. 双击作为**“ListDefinition1”**项目项的子级的 Schema.xml 文件。

  4. 找到之前已修改的 ViewFields 元素。

  5. 在 ViewFields 元素下,移除其 Name 特性分别设置为 WorkPhone 和 Email 的两个子 FieldRef 元素。 此操作将从 SharePoint 网站上的**“Employees”(雇员)**列表的默认视图中移除这些字段。 完成此操作后,ViewFields 元素应具有以下内容。

    <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="FirstName" />
      <FieldRef Name="Company" />
    </ViewFields>
    
  6. 按 F5。 验证另一个 Visual Studio 实例中的代码是否会在您之前在 CanExecute 方法中设置的断点处停止。

  7. 再次按 F5。 验证代码是否会在您之前在 Execute 方法中设置的断点处停止。

  8. 最后一次按 F5。 SharePoint 网站将打开并显示**“Employees”(雇员)**列表。

  9. 在快速启动区域内,单击**“Employees”(雇员)**列表。

  10. 验证以下详细信息:

    • 您先前手动添加的**“Andy Ruth”**项仍位于列表中。

    • **“Business Phone”(工作电话)“E-mail Address”(电子邮件地址)**列未显示在此列表视图中。

    **“升级”部署配置将修改 SharePoint 网站上的现有“Employees”(雇员)列表实例。 如果使用的是“默认”部署配置,而不是“升级”配置,则会遇到部署冲突。 Visual Studio 将通过替换“Employees”(雇员)列表来解决此冲突,并将删除“Andy Ruth”**项。

清理开发计算机

测试完升级部署步骤之后,请从 SharePoint 网站中移除列表实例和列表定义,并从 Visual Studio 中移除部署步骤扩展。

从 SharePoint 网站中移除列表实例

  1. 在 SharePoint 网站上打开**“Employees”(雇员)**列表(如果此列表尚未打开)。

  2. 在 SharePoint 网站上的功能区中,单击**“列表”**选项卡。

  3. 在**“列表”选项卡上的“设置”组中,单击“列表设置”**。

  4. 在**“权限和管理”下,单击“删除此列表”。 单击“确定”**以确认要将此列表发送到回收站。

  5. 关闭 Web 浏览器。

从 SharePoint 网站中移除列表定义

  • 在 Visual Studio 的实验实例中,在**“生成”菜单上选择“收回”**。

    Visual Studio 将从 SharePoint 网站收回列表定义。

卸载扩展

  1. 在 Visual Studio 的实验实例中,在**“工具”菜单上单击“扩展管理器”**。

    这将打开**“扩展管理器”**对话框。

  2. 在扩展列表中,单击**“SharePoint 项目的部署步骤”,然后单击“卸载”**。

  3. 在出现的对话框中,单击**“是”**以确认您要卸载该扩展。

  4. 单击**“立即重新启动”**以完成卸载。

  5. 关闭 Visual Studio 的两个实例(Visual Studio 的实验实例和 Visual Studio 的已打开 UpgradeDeploymentStep 解决方案的实例)。

请参见

其他资源

扩展 SharePoint 打包和部署