逐步解說:使用項目範本建立自訂動作專案項目 (第 1 部分)

您可以建立自己的專案項目類型擴充 Visual Studio 中的 SharePoint 專案系統。在本逐步解說中,您將建立可加入至 SharePoint 專案的專案項目,以便在 SharePoint 網站上建立自訂動作。自訂動作會將功能表項目加入至 SharePoint 網站的 [網站動作] 功能表。

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

  • 建立為自訂動作定義新的 SharePoint 專案項目類型的 Visual Studio 擴充功能。新專案項目類型會實作幾個自訂功能:

    • 捷徑功能表可做為專案項目其他相關工作的起點,例如在 Visual Studio 中顯示自訂動作的設計工具。

    • 開發人員變更專案項目以及包含該專案項目之專案的特定屬性時執行的程式碼。

    • 出現在 [方案總管] 中專案項目旁邊的自訂圖示。

  • 建立專案項目的 Visual Studio 項目範本。

  • 建置 Visual Studio Extension (VSIX) 套件以部署專案項目範本和擴充組件。

  • 偵錯和測試專案項目。

這是獨立的逐步解說。完成本逐步解說之後,您可以將精靈加入至項目範本來增強專案項目。如需詳細資訊,請參閱逐步解說:使用項目範本建立自訂動作專案項目 (第 2 部分)

注意事項注意事項

您可以從下列位置取得本逐步解說所含之完成的專案、程式碼和其他檔案:https://go.microsoft.com/fwlink/?LinkId=191369 (英文)。

必要條件

開發電腦上需要下列元件才能完成此逐步解說:

了解下列概念有助於完成此逐步解說 (但非必要):

建立專案

若要完成這個逐步解說,您必須建立三個專案:

  • VSIX 專案。這個專案會建立 VSIX 套件,以部署 SharePoint 專案項目。

  • 項目範本專案。這個專案會建立可用以將 SharePoint 專案項目加入至 SharePoint 專案的項目範本。

  • 類別庫專案。這個專案會實作 Visual Studio 擴充功能,以定義 SharePoint 專案項目的行為。

從建立這些專案開始進行此逐步解說。

若要建立 VSIX 專案

  1. 啟動 Visual Studio。

  2. 在功能表列上,選擇 [檔案], [新增], [Project]。

  3. 在[新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 被選取。

  4. 在 [新增專案] 對話方塊中,展開 [Visual C#] 或 [Visual Basic] 節點,然後選取 [擴充性] 節點。

    注意事項注意事項

    為,當您安裝 Visual Studio SDK, [擴充性] 有節點的。如需詳細資訊,請參閱本主題稍早討論的<必要條件>一節。

  5. 選取 [VSIX 專案] 範本。

  6. 在 [Name]方塊中,輸入 CustomActionProjectItem專案,然後選取 [確定] 按鈕。

    Visual Studio 會將 CustomActionProjectItem 專案加入至 [方案總管]。

若要建立項目範本專案

  1. 在 [方案總管],開啟方案節點的捷徑功能表,選擇[新增],然後選取 [新增專案]。

    注意事項注意事項

    在 Visual Basic 專案中,方案節點只有在已選取General, Projects and Solutions, Options Dialog Box中的 [永遠顯示方案] 核取方塊時,才會出現在 [方案總管] 中。

  2. 在[新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 被選取。

  3. 在 [新增專案] 對話方塊中,展開 [Visual C#] 或 [Visual Basic] 節點,然後選取 [擴充性] 節點。

  4. 在專案範本清單中,選取 [C# 項目範本] 或 [Visual Basic 項目範本]範本。

  5. 在 [Name]方塊中,輸入 ItemTemplate,然後選取 [確定] 按鈕。

    Visual Studio 會將 ItemTemplate 專案加入至方案。

若要建立擴充功能專案

  1. 在 [方案總管],開啟方案節點的捷徑功能表,選擇[新增],然後選取 [新增專案]。

  2. 在[新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 被選取。

  3. 在 [新增專案] 對話方塊中,展開 [Visual C#] 或 [Visual Basic] 節點,然後選取 [視窗] 節點,然後選取 [類別庫] 專案範本。

  4. 在 [Name]方塊中,輸入 ProjectItemDefinition,然後選取 [確定] 按鈕。

    Visual Studio 隨即將 [ProjectItemDefinition] 專案加入至方案,然後開啟預設的 Class1 程式碼檔。

  5. 從專案刪除 Class1 程式碼檔。

設定擴充功能專案

在您撰寫程式碼定義 SharePoint 專案項目類型之前,必須先將程式碼檔案和組件參考加入至擴充功能專案。

若要設定專案

  1. 在 [方案總管],開啟 [ProjectItemDefinition]專案的捷徑功能表,選擇[新增],然後選取 [新項目]。

  2. 在專案項目清單中,選取 [程式碼檔]。

  3. 在 [Name]方塊中,輸入適當副檔名的名稱 CustomAction ,然後選取 [新增] 按鈕。

  4. 在 [方案總管],開啟 [ProjectItemDefinition]專案的捷徑功能表,然後選取 [新增參考]。

  5. 參考管理員– ProjectItemDefinition 對話方塊中,選取 [組件] 節點,然後選取 [Framework] 節點。

  6. 在下列組件都旁邊的核取方塊:

    • System.ComponentModel.Composition

    • System.Windows.Forms

  7. 選取 [延伸] 節點,在 Microsoft.VisualStudio.Sharepoint 組件旁邊的核取方塊,然後選取 [確定] 按鈕。

定義新的 SharePoint 專案項目類型

建立實作 ISharePointProjectItemTypeProvider 介面的類別,以定義新專案項目類型的行為。在您要定義新的專案項目類型時實作此介面。

若要定義新的 SharePoint 專案項目類型

  1. 在 ProjectItemDefinition 專案中,開啟 CustomAction 程式碼檔案。

  2. 使用下列程式碼取代此檔案中的程式碼。

    Imports System
    Imports System.Diagnostics
    Imports System.ComponentModel
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio.SharePoint
    
    Namespace Contoso.SharePointProjectItems.CustomAction
    
        ' Export attribute: Enables Visual Studio to discover and load this extension.
        ' SharePointProjectItemType attribute: Specifies the ID for this new project item type. This string must 
        '     match the value of the Type attribute of the ProjectItem element in the .spdata file for 
        '     the project item.
        ' SharePointProjectItemIcon attribute: Specifies the icon to display with this project item in Solution Explorer.
        ' CustomActionProjectItemTypeProvider class: Defines a new type of project item that can be used to create a custom 
        '     action on a SharePoint site.
        <Export(GetType(ISharePointProjectItemTypeProvider))> _
        <SharePointProjectItemType("Contoso.CustomAction")> _
        <SharePointProjectItemIcon("ProjectItemDefinition.CustomAction_SolutionExplorer.ico")> _
        Partial Friend Class CustomActionProjectItemTypeProvider
            Implements ISharePointProjectItemTypeProvider
    
            Private WithEvents projectItemTypeDefinition As ISharePointProjectItemTypeDefinition
    
            ' Configures the behavior of the project item type.
            Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
                Implements ISharePointProjectItemTypeProvider.InitializeType
    
                projectItemTypeDefinition.Name = "CustomAction"
                projectItemTypeDefinition.SupportedDeploymentScopes = _
                    SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
                projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All
                Me.projectItemTypeDefinition = projectItemTypeDefinition
            End Sub
    
            Private Const DesignerMenuItemText As String = "View Custom Action Designer"
    
            Private Sub ProjectItemMenuItemsRequested(ByVal Sender As Object, _
                ByVal e As SharePointProjectItemMenuItemsRequestedEventArgs) _
                Handles projectItemTypeDefinition.ProjectItemMenuItemsRequested
    
                Dim viewDesignerMenuItem As IMenuItem = e.ViewMenuItems.Add(DesignerMenuItemText)
                AddHandler viewDesignerMenuItem.Click, AddressOf MenuItemClick
            End Sub
    
            Private Sub MenuItemClick(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
                Dim projectItem As ISharePointProjectItem = CType(e.Owner, ISharePointProjectItem)
                Dim message As String = String.Format("You clicked the menu on the {0} item. " & _
                    "You could perform some related task here, such as displaying a designer " & _
                    "for the custom action.", projectItem.Name)
                System.Windows.Forms.MessageBox.Show(message, "Contoso Custom Action")
            End Sub
    
            Private Sub ProjectItemNameChanged(ByVal Sender As Object, ByVal e As NameChangedEventArgs) _
                Handles projectItemTypeDefinition.ProjectItemNameChanged
                Dim projectItem As ISharePointProjectItem = CType(Sender, ISharePointProjectItem)
                Dim message As String = String.Format("The name of the {0} item changed to: {1}", _
                    e.OldName, projectItem.Name)
                projectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
            End Sub
        End Class
    End Namespace
    
    using System;
    using System.Diagnostics;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.SharePoint;
    
    namespace Contoso.SharePointProjectItems.CustomAction
    {
        // Enables Visual Studio to discover and load this extension.
        [Export(typeof(ISharePointProjectItemTypeProvider))]
    
        // Specifies the ID for this new project item type. This string must match the value of the 
        // Type attribute of the ProjectItem element in the .spdata file for the project item.
        [SharePointProjectItemType("Contoso.CustomAction")]
    
        // Specifies the icon to display with this project item in Solution Explorer.
        [SharePointProjectItemIcon("ProjectItemDefinition.CustomAction_SolutionExplorer.ico")]
    
        // Defines a new type of project item that can be used to create a custom action on a SharePoint site.
        internal partial class CustomActionProjectItemTypeProvider : ISharePointProjectItemTypeProvider
        {
            // Implements IProjectItemTypeProvider.InitializeType. Configures the behavior of the project item type.
            public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
            {
                projectItemTypeDefinition.Name = "CustomAction";
                projectItemTypeDefinition.SupportedDeploymentScopes =
                    SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
                projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;
    
                projectItemTypeDefinition.ProjectItemNameChanged += ProjectItemNameChanged;
                projectItemTypeDefinition.ProjectItemMenuItemsRequested += ProjectItemMenuItemsRequested;
            }
    
            private const string designerMenuItemText = "View Custom Action Designer";
    
            private void ProjectItemMenuItemsRequested(object sender, SharePointProjectItemMenuItemsRequestedEventArgs e)
            {
                e.ViewMenuItems.Add(designerMenuItemText).Click += MenuItemClick;
            }
    
            private void MenuItemClick(object sender, MenuItemEventArgs e)
            {
                ISharePointProjectItem projectItem = (ISharePointProjectItem)e.Owner;
                string message = String.Format("You clicked the menu on the {0} item. " +
                    "You could perform some related task here, such as displaying a designer " +
                    "for the custom action.", projectItem.Name);
                System.Windows.Forms.MessageBox.Show(message, "Contoso Custom Action");
            }
    
            private void ProjectItemNameChanged(object sender, NameChangedEventArgs e)
            {
                ISharePointProjectItem projectItem = (ISharePointProjectItem)sender;
                string message = String.Format("The name of the {0} item changed to: {1}",
                    e.OldName, projectItem.Name);
                projectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
            }
        }
    }
    

在方案總管中建立專案項目的圖示

當您建立自訂 SharePoint 專案項目時,可以將影像 (圖示或點陣圖) 與專案項目產生關聯。這個影像會出現在 [方案總管] 中該專案項目的旁邊。

在下列程序中,您會建立專案項目的圖示,並且將圖示嵌入擴充組件。您之前建立之 CustomActionProjectItemTypeProvider 類別的 SharePointProjectItemIconAttribute 會參考此圖示。

若要建立專案項目的自訂圖示

  1. 在 [方案總管],開啟 [ProjectItemDefinition]專案的捷徑功能表,選擇,然後選取 [新增][新項目…]。

  2. 在專案項目清單中,選取 [圖示檔] 項目。

    注意事項注意事項

    在 Visual Basic 專案,您必須選取 [一般] 節點顯示 [圖示檔] 項目。

  3. 在 [Name]方塊中,輸入 CustomAction_SolutionExplorer.ico,然後選取 [新增] 按鈕。

    新圖示隨即開啟於 [影像編輯器] 中。

  4. 編輯 16x16 版的圖示檔,以便能夠辨識其設計,然後儲存圖示檔。

  5. 在 [方案總管],選取 [CustomAction_SolutionExplorer.ico]。

  6. 在 [屬性] 視窗,請在 [建置動作] 屬性選取旁邊的箭號。

  7. 在出現的清單中,選取 [內嵌資源]。

檢查點

在逐步解說中進行至此處時,專案項目的所有程式碼都會位於專案中。建立驗證專案在編譯時未發生任何錯誤。

若要建置您的專案

  • 開啟 [ProjectItemDefinition]專案的捷徑功能表並選擇 [組建]。

建立 Visual Studio 項目範本

若要讓其他開發人員使用您的專案項目,您必須建立專案範本或項目範本。開發人員會在 Visual Studio 中透過建立新專案或在現有專案中加入項目的方式,使用這些範本建立專案項目的執行個體。在本逐步解說中,請使用 ItemTemplate 專案來設定您的專案項目。

若要建立項目範本

  1. 從 ItemTemplate 專案刪除 Class1 程式碼檔案。

  2. 在 ItemTemplate 專案中,開啟 ItemTemplate.vstemplate 檔案。

  3. 以下列 XML 取代檔案的內容,然後儲存並關閉檔案。

    注意事項注意事項

    下列 XML 適用於 Visual C# 項目範本。如果您要建立 Visual Basic 項目範本,請將 ProjectType 項目的值取代為 VisualBasic。

    <?xml version="1.0" encoding="utf-8"?>
    <VSTemplate Version="2.0.0" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
      <TemplateData>
        <DefaultName>CustomAction</DefaultName>
        <Name>Custom Action</Name>
        <Description>SharePoint Custom Action by Contoso</Description>
        <ProjectType>CSharp</ProjectType>
        <SortOrder>1000</SortOrder>
        <Icon>ItemTemplate.ico</Icon>
        <ProvideDefaultName>true</ProvideDefaultName>
      </TemplateData>
      <TemplateContent>
        <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$\Elements.xml">Elements.xml</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$\SharePointProjectItem.spdata">CustomAction.spdata</ProjectItem>
      </TemplateContent>
    </VSTemplate>
    

    此檔案會定義項目範本的內容和行為。如需此檔案內容的詳細資訊,請參閱 Visual Studio 範本結構描述參考

  4. 在 [方案總管],開啟 [ItemTemplate]專案的捷徑功能表,選擇[新增],然後選取 [新項目]。

  5. 在 [新增項目] 對話方塊中,選取 [文字檔] 範本。

  6. 在 [Name]方塊中,輸入 CustomAction.spdata,然後選取 [新增] 按鈕。

  7. 將下列 XML 加入至 CustomAction.spdata 檔案,然後儲存並關閉檔案。

    <?xml version="1.0" encoding="utf-8"?>
    <ProjectItem Type="Contoso.CustomAction" DefaultFile="Elements.xml" 
     xmlns="https://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
      <Files>
        <ProjectItemFile Source="Elements.xml" Target="$fileinputname$\" Type="ElementManifest" />
      </Files>
    </ProjectItem>
    

    此檔案包含專案項目所包含檔案的相關資訊。ProjectItem 項目的 Type 屬性必須設為與傳遞至專案項目定義 (您之前在本逐步解說中建立的 CustomActionProjectItemTypeProvider 類別) 上的 SharePointProjectItemTypeAttribute 相同的字串。如需 .spdata 檔案內容的詳細資訊,請參閱 SharePoint 專案項目結構描述參考

  8. 在 [方案總管],開啟 [ItemTemplate]專案的捷徑功能表,選擇[新增],然後選取 [新項目]。

  9. 在 [新增項目] 對話方塊中,選取 [XML 檔] 範本。

  10. 在 [Name]方塊中,輸入 Elements.xml,然後選取 [新增] 按鈕。

  11. 以下列 XML 取代 Elements.xml 檔案的內容,然後儲存並關閉檔案。

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements Id="$guid8$" xmlns="https://schemas.microsoft.com/sharepoint/">
      <CustomAction Id="Replace this with a GUID or some other unique string"
                    GroupId="SiteActions"
                    Location="Microsoft.SharePoint.StandardMenu"
                    Sequence="1000"
                    Title="Replace this with your title"
                    Description="Replace this with your description" >
        <UrlAction Url="~site/Lists/Tasks/AllItems.aspx"/>
      </CustomAction>
    </Elements>
    

    此檔案會定義在 SharePoint 網站的 [網站動作] 功能表上建立功能表項目的預設自訂動作。當使用者選取功能表項目時,在 UrlAction 項目指定的 URL 在這台瀏覽器中開啟。如需可用於定義自訂動作之 XML 項目的詳細資訊,請參閱自訂動作定義 (英文)。

  12. (選擇性) 開啟 ItemTemplate.ico 檔案並加以修改,使其具有您可辨識的設計。此圖示將在 [加入新項目] 對話方塊中該專案項目旁顯示。

  13. 在 [方案總管],開啟 [ItemTemplate]專案的捷徑功能表,然後選取 [卸載專案]。

  14. 再次開啟 [ItemTemplate]專案的捷徑功能表,然後選取 [編輯 ItemTemplate.csproj]或 [編輯 ItemTemplate.vbproj]。

  15. 在專案檔中尋找下列 VSTemplate 項目。

    <VSTemplate Include="ItemTemplate.vstemplate">
    
  16. 以下列 XML 取代這個 VSTemplate 項目,然後儲存並關閉檔案。

    <VSTemplate Include="ItemTemplate.vstemplate">
      <OutputSubPath>SharePoint\SharePoint14</OutputSubPath>
    </VSTemplate>
    

    OutputSubPath 項目會在您建置專案時用以建立項目範本的路徑中,指定其他資料夾。這裡指定的資料夾可確保項目範本可供使用,只有在用戶 [新增項目] 開啟對話方塊時, [SharePoint] 展開節點,然後選取 [2010] 節點。

  17. 在 [方案總管],開啟 [ItemTemplate]專案的捷徑功能表,然後選取 [重新載入專案]。

建立 VSIX 套件以部署專案項目

若要部署擴充功能,請在您的方案中使用 VSIX 專案,以建立 VSIX 套件。首先,修改 VSIX 專案包含的 source.extension.vsixmanifest 檔案,來設定 VSIX 套件。接著,建置方案來建立 VSIX 套件。

若要設定和建立 VSIX 套件

  1. 在 [方案總管],開啟 [source.extension.vsixmanifest]檔案的捷徑功能表在 CustomActionProjectItem 專案,然後選取 [開啟]。

    Visual Studio 會在資訊清單編輯器中開啟檔案。source.extension.vsixmanifest 檔案是所有 VSIX 套件所需要的 extension.vsixmanifest 檔案的基準。如需這個檔案的詳細資訊,請參閱VSIX 擴充結構描述參考

  2. 在 [產品名稱]方塊中,輸入 自訂動作專案項目。

  3. 在 [作者]方塊中,輸入 Contoso。

  4. 在 [Description]方塊中,輸入 表示自訂動作的 SharePoint 專案項目。

  5. 在 [資產] 索引標籤上,選取 [新增] 按鈕。

    [將新的屬性] 對話方塊隨即出現。

  6. 在 [型別] 清單中,選取 [Microsoft.VisualStudio.ItemTemplate]。

    注意事項注意事項

    這個值對應於 extension.vsixmanifest 檔案中的 ItemTemplate 項目。此項目會識別 VSIX 套件中包含專案項目範本的子資料夾。如需詳細資訊,請參閱ItemTemplate Element

  7. 在 [Source] 清單中,選取 [在目前方案中的專案]。

  8. 在 [Project] 清單中,選取 [ItemTemplate],然後選取 [確定] 按鈕。

  9. 在 [資產] 索引標籤,再選取 [新增] 按鈕。

    [將新的屬性] 對話方塊隨即出現。

  10. 在 [型別] 清單中,選取 [Microsoft.VisualStudio.MefComponent]。

    注意事項注意事項

    這個值對應於 extension.vsixmanifest 檔案中的 MefComponent 項目。這個項目指定 VSIX 套件中的擴充組件名稱。如需詳細資訊,請參閱MEFComponent Element

  11. 在 [Source] 清單中,選取 [在目前方案中的專案]。

  12. 在 [Project] 清單中,選取 [ProjectItemDefinition]。

  13. 選擇 [確定] 按鈕。

  14. 在功能表列上,選擇, [組建][建置方案],然後確定專案在編譯時未發生任何錯誤。

  15. 確定 CustomActionProjectItem 專案的建置輸出資料夾包含 CustomActionProjectItem.vsix 檔。

    根據預設,建置輸出資料夾為 ..\bin\Debug 資料夾 (位於包含 CustomActionProjectItem 專案的資料夾下)。

測試專案項目

您現在可以測試專案項目。首先,在 Visual Studio 的實驗執行個體中開始偵錯 CustomActionProjectItem 方案。接著,在 Visual Studio 的實驗執行個體中,測試 SharePoint 專案中的 [自訂動作] 專案項目。最後,建置並執行 SharePoint 專案,確認自訂動作功能正常。

若要開始偵錯方案

  1. 重新啟動以管理認證的 Visual Studio,然後開啟 CustomActionProjectItem 方案。

  2. 開啟 CustomAction 程式碼檔案,然後將中斷點加入至 InitializeType 方法的第一行程式碼。

  3. 選取 [F5]索引鍵開始偵錯。

    Visual Studio 會將擴充功能安裝至 %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Custom Action Project Item\1.0,並啟動 Visual Studio 的實驗執行個體。您在這個執行個體中測試專案項目 Visual Studio。

若要在 Visual Studio 中測試專案項目

  1. 在 Visual Studio 的實驗執行個體,在功能表列上,選擇 [檔案], [新增], [Project]。

  2. 展開 Visual C# 或 [Visual Basic](視語言而定的項目範本支援), [SharePoint] 展開,然後選取 [2010] 節點。

  3. 在專案範本清單中,選取 [SharePoint 2010 專案]。

  4. 在 [Name]方塊中,輸入 CustomActionTest,然後選取 [確定] 按鈕。

  5. 在 [SharePoint 自訂精靈],輸入要用於偵錯的網站 URL,然後選擇 [完成] 按鈕。

  6. 在 [方案總管],請開啟專案節點的捷徑功能表,選擇[新增],然後選取 [新項目]。

  7. 在 [新增項目] 對話方塊中,選取 [2010] 節點在 [SharePoint] 節點下。

    確認 [自訂動作] 項目出現在專案項目的清單中。

  8. 選取 [自訂動作] 項目,然後選取 [新增] 按鈕。

    Visual Studio 隨即將名為的專案的 [CustomAction1]的項目並在編輯器中開啟 Elements.xml 檔案。

  9. 確認另一個 Visual Studio 執行個體中的程式碼在您之前於 InitializeType 方法中設定的中斷點停止。

  10. 選取 [F5]索引鍵繼續偵錯專案。

  11. 在 Visual Studio 的實驗執行個體,在 [方案總管],開啟 [CustomAction1]節點的捷徑功能表,然後選取 [檢視自訂動作設計工具]。

  12. 確認訊息方塊是否出現,然後選取 [確定] 按鈕。

    您可以使用此捷徑功能表為開發人員提供額外的選項或命令,例如顯示自訂動作的設計工具。

  13. 在功能表列上,選擇 [檢視], [輸出]。

    [輸出] 視窗隨即開啟。

  14. 在 [方案總管],開啟 [CustomAction1]項目的捷徑功能表,然後將它的名稱加入至 MyCustomAction。

    在 [輸出] 視窗,確認訊息隨即出現。這個訊息 ProjectItemNameChanged 事件處理常式撰寫在 CustomActionProjectItemTypeProvider 類別定義。當開發人員修改專案項目時,您可以處理此事件和其他專案項目事件以實作自訂行為。

若要測試 SharePoint 中的自訂動作

  1. 在 Visual Studio 的實驗執行個體中,開啟屬於 [MyCustomAction]專案項目之子系的 Elements.xml 檔案。

  2. 在 Elements.xml 檔案,請進行下列變更,然後儲存檔案:

    • 在 CustomAction 項目,設定 Id 屬性為 GUID 或其他唯一的字串,如下列範例所示:

      Id="cd85f6a7-af2e-44ab-885a-0c795b52121a"
      
    • 在 CustomAction 項目,設定 Title 屬性,如下列範例所示:

      Title="SharePoint Developer Center"
      
    • 在 CustomAction 項目,設定 Description 屬性,如下列範例所示:

      Description="Opens the SharePoint Developer Center Web site."
      
    • 在 UrlAction 項目,設定 Url 屬性,如下列範例所示:

      Url="https://msdn.microsoft.com/sharepoint/default.aspx"
      
  3. 選擇 F5 鍵。

    這個自訂動作在專案的 [網站 URL] 屬性指定的 SharePoint 網站封裝並部署。這個瀏覽器開啟此網站的預設網頁。

    注意事項注意事項

    如果 [已停用指令碼偵錯] 對話方塊出現時,請選擇 [] 按鈕繼續偵錯專案。

  4. 在 [設置動作] 功能表中,選擇 [SharePoint 開發人員中心],確認瀏覽器開啟此網站 https://msdn.microsoft.com/sharepoint/default.aspx,然後關閉這個瀏覽器。

清理開發電腦

在您完成測試專案項目之後,請從 Visual Studio 的實驗執行個體中移除專案項目範本。

若要清理開發電腦

  1. 在 Visual Studio 的實驗執行個體,在功能表列上,選擇, [工具][副檔名和更新]。

    [副檔名和更新] 對話方塊隨即開啟。

  2. 在擴充功能清單中,選取 [自訂動作專案項目],然後選取 [解除安裝] 按鈕。

  3. 在出現的對話方塊中,選取 [] 按鈕確認您要解除安裝擴充功能。

  4. 選取 [立即重新啟動] 按鈕完成解除安裝。

  5. 結束 CustomActionProjectItem 方案已開啟 Visual Studio 的實驗執行個體和執行個體。

後續步驟

當您完成本逐步解說之後,您可以將精靈加入至項目範本。當使用者將 [自訂動作] 專案項目加入至 SharePoint 專案時,精靈會收集動作的相關資訊 (例如其位置並巡覽至的 URL,當動作被選取時) 並將此資訊加入至新專案項目的 Elements.xml 檔案。如需詳細資訊,請參閱逐步解說:使用項目範本建立自訂動作專案項目 (第 2 部分)

請參閱

工作

逐步解說:使用項目範本建立自訂動作專案項目 (第 2 部分)

建立圖示或其他影像 (圖示影像編輯器)

參考

Visual Studio 範本結構描述參考

圖示影像編輯器

概念

定義自訂 SharePoint 專案項目類型

為 SharePoint 專案項目建立項目範本和專案範本

使用 SharePoint 專案服務