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

當您在 Visual Studio 中定義自訂類型的 SharePoint 專案項目,並為其建立與項目範本之間的關聯後,您也可以提供該範本專用的精靈。您可以使用精靈,在使用者利用您的範本加入專案項目的新執行個體時,向使用者收集資訊。您收集到的資訊可用來初始化專案項目。

在本逐步解說中,您將會在逐步解說:使用項目範本建立自訂動作專案項目 (第 1 部分)示範的 [自訂動作] 專案項目中加入精靈。當使用者將 [自訂動作] 專案項目加入至 SharePoint 專案時,精靈會收集此自訂動作的相關資訊 (例如其位置和巡覽 URL,當使用者選取它) 並將此資訊加入至新專案項目的 Elements.xml 檔案。

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

  • 建立與項目範本有關聯之自訂 SharePoint 專案項目類型的精靈。

  • SharePoint 定義類似於內建精靈的自訂精靈 UI 專案在 Visual Studio 的項目。

  • 使用可取代的參數,以您在精靈中收集到的資料初始化 SharePoint 專案檔。

  • 對精靈進行偵錯和測試。

注意事項注意事項

您可以下載包含已完成的專案、程式碼和其他檔案逐步解說的從下列位置的範例: SharePoint 專案檔工具擴充性逐步解說.

必要條件

若要執行這個逐步解說,您必須先完成逐步解說:使用項目範本建立自訂動作專案項目 (第 1 部分),以建立 CustomActionProjectItem 方案。

此外,您的開發電腦上必須要有下列元件,才能完成此逐步解說:

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

建立精靈專案

若要完成這個逐步解說,您必須將專案加入至 CustomActionProjectItem 方案在 逐步解說:使用項目範本建立自訂動作專案項目 (第 1 部分)建立。您將會在這個專案中實作 IWizard 介面並定義精靈 UI。

若要建立精靈專案

  1. 在 Visual Studio 中,開啟 CustomActionProjectItem 方案

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

    注意事項注意事項

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

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

  4. 在 [新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 .NET Framework 版本的清單中選取。

  5. 選取 [WPF 使用者控制項程式庫] 專案範本,然後將專案命名為 ItemTemplateWizard,然後選取 [確定] 按鈕。

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

  6. 從專案刪除 UserControl1 項目。

設定精靈專案

在建立精靈之前,您必須將 Windows Presentation Foundation (WPF) 視窗、程式碼檔案和組件參考加入至專案。

若要設定精靈專案

  1. 在 [方案總管],開啟來自 [ItemTemplateWizard]專案節點的捷徑功能表,然後選取 [屬性]。

  2. 在 [專案設計工具],請確認目標 Framework 設定為 .NET Framework 4.5。

    對於 Visual C# 專案中,您可以在 [應用程式] 選項的值。對於 Visual Basic 專案中,您可以在 [編譯] 選項的值。如需詳細資訊,請參閱HOW TO:以 .NET Framework 版本為目標

  3. 在 [ItemTemplateWizard]專案,請將 [視窗 (WPF)]項目加入至專案,然後 WizardWindow命名項目。

  4. 加入名為和的兩個字串 CustomActionWizard 程式碼檔案。

  5. 開啟 [ItemTemplateWizard]專案的捷徑功能表,然後選取 [新增參考]。

  6. 參考管理員- ItemTemplateWizard 對話方塊,在 [組件] 節點底下,選取[延伸] 節點。

  7. 在下列組件旁邊的核取方塊,然後選取 [確定] 按鈕:

    • EnvDTE

    • Microsoft.VisualStudio.Shell.11.0

    • Microsoft.VisualStudio.TemplateWizardInterface

  8. 在 [方案總管],在 ItemTemplateWizard 專案的 [參考] 資料夾,選取 [EnvDTE]參考。

    注意事項注意事項

    在 Visual Basic 專案中,只有在已選取General, Projects and Solutions, Options Dialog Box中的 [永遠顯示方案] 核取方塊時,才會出現 [參考] 資料夾。

  9. 在 [屬性] 視窗,變更 [內嵌 Interop 型別] 屬性的值變更為 [錯誤]。

定義自訂動作的預設位置和 ID 字串

每個自訂動作都會有一個位置和 ID,分別由 Elements.xml 檔案中 CustomAction 項目的 Location 和 GroupID 屬性所指定。在這個步驟中,您會為 ItemTemplateWizard 專案中的這些屬性定義一些有效字串。當您完成本逐步解說時,這些字串寫入 Elements.xml 檔案在自訂動作專案項目,當使用者在精靈中指定位置和 ID。

為簡單起見,此範例僅支援可用預設位置和 ID 的子集。如需完整清單,請參閱預設的自訂動作位置和 ID (英文)。

若要定義預設位置和 ID 字串

  1. 開啟。

  2. 在 [ItemTemplateWizard]專案,請使用下列程式碼取代字串程式碼檔案中的程式碼。

    ' This sample only supports several custom action locations and their group IDs. 
    Friend Class CustomActionLocations
        Friend Const ListEdit As String = "Microsoft.SharePoint.ListEdit"
        Friend Const StandardMenu As String = "Microsoft.SharePoint.StandardMenu"
    End Class
    
    Friend Class StandardMenuGroupIds
        Friend Const Actions As String = "ActionsMenu"
        Friend Const ActionsSurvey As String = "ActionsMenuForSurvey"
        Friend Const NewMenu As String = "NewMenu"
        Friend Const Settings As String = "SettingsMenu"
        Friend Const SettingsSurvey As String = "SettingsMenuForSurvey"
        Friend Const SiteActions As String = "SiteActions"
        Friend Const Upload As String = "UploadMenu"
        Friend Const ViewSelector As String = "ViewSelectorMenu"
    End Class
    
    Friend Class ListEditGroupIds
        Friend Const Communications As String = "Communications"
        Friend Const GeneralSettings As String = "GeneralSettings"
        Friend Const Permissions As String = "Permissions"
    End Class
    
    Friend Class DefaultTextBoxStrings
        Friend Const TitleText As String = "Replace this with your title"
        Friend Const DescriptionText As String = "Replace this with your description"
        Friend Const UrlText As String = "~site/Lists/Tasks/AllItems.aspx"
    End Class
    
    
    namespace ItemTemplateWizard
    {
        // This sample only supports several custom action locations and their group IDs. 
        internal class CustomActionLocations
        {
            internal const string ListEdit = "Microsoft.SharePoint.ListEdit";
            internal const string StandardMenu = "Microsoft.SharePoint.StandardMenu";
        }
    
        internal class StandardMenuGroupIds
        {
            internal const string Actions = "ActionsMenu";
            internal const string ActionsSurvey = "ActionsMenuForSurvey";
            internal const string NewMenu = "NewMenu";
            internal const string Settings = "SettingsMenu";
            internal const string SettingsSurvey = "SettingsMenuForSurvey";
            internal const string SiteActions = "SiteActions";
            internal const string Upload = "UploadMenu";
            internal const string ViewSelector = "ViewSelectorMenu";
        }
    
        internal class ListEditGroupIds
        {
            internal const string Communications = "Communications";
            internal const string GeneralSettings = "GeneralSettings";
            internal const string Permissions = "Permissions";
        }
    
        internal class DefaultTextBoxStrings
        {
            internal const string TitleText = "Replace this with your title";
            internal const string DescriptionText = "Replace this with your description";
            internal const string UrlText = "~site/Lists/Tasks/AllItems.aspx";
        }
    }
    

建立精靈 UI

加入 XAML 以定義精靈的 UI,並加入一些程式碼以將精靈中的部分控制項繫結至 ID 字串。您建立的精靈類似 SharePoint 專案內建精靈在 Visual Studio。

若要建立精靈 UI

  1. 在 [ItemTemplateWizard]專案,開啟 [WizardWindow.xaml]檔案的捷徑功能表,然後選取 [開啟] 以設計工具開啟的視窗。

  2. 在 [XAML] 檢視中,以下列 XAML 取代目前的 XAML。這個 XAML 定義的 UI 包含標題、用於指定自訂動作行為的控制項和位於視窗底部的導覽按鈕。

    注意事項注意事項

    加入這個程式碼之後,將會出現一些編譯錯誤。在後續步驟中加入程式碼時,這些錯誤將不存在。

    <ui:DialogWindow x:Class="ItemTemplateWizard.WizardWindow"
                     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:ui="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.11.0"        
                     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
                     Title="SharePoint Customization Wizard" Height="500" Width="700" ResizeMode="NoResize" 
                     Loaded="Window_Loaded" TextOptions.TextFormattingMode="Display">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="75*" />
                <RowDefinition Height="364*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="60*" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Name="headingGrid" Background="White">
                <Label Grid.Row="0" Content="Configure the Custom Action" Name="pageHeaderLabel" HorizontalAlignment="Left" 
                       VerticalAlignment="Center" Margin="18,0,0,0" FontWeight="ExtraBold" />
            </Grid>
            <Grid Grid.Row="1" Name="mainGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="400*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical">
                    <Label Margin="0,20,0,0" Content="Location:" Name="locationLabel" FontWeight="Bold" />
                    <RadioButton Content="_List Edit" Margin="5,0,0,0" Name="listEditRadioButton" 
                                 Checked="listEditRadioButton_Checked" FontWeight="Normal"  />
                    <RadioButton Content="_Standard Menu" Margin="5,5,0,0" Name="standardMenuRadioButton" 
                                 Checked="standardMenuRadioButton_Checked" FontWeight="Normal" />
                </StackPanel>
                <Label Grid.Row="1" Grid.Column="1" Margin="0,15,0,0" Content="_Group ID:" Name="groupIdLabel" 
                       FontWeight="Bold" Target="{Binding ElementName=idComboBox}" />
                <ComboBox Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Margin="0,15,0,0" Height="23" 
                          Width="253" Name="idComboBox" IsEditable="False" IsSynchronizedWithCurrentItem="True" />
                <Label Grid.Row="2" Grid.Column="1" Margin="0,15,0,0" Content="_Title:" Name="titleLabel" 
                       FontWeight="Bold" Target="{Binding ElementName=titleTextBox}" />
                <TextBox Grid.Row="2" Grid.Column="2" HorizontalAlignment="Left" Margin="0,15,0,0" Height="23" 
                         Name="titleTextBox" Width="410" Text="" />
                <Label Grid.Row="3" Grid.Column="1" Margin="0,15,0,0" Content="_Description:" Name="descriptionLabel" 
                       FontWeight="Bold" Target="{Binding ElementName=descriptionTextBox}" />
                <TextBox Grid.Row="3" Grid.Column="2" HorizontalAlignment="Left" Margin="0,15,0,0" Height="23" 
                         Name="descriptionTextBox" Width="410" Text="" />
                <Label Grid.Row="4" Grid.Column="1" Margin="0,15,0,0" Content="_URL:" Height="28" Name="urlLabel" 
                       FontWeight="Bold" Target="{Binding ElementName=urlTextBox}" />
                <TextBox Grid.Row="4" Grid.Column="2" HorizontalAlignment="Left" Margin="0,15,0,0" Height="23" 
                         Name="urlTextBox" Width="410" Text="" />
            </Grid>
            <Rectangle Grid.Row="2" Name="separatorRectangle" Fill="White"  />
            <StackPanel Grid.Row="3" Name="navigationPanel" Orientation="Horizontal">
                <Button Content="_Finish" Margin="500,0,0,0" Height="25" Name="finishButton" Width="85" 
                        Click="finishButton_Click" IsDefault="True" />
                <Button Content="Cancel" Margin="10,0,0,0" Height="25" Name="cancelButton" Width="85" IsCancel="True" />
            </StackPanel>
        </Grid>
    </ui:DialogWindow>
    
    注意事項注意事項

    在這個 XAML 會建立的視窗 DialogWindow 從基底類別衍生。當您將自訂 WPF 對話方塊加入至 Visual Studio 時,建議您從這個類別衍生自己的對話方塊具有一致的樣式與在 Visual Studio 的其他對話方塊並避免可能發生的強制回應對話方塊的問題。如需詳細資訊,請參閱How to: 建立和管理對話方塊

  3. 如果您正在開發 Visual Basic 專案,請從Window 項目的 x:Class 屬性的 WizardWindow 類別名稱移除 ItemTemplateWizard 命名空間。這個項目在 XAML 的第一行。當您完成時,第一行應類似下列程式碼:

    <Window x:Class="WizardWindow"
    
  4. 在 WizardWindow.xaml 檔案的程式碼後置檔案中,以下列程式碼取代目前的程式碼。

    Public Class WizardWindow
        Private standardMenuGroups As List(Of String)
        Private listEditGroups As List(Of String)
        Private standardMenuGroupIdBinding As Binding
        Private listEditGroupIdBinding As Binding
        Private standardMenuGroupIdBindingView As ListCollectionView
        Private listEditGroupIdBindingView As ListCollectionView
    
        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            standardMenuGroups = New List(Of String) From {
                StandardMenuGroupIds.Actions,
                StandardMenuGroupIds.ActionsSurvey,
                StandardMenuGroupIds.NewMenu,
                StandardMenuGroupIds.Settings,
                StandardMenuGroupIds.SettingsSurvey,
                StandardMenuGroupIds.SiteActions,
                StandardMenuGroupIds.Upload,
                StandardMenuGroupIds.ViewSelector}
            listEditGroups = New List(Of String) From {
                ListEditGroupIds.Communications,
                ListEditGroupIds.GeneralSettings,
                ListEditGroupIds.Permissions}
    
            standardMenuGroupIdBinding = New Binding()
            standardMenuGroupIdBinding.Source = standardMenuGroups
            listEditGroupIdBinding = New Binding()
            listEditGroupIdBinding.Source = listEditGroups
    
            standardMenuGroupIdBindingView = CType(CollectionViewSource.GetDefaultView(standardMenuGroups), ListCollectionView)
            listEditGroupIdBindingView = CType(CollectionViewSource.GetDefaultView(listEditGroups), ListCollectionView)
    
            standardMenuRadioButton.IsChecked = True
        End Sub
    
        Private Sub standardMenuRadioButton_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
            BindingOperations.ClearBinding(idComboBox, ComboBox.ItemsSourceProperty)
            idComboBox.SetBinding(ComboBox.ItemsSourceProperty, standardMenuGroupIdBinding)
            standardMenuGroupIdBindingView.MoveCurrentToFirst()
        End Sub
    
        Private Sub listEditRadioButton_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
            BindingOperations.ClearBinding(idComboBox, ComboBox.ItemsSourceProperty)
            idComboBox.SetBinding(ComboBox.ItemsSourceProperty, listEditGroupIdBinding)
            listEditGroupIdBindingView.MoveCurrentToFirst()
        End Sub
    
        Private Sub finishButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Me.DialogResult = True
            Me.Close()
        End Sub
    End Class
    
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using Microsoft.VisualStudio.PlatformUI;
    
    namespace ItemTemplateWizard
    {
        public partial class WizardWindow : DialogWindow
        {
            private List<string> standardMenuGroups;
            private List<string> listEditGroups;
            private Binding standardMenuGroupIdBinding;
            private Binding listEditGroupIdBinding;
            private ListCollectionView standardMenuGroupIdBindingView;
            private ListCollectionView listEditGroupIdBindingView;
    
            public WizardWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                standardMenuGroups = new List<string>() { 
                    StandardMenuGroupIds.Actions,
                    StandardMenuGroupIds.ActionsSurvey,
                    StandardMenuGroupIds.NewMenu, 
                    StandardMenuGroupIds.Settings, 
                    StandardMenuGroupIds.SettingsSurvey,
                    StandardMenuGroupIds.SiteActions, 
                    StandardMenuGroupIds.Upload, 
                    StandardMenuGroupIds.ViewSelector };
                listEditGroups = new List<string>() { 
                    ListEditGroupIds.Communications, 
                    ListEditGroupIds.GeneralSettings,
                    ListEditGroupIds.Permissions };
    
                standardMenuGroupIdBinding = new Binding();
                standardMenuGroupIdBinding.Source = standardMenuGroups;
                listEditGroupIdBinding = new Binding();
                listEditGroupIdBinding.Source = listEditGroups;
    
                standardMenuGroupIdBindingView = (ListCollectionView)CollectionViewSource.GetDefaultView(standardMenuGroups);
                listEditGroupIdBindingView = (ListCollectionView)CollectionViewSource.GetDefaultView(listEditGroups);
    
                standardMenuRadioButton.IsChecked = true;
            }
    
            private void standardMenuRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                BindingOperations.ClearBinding(idComboBox, ComboBox.ItemsSourceProperty);
                idComboBox.SetBinding(ComboBox.ItemsSourceProperty, standardMenuGroupIdBinding);
                standardMenuGroupIdBindingView.MoveCurrentToFirst();
            }
    
            private void listEditRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                BindingOperations.ClearBinding(idComboBox, ComboBox.ItemsSourceProperty);
                idComboBox.SetBinding(ComboBox.ItemsSourceProperty, listEditGroupIdBinding);
                listEditGroupIdBindingView.MoveCurrentToFirst();
            }
    
            private void finishButton_Click(object sender, RoutedEventArgs e)
            {
                this.DialogResult = true;
                this.Close();
            }
        }
    }
    

實作精靈

精靈的功能是藉由實作 IWizard 介面來定義。

若要實作精靈

  • 在 [ItemTemplateWizard]專案,開啟 [CustomActionWizard]程式碼檔,然後以下列程式碼取代此檔案中目前的程式碼:

    Imports EnvDTE
    Imports Microsoft.VisualStudio.TemplateWizard
    Imports System
    Imports System.Collections.Generic
    
    Public Class CustomActionWizard
        Implements IWizard
    
        Private wizardPage As WizardWindow
    
    #Region "IWizard Methods"
    
        Public Sub RunStarted(ByVal automationObject As Object, ByVal replacementsDictionary As Dictionary(Of String, String), _
            ByVal runKind As WizardRunKind, ByVal customParams() As Object) Implements IWizard.RunStarted
            wizardPage = New WizardWindow()
            Dim dialogCompleted? As Boolean = wizardPage.ShowModal()
    
            If (dialogCompleted = True) Then
                PopulateReplacementDictionary(replacementsDictionary)
            Else
                Throw New WizardCancelledException()
            End If
        End Sub
    
        ' Always return true; this IWizard implementation throws a WizardCancelledException
        ' that is handled by Visual Studio if the user cancels the wizard.
        Public Function ShouldAddProjectItem(ByVal filePath As String) As Boolean _
            Implements IWizard.ShouldAddProjectItem
            Return True
        End Function
    
        ' The following IWizard methods are not implemented in this example.
        Public Sub BeforeOpeningFile(ByVal projectItem As ProjectItem) _
            Implements IWizard.BeforeOpeningFile
        End Sub
    
        Public Sub ProjectFinishedGenerating(ByVal project As Project) _
            Implements IWizard.ProjectFinishedGenerating
        End Sub
    
        Public Sub ProjectItemFinishedGenerating(ByVal projectItem As ProjectItem) _
            Implements IWizard.ProjectItemFinishedGenerating
        End Sub
    
        Public Sub RunFinished() Implements IWizard.RunFinished
        End Sub
    
    #End Region
    
        Private Sub PopulateReplacementDictionary(ByVal replacementsDictionary As Dictionary(Of String, String))
    
            ' Fill in the replacement values from the UI selections on the wizard page. These values are automatically inserted
            ' into the Elements.xml file for the custom action.
            Dim locationValue As String = If(wizardPage.standardMenuRadioButton.IsChecked,
                    CustomActionLocations.StandardMenu, CustomActionLocations.ListEdit)
            replacementsDictionary.Add("$LocationValue$", locationValue)
            replacementsDictionary.Add("$GroupIdValue$", CType(wizardPage.idComboBox.SelectedItem, String))
            replacementsDictionary.Add("$IdValue$", Guid.NewGuid().ToString())
    
            Dim titleText As String = DefaultTextBoxStrings.TitleText
            If False = String.IsNullOrEmpty(wizardPage.titleTextBox.Text) Then
                titleText = wizardPage.titleTextBox.Text
            End If
    
            Dim descriptionText As String = DefaultTextBoxStrings.DescriptionText
            If False = String.IsNullOrEmpty(wizardPage.descriptionTextBox.Text) Then
                descriptionText = wizardPage.descriptionTextBox.Text
            End If
    
            Dim urlText As String = DefaultTextBoxStrings.UrlText
            If False = String.IsNullOrEmpty(wizardPage.urlTextBox.Text) Then
                urlText = wizardPage.urlTextBox.Text
            End If
    
            replacementsDictionary.Add("$TitleValue$", titleText)
            replacementsDictionary.Add("$DescriptionValue$", descriptionText)
            replacementsDictionary.Add("$UrlValue$", urlText)
        End Sub
    End Class
    
    using EnvDTE;
    using Microsoft.VisualStudio.TemplateWizard;
    using System;
    using System.Collections.Generic;
    
    namespace ItemTemplateWizard
    {
        public class CustomActionWizard : IWizard
        {
            private WizardWindow wizardPage;
    
            public CustomActionWizard()
            {
            }
    
            #region IWizard Methods
    
            public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, 
                WizardRunKind runKind, object[] customParams)
            {
                wizardPage = new WizardWindow();
                Nullable<bool> dialogCompleted = wizardPage.ShowModal();
    
                if (dialogCompleted == true)
                {
                    PopulateReplacementDictionary(replacementsDictionary);
                }
                else
                {
                    throw new WizardCancelledException();
                }
            }
    
            // Always return true; this IWizard implementation throws a WizardCancelledException
            // that is handled by Visual Studio if the user cancels the wizard.
            public bool ShouldAddProjectItem(string filePath)
            {
                return true;
            }
    
            // The following IWizard methods are not implemented in this example.
            public void BeforeOpeningFile(ProjectItem projectItem)
            {
            }
    
            public void ProjectFinishedGenerating(Project project)
            {
            }
    
            public void ProjectItemFinishedGenerating(ProjectItem projectItem)
            {
            }
    
            public void RunFinished()
            {
            }
    
            #endregion
    
            private void PopulateReplacementDictionary(Dictionary<string, string> replacementsDictionary)
            {
                // Fill in the replacement values from the UI selections on the wizard page. These values are automatically inserted
                // into the Elements.xml file for the custom action.
                string locationValue = (bool)wizardPage.standardMenuRadioButton.IsChecked ?
                    CustomActionLocations.StandardMenu : CustomActionLocations.ListEdit;
                replacementsDictionary.Add("$LocationValue$", locationValue);
                replacementsDictionary.Add("$GroupIdValue$", (string)wizardPage.idComboBox.SelectedItem);
                replacementsDictionary.Add("$IdValue$", Guid.NewGuid().ToString());
    
                string titleText = DefaultTextBoxStrings.TitleText;
                if (!String.IsNullOrEmpty(wizardPage.titleTextBox.Text))
                {
                    titleText = wizardPage.titleTextBox.Text;
                }
    
                string descriptionText = DefaultTextBoxStrings.DescriptionText;
                if (!String.IsNullOrEmpty(wizardPage.descriptionTextBox.Text))
                {
                    descriptionText = wizardPage.descriptionTextBox.Text;
                }
    
                string urlText = DefaultTextBoxStrings.UrlText;
                if (!String.IsNullOrEmpty(wizardPage.urlTextBox.Text))
                {
                    urlText = wizardPage.urlTextBox.Text;
                }
    
                replacementsDictionary.Add("$TitleValue$", titleText);
                replacementsDictionary.Add("$DescriptionValue$", descriptionText);
                replacementsDictionary.Add("$UrlValue$", urlText);
            }
        }
    }
    

檢查點

在逐步解說中進行至此處時,精靈的所有程式碼都會位於專案中。建置專案,以確定在編譯時未發生任何錯誤。

若要建置您的專案

  • 在功能表列上,選擇, [組建][建置方案]。

建立精靈與項目範本的關聯

現在您已實作精靈,接著必須與 [自訂動作] 項目範本藉由完成三個主要步驟:

  1. 使用強式名稱簽署精靈組件。

  2. 取得精靈組件的公開金鑰語彙基元。

  3. 將精靈組件的參考加入至 [自訂動作] 項目範本的 .vstemplate 檔案中。

若要使用強式名稱簽署精靈組件

  1. 在 [方案總管],開啟來自 [ItemTemplateWizard]專案節點的捷徑功能表,然後選取 [屬性]。

  2. 選取 [簽署] 索引標籤上的 [簽署組件] 核取方塊。

  3. 在 [選擇強式名稱金鑰檔] 清單中, [<New...>] 選擇。

  4. 在 [建立強式名稱金鑰] 對話方塊中,輸入名稱,清除 [保護我的與密碼的金鑰檔] 核取方塊,然後選取 [確定] 按鈕。

  5. 在功能表列上,選擇, [組建][建置方案]。

若要取得精靈組件的公開金鑰語彙基元

  1. 在 Visual Studio 命令提示字元視窗中,執行下列命令,以取代 PathToWizardAssembly 與完整路徑 ItemTemplateWizard 專案的 ItemTemplateWizard.dll 組件位於您開發電腦。

    sn.exe -T PathToWizardAssembly
    

    ItemTemplateWizard.dll 組件的公開金鑰語彙基元會寫入至 [Visual Studio 命令提示字元] 視窗。

  2. 讓 [Visual Studio 命令提示字元] 視窗保持開啟。您將需要公開金鑰語彙基元完成下面的程序。

若要在 .vstemplate 檔案中加入精靈組件的參考

  1. 在 [方案總管],展開 [ItemTemplate]專案節點,然後開啟 ItemTemplate.vstemplate 檔案。

  2. 在檔案結尾附近,於 </TemplateContent> 與 </VSTemplate> 標記之間加入 WizardExtension 項目。用您在上一個程序取得的公開金鑰語彙基元取代 PublicKeyToken 屬性的 YourToken 值。

    <WizardExtension>
      <Assembly>ItemTemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=YourToken</Assembly>
      <FullClassName>ItemTemplateWizard.CustomActionWizard</FullClassName>
    </WizardExtension>
    

    如需 WizardExtension 項目的詳細資訊,請參閱 WizardExtension 項目 (Visual Studio 範本)

  3. 儲存並關閉檔案。

將可取代的參數加入至項目範本中的 Elements.xml 檔案

將數個可取代的參數加入至 ItemTemplate 專案中的 Elements.xml 檔案。這些參數會以您先前定義之 CustomActionWizard 類別中的 PopulateReplacementDictionary 方法初始化。當使用者將 [自訂動作] 專案項目加入至專案時,Visual Studio 即會自動在新的專案項目中,將 Elements.xml 檔案中的這些參數取代為使用者在精靈中所指定的值。

可取代的參數是以貨幣符號的語彙基元 ($) 字元開頭和結尾。除了定義您可取代的參數以外,您可以使用內建參數 SharePoint 專案系統定義和初始化。如需詳細資訊,請參閱可置換的參數

若要將可取代的參數加入至 Elements.xml 檔案

  1. 在 ItemTemplate 專案中,以下列 XML 取代 Elements.xml 檔案的內容。

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements Id="$guid8$" xmlns="https://schemas.microsoft.com/sharepoint/">
      <CustomAction Id="$IdValue$"
                    GroupId="$GroupIdValue$"
                    Location="$LocationValue$"
                    Sequence="1000"
                    Title="$TitleValue$"
                    Description="$DescriptionValue$" >
        <UrlAction Url="$UrlValue$"/>
      </CustomAction>
    </Elements>
    

    新的 XML 會將 Id、GroupId、Location、Description 和 Url 屬性的值變更為可取代的參數。

  2. 儲存並關閉檔案。

將精靈加入至 VSIX 套件

在 VSIX 專案中的 source.extension.vsixmanifest 檔案中,加入精靈專案的參考,以便部署與包含專案項目的 VSIX 套件。

若要將精靈加入至 VSIX 套件

  1. 在 [方案總管],開啟捷徑功能表在 CustomActionProjectItem 專案的 [source.extension.vsixmanifest]檔案,然後選取 [開啟] 開啟在資訊清單編輯器中開啟檔案。

  2. 在資訊清單編輯器中,選取 [資產] 索引標籤,然後選取 [新增] 按鈕。

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

  3. 在 [型別] 清單中,選取 [Microsoft.VisualStudio.Assembly]。

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

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

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

測試精靈

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

開始偵錯方案

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

  2. 在 ItemTemplateWizard 專案中,開啟 CustomActionWizard 程式碼檔案,然後將中斷點加入至 RunStarted 方法的第一行程式碼。

  3. 在功能表列上,選擇 [偵錯], [例外狀況]。

  4. 在 [例外狀況] 對話方塊中,確定已清除 Common Language Runtime 例外狀況。 的 [擲回] 和 [使用者未處理] 核取方塊,然後選取 [確定] 按鈕。

  5. 您可以選擇 F5 鍵開始偵錯,或者在功能表列上,選擇, [偵錯][啟動偵錯]。

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

若要在 Visual Studio 中測試精靈

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

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

  3. 在專案範本清單中,選取 [SharePoint 2010 專案],將專案命名為 CustomActionWizardTest,然後選取 [確定] 按鈕。

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

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

  6. 將新的項目- CustomItemWizardTest 對話方塊中,展開 [SharePoint] 節點,然後展開 [2010] 節點。

  7. 在專案項目清單中,選取 [自訂動作] 項目,然後選取 [新增] 按鈕。

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

  9. 繼續選擇 F5 鍵偵錯專案,或在功能表列上,選擇[偵錯], [繼續]。

    [SharePoint 自訂精靈] 隨即出現。

  10. 在 [位置] 下,選取 [清單中編輯] 選項按鈕。

  11. 在 [群組 ID]清單中,選取 [通訊]。

  12. 在 [標題]方塊中,輸入 SharePoint 開發人員中心。

  13. 在 [Description]方塊中,輸入 開啟 SharePoint 開發人員中心網站。

  14. 在 [URL]方塊中,輸入 https://msdn.microsoft.com/sharepoint/default.aspx,然後選取 [完成] 按鈕。

    isual Studio 加入名為的專案的 [CustomAction1]的項目並在編輯器中開啟 Elements.xml 檔案。確認 Elements.xml 包含您在精靈中指定的值。

若要測試 SharePoint 中的自訂動作

  1. 在 Visual Studio 的實驗執行個體中,選擇 F5 鍵,或在功能表列上,選擇, [偵錯][啟動偵錯]。

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

    注意事項注意事項

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

  2. 在 SharePoint 網站上的區域中,選取 [工作] 連結。

    [工作–所有工作] 頁面隨即出現。

  3. 在功能區上的 [清單工具] 索引標籤上,選取 [清單] 索引標籤,然後,在 [設定] 群組中,選取 [清單設定]。

    [列出設定] 頁面隨即出現。

  4. 在標題在網頁頂端附近 [通訊] 下,選取 [SharePoint 開發人員中心] 連結,確認瀏覽器開啟此網站 https://msdn.microsoft.com/sharepoint/default.aspx,然後關閉瀏覽器。

清理開發電腦

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

若要清理開發電腦

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

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

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

  3. 在出現的對話方塊中,選取 [] 按鈕確認您要解除安裝擴充功能,然後選取 [立即重新啟動] 按鈕完成解除安裝。

  4. 關閉 Visual Studio 的實驗執行個體和 CustomActionProjectItem 方案已開啟) 的兩個執行個體執行 Visual Studio。

請參閱

工作

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

HOW TO:搭配專案範本使用精靈

參考

Visual Studio 範本結構描述參考

IWizard

概念

定義自訂 SharePoint 專案項目類型

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

其他資源

根據中的自訂位置和 ID