共用方式為


逐步解說:建立 SharePoint 專案的自訂部署步驟

當您部署 SharePoint 專案時,Visual Studio 2010 會依照特定順序執行一系列「部署步驟」(Deployment Step)。 Visual Studio 包含許多內建的部署步驟,但是您也可以建立自己的步驟。

在本逐步解說中,您將建立自訂的部署步驟,以便在 SharePoint 伺服器上升級方案。 Visual Studio 包含許多工作的內建部署步驟,例如撤銷或加入方案,但是其中不包括升級方案的部署步驟。 根據預設,當您部署 SharePoint 方案時,Visual Studio 會先撤銷方案 (如果已部署的話),然後重新部署整個方案。 如需內建部署步驟的詳細資訊,請參閱部署 SharePoint 方案套件

本逐步解說將示範下列工作:

  • 建立執行兩項主要工作的 Visual Studio 擴充功能:

    • 它會定義升級 SharePoint 方案的自訂部署步驟。

    • 它會建立專案擴充功能,用來定義「部署組態」(Deployment Configuration)。 部署組態是一組針對指定的專案執行的部署步驟。 新的部署組態包括自訂部署步驟和數個內建的部署步驟。

  • 建立兩個由擴充組件呼叫的自訂「SharePoint 命令」(SharePoint Command)。 SharePoint 命令是擴充組件所呼叫,以在 SharePoint 伺服器物件模型中使用 API 的方法。 如需詳細資訊,請參閱呼叫 SharePoint 物件模型

  • 建置 Visual Studio Extension (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. 在 [專案名稱] 方塊中,輸入 Upgrade Deployment Step for SharePoint Projects。

  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 命令的所有自訂擴充組件都需要 Sharepoint.Commands.v4 這個值。

  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] 索引標籤上,清除 [偵錯後自動撤銷] 核取方塊。

  16. 在 [方案總管] 中,展開 [ListDefinition1] 節點,然後展開 [ListInstance1] 子節點。

  17. 按兩下 Elements.xml 檔,該檔案為 [ListInstance1] 節點的子項目。 Elements.xml 檔隨即在編輯器中開啟。

  18. 使用下列 XML 取代此檔案中的預設 XML: 此 XML 會將清單名稱變更為 [員工]。 此外還會加入名為 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 網站就會開啟以顯示新的 [員工] 清單,而且清單中會包含 Jim Hance 這個項目。

  5. 關閉 Web 瀏覽器。

若要修改清單定義和清單執行個體並且重新部署

  1. 在 EmployeesListDefinition 專案中按兩下 Elements.xml 檔,該檔案為 [ListInstance1] 專案項目的子項目。

  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. 按兩下 Schema.xml 檔,該檔案為 [ListDefinition1] 專案項目的子項目。

  4. 在這個檔案中搜尋字串 BaseViewID="1"。 這個字串是將要在下一個步驟中修改之 View 項目的屬性。

  5. 在此 View 項目下方,尋找 ViewFields 子項目。

  6. 移除 ViewFields 項目下方 Name 屬性分別設為 Attachments 和 HomePhone 的兩個 FieldRef 子項目。 這個動作會移除 SharePoint 網站上,[員工] 清單的預設檢視中的這些欄位。 當您完成時,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 網站以顯示 [員工] 清單。

  9. 確認下列詳細資料:

    • [附件] 和 [住家電話] 欄不會出現在清單的此檢視中。

    • 現在清單是空的。 若您使用 [預設] 部署組態重新部署方案,[員工] 清單就會被專案中新的空清單取代。

測試部署步驟

您現在可以開始測試升級部署步驟。 首先將項目加入至 SharePoint 中的清單執行個體。 接下來,變更清單定義和清單執行個體,然後在 SharePoint 網站上將它們升級,以確認升級部署步驟並未覆寫新項目。

若要手動將項目加入至清單

  1. 在 SharePoint 網站的功能區中,按一下 [項目] 索引標籤。

  2. 在 [新增] 群組中按一下 [新項目]。

  3. 在 [姓氏] 欄位中輸入 Ruth。

  4. 在 [名字] 欄位中輸入 Andy。

  5. 在 [公司] 欄位中輸入 Contoso。

  6. 按一下 [儲存]。 確認新項目出現在清單中。 稍後您將在本逐步解說中,使用此項目確認升級部署步驟不會覆寫此清單的內容。

  7. 關閉 Web 瀏覽器。

若要測試升級部署步驟

  1. 在 Visual Studio 的實驗執行個體中,按一下 [方式總管] 內的 [EmployeesListDefinition] 專案節點。

  2. 在 [屬性] 視窗中,將 [現用部署組態] 屬性設定為 [升級]。 這是包含新的升級部署步驟的自訂部署組態。

  3. 按兩下 Schema.xml 檔,該檔案為 [ListDefinition1] 專案項目的子項目。

  4. 找到您之前修改的 ViewFields 項目。

  5. 移除 ViewFields 項目下方 Name 屬性分別設為 WorkPhone 和 Email 的兩個 FieldRef 子項目。 這個動作會移除 SharePoint 網站上,[員工] 清單的預設檢視中的這些欄位。 當您完成時,ViewFields 項目應包含下列內容。

    <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="FirstName" />
      <FieldRef Name="Company" />
    </ViewFields>
    
  6. 請按 F5。 確認另一個 Visual Studio 執行個體中的程式碼在您之前於 CanExecute 方法中設定的中斷點停止。

  7. 再按一次 F5。 確認程式碼在您之前於 Execute 方法中設定的中斷點停止。

  8. 再按 F5 最後一次。 SharePoint 網站隨即開啟為 [員工] 清單。

  9. 在 [快速啟動] 區中,按一下 [員工] 清單。

  10. 確認下列詳細資料:

    • 您之前手動加入的[Andy Ruth] 項目仍然會在清單中。

    • [公司電話] 和 [電子郵件地址] 欄不會在清單的此檢視中顯示。

    [升級] 部署組態會修改 SharePoint 網站上現有的 [員工] 清單執行個體。 如果您使用 [預設] 部署組態,而不是 [升級] 組態,則可能發生部署衝突。 Visual Studio 會取代 [員工] 清單來解決衝突,並且會刪除 [Andy Ruth] 項目。

清理開發電腦

在您完成測試升級部署步驟之後,從 SharePoint 網站移除清單執行個體和清單定義,並且從 Visual Studio 移除部署步驟擴充。

若要從 SharePoint 網站移除清單執行個體

  1. 開啟 SharePoint 網站上的 [員工] 清單 (如果尚未開啟的話)。

  2. 在 SharePoint 網站的功能區中,按一下 [清單] 索引標籤。

  3. 在 [清單] 索引標籤的 [設定] 群組中,按一下 [清單設定]。

  4. 在 [權限和管理] 下方,按一下 [刪除此清單]。 按一下 [確定],確認您要將清單傳送至資源回收筒。

  5. 關閉 Web 瀏覽器。

若要從 SharePoint 網站移除清單定義

  • 在 Visual Studio 的實驗執行個體中,選取 [建置] 功能表上的 [撤銷]。

    Visual Studio 會撤銷 SharePoint 網站上的清單定義。

若要解除安裝擴充功能

  1. 在 Visual Studio 的實驗執行個體中,按一下 [工具] 功能表上的 [擴充管理員]。

    [擴充管理員] 對話方塊隨即開啟。

  2. 在擴充功能清單中,按一下 [SharePoint 專案的部署步驟],然後按一下 [解除安裝]。

  3. 在所顯示的對話方塊中,按一下 [],確認您要解除安裝擴充功能。

  4. 按一下 [立即重新啟動] 完成解除安裝。

  5. 關閉 Visual Studio 的兩個執行個體 (Visual Studio 的實驗執行個體,以及其中有開啟 UpgradeDeploymentStep 專案的執行個體)。

請參閱

其他資源

擴充 SharePoint 封裝和部署