共用方式為


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

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

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

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

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

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

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

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

注意事項注意事項

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

必要條件

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

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

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

建立精靈專案

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

若要建立精靈專案

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

  2. 在 [方案總管] 中,以滑鼠右鍵按一下方案節點,按一下 [加入],再按一下 [新增專案]。

    注意事項注意事項

    在 Visual Basic 專案中,方案節點只有在已選取選項對話方塊、專案和方案、一般中的 [永遠顯示方案] 核取方塊時,才會出現在 [方案總管] 中。

  3. 在 [新增專案] 對話方塊中展開 [Visual C#] 或 [Visual Basic] 節點,然後按一下 [Windows] 節點。

  4. 在 [新增專案] 對話方塊頂端的下拉式方塊中,確定已選取 [.NET Framework 4]。

  5. 選取 [WPF 使用者控制項程式庫] 專案範本。

  6. 在 [名稱] 方塊中,輸入 ItemTemplateWizard。

  7. 按一下 [確定]。

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

  8. 從專案刪除 UserControl1 項目。

設定精靈專案

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

若要設定精靈專案

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [ItemTemplateWizard] 專案節點,然後按一下 [屬性]。

  2. 在 [專案設計工具] 中,將目標 Framework 項目從 .NET Framework 4 Client Profile 變更為 .NET Framework 4。 您可以分別針對 Visual C# 專案和 Visual Basic 專案,在 [應用程式] 索引標籤和 [編譯] 索引標籤上執行這個動作。 如需詳細資訊,請參閱 HOW TO:以特定的 .NET Framework 版本或設定檔為目標

    注意事項注意事項

    根據預設,建立以 .NET Framework 4 為目標的新專案時,這個專案會以用戶端設定檔為目標。 本逐步解說需要完整的 .NET Framework 4。

  3. 在 ItemTemplateWizard 專案中,將新的 [視窗 (WPF)] 項目加入至專案中。 將新的項目命名為 WizardWindow

  4. 加入兩個分別具有下列名稱的程式碼檔案:

    • CustomActionWizard

    • Strings

  5. 在 [專案] 功能表上,按一下 [加入參考]。

  6. 在 [.NET] 索引標籤上,按住 CTRL,然後按一下下列組件,再按一下 [確定]:

    • EnvDTE

    • Microsoft.VisualStudio.Shell.10.0

    • Microsoft.VisualStudio.TemplateWizardInterface

  7. 在 [方案總管] 中,按一下 ItemTemplateWizard 專案的 [參考] 資料夾底下的 [EnvDTE]。

    注意事項注意事項

    在 Visual Basic 專案中,只有在已選取選項對話方塊、專案和方案、一般中的 [永遠顯示方案] 核取方塊時,才會出現 [參考] 資料夾。

  8. 在 [屬性] 視窗中,將 [內嵌 Interop 型別] 屬性變更為 [False]。

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

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

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

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

  1. 在 ItemTemplateWizard 專案中,開啟 Strings 程式碼檔案。

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

    ' 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 字串。 所建立的精靈類似於 Visual Studio 2010 中 SharePoint 專案的內建精靈。

若要建立精靈 UI

  1. 在 ItemTemplateWizard 專案中,按兩下 WizardWindow.xaml 檔案將視窗在設計工具中開啟。

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

    注意事項注意事項

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

    <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.10.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 基底類別。 在 Visual Studio 中加入自訂 WPF 對話方塊時,建議您從這個類別衍生對話方塊,使其樣式與其他 Visual Studio 對話方塊一致,以避免可能發生的強制回應對話方塊的問題。 如需詳細資訊,請參閱 How to: Create and Manage Dialog Boxes

  3. 如果您正在開發 Visual Basic 專案,請在 Window 項目的 x:Class 屬性中,將 ItemTemplateWizard 命名空間從 WizardWindow 類別名稱移除。 這位於 XAML 的第一行。 當您完成之後,第一行應如下所示。

    <Window x:Class="WizardWindow"
    
  4. 開啟 WizardWindow.xaml 檔案的程式碼後置檔案。

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

    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 介面來定義。

若要實作精靈

  1. 在 ItemTemplateWizard 專案中,開啟 CustomActionWizard 程式碼檔案。

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

    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. 選取 [簽署組件] 核取方塊。

  4. 在 [選擇強式名稱金鑰檔] 下拉式清單中,選取 [<新增>]。

  5. 在 [建立強式名稱金鑰] 對話方塊中,輸入名稱並清除 [以密碼保護我的金鑰檔] 核取方塊。

  6. 按一下 [確定]。

  7. 在 [建置] 功能表上,選取 [建置方案]。

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

  1. 開啟 [Visual Studio 命令提示字元] 視窗。

  2. 執行下列命令。 將 <精靈組件路徑> 取代為 ItemTemplateWizard 專案的內建 ItemTemplateWizard.dll 組件位於您開發電腦上的完整路徑。

    sn.exe -T path to wizard assembly
    

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

  3. 讓 [Visual Studio 命令提示字元] 視窗保持開啟。 在下一個程序中,您將會需要這個公開金鑰語彙基元。

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

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

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

    <WizardExtension>
      <Assembly>ItemTemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=your token</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 專案中的 Elements.xml 檔案。

  2. 以下列 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 屬性的值變更為可取代的參數。

  3. 儲存並關閉檔案。

將精靈加入至 VSIX 套件

若要使用包含專案項目的 VSIX 套件來部署精靈,請將精靈專案的參考加入至 VSIX 專案中的 source.extension.vsixmanifest 檔案。

若要將精靈加入至 VSIX 套件

  1. 在 [方案總管] 中,按兩下 [CustomActionProjectItem] 專案底下的 source.extension.vsixmanifest 檔案。

    Visual Studio 會在資訊清單編輯器中開啟檔案。

  2. 在編輯器的 [內容] 區段中,按一下 [加入內容] 按鈕。

  3. 在 [加入內容] 對話方塊的 [選取內容類型] 清單方塊中,選取 [範本精靈]。

  4. 按一下 [選取來源] 底下的 [專案] 選項按鈕,並選取其旁邊清單方塊中的 [ItemTemplateWizard]。

  5. 按一下 [確定]。

  6. 在 [建置] 功能表上,按一下 [建置方案]。 請確定方案編譯作業未發生錯誤。

測試精靈

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

若要開始偵錯方案

  1. 以系統管理員權限重新啟動 Visual Studio 並且開啟 [CustomActionProjectItem] 專案。

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

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

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

  5. 按一下 [確定]。

  6. 按 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 的實驗執行個體中,指向 [檔案] 功能表上的 [新增],然後按一下 [專案]。

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

  3. 在專案範本清單中,按一下 [空專案]。

  4. 在 [名稱] 方塊中,輸入 CustomActionWizardTest。

  5. 按一下 [確定]。

  6. 在 [SharePoint 自訂精靈] 中,輸入要用於偵錯的網站 URL,然後按一下 [完成]。

  7. 在 [方案總管] 中,以滑鼠右鍵按一下專案節點,指向 [加入],然後按一下 [新增項目]。

  8. 在 [加入新項目] 對話方塊中,按一下 [SharePoint] 節點底下的 [2010] 節點。

  9. 在專案項目的清單中,按一下 [自訂動作],然後按一下 [加入]。

  10. 確認另一個 Visual Studio 執行個體中的程式碼在您之前於 RunStarted 方法中設定的中斷點停止。 按 F5 繼續偵錯專案。

  11. 在精靈中進行下列選取:

    • 按一下 [Location] 底下的 [List Edit]。

    • 針對 [群組 ID],按一下 [Communications]。

    • 對 [Title],輸入 SharePoint Developer Center。

    • 對 [Description],輸入 Opens the SharePoint Developer Center Web site。

    • 對 [URL],輸入 https://msdn.microsoft.com/sharepoint/default.aspx。

  12. 按一下 [完成]。

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

若要測試 SharePoint 中的自訂動作

  1. 在 Visual Studio 的實驗執行個體中按 F5。 自訂動作將會封裝並部署至專案的 [網站 URL] 屬性指定的 SharePoint 網站。 Web 瀏覽器會開啟此網站的預設頁面。

    注意事項注意事項

    如果顯示 [已停用指令碼偵錯] 對話方塊,請按一下 [] 繼續進行專案偵錯。

  2. 按一下 [快速啟動] 區域中的 [工作]。

  3. 按一下 [功能區] 上的 [清單] 索引標籤。

  4. 按一下 [設定] 群組中的 [清單設定]。

  5. 在接近頁面頂端的 [通訊] 標題底下,確認帶有「SharePoint 開發人員中心」文字的連結出現。

  6. 按一下 [SharePoint 開發人員中心] 連結。 確認瀏覽器開啟 https://msdn.microsoft.com/sharepoint/default.aspx 這個網站。

  7. 關閉 Web 瀏覽器。

清理開發電腦

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

若要清理開發電腦

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

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

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

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

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

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

請參閱

工作

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

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

參考

Visual Studio 範本結構描述參考

IWizard

其他資源

定義自訂 SharePoint 專案項目類型

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

預設的自訂動作位置和 ID