SharePoint プロジェクト項目のカスタム種類を定義し、Visual Studio でその種類を項目テンプレートと関連付けてから、テンプレート用のウィザードを用意することもできます。 ユーザーが独自のテンプレートを使用してプロジェクト項目の新しいインスタンスをプロジェクトに追加するときに、ウィザードを使用してユーザーから情報を収集できます。 収集した情報を使用して、プロジェクト項目を初期化できます。
このチュートリアルでは、「チュートリアル: 項目テンプレートに基づくカスタム動作プロジェクト項目の作成 (パート 1)」で示されているカスタム動作プロジェクト項目にウィザードを追加します。 ユーザーがカスタム動作プロジェクト項目を SharePoint プロジェクトに追加するときに、このウィザードはカスタム動作についての情報 (クリックされたときに移動するための場所と URL など) を収集し、Elements.xml ファイルの新しいプロジェクト項目にこの情報を追加します。
このチュートリアルでは、次のタスクについて説明します。
項目テンプレートに関連付けるカスタム SharePoint プロジェクト項目の種類に対するウィザードの作成。
Visual Studio 2010 の SharePoint プロジェクト項目用の組み込みウィザードと似たカスタム ウィザードの UI の定義。
置き換え可能なパラメーターを使用した、ウィザードで収集したデータでの SharePoint プロジェクト ファイルの初期化。
ウィザードのデバッグおよびテスト。
注意
このチュートリアル用の完全なプロジェクト、コード、およびその他のファイルを含むサンプルは、https://go.microsoft.com/fwlink/?LinkId=191369 からダウンロードできます。
必須コンポーネント
このチュートリアルを実行するには、先に「チュートリアル: 項目テンプレートに基づくカスタム動作プロジェクト項目の作成 (パート 1)」を完了して CustomActionProjectItem ソリューションを作成しておく必要があります。
また、このチュートリアルを実行するには、開発コンピューターに次のコンポーネントが必要です。
サポート対象エディションの Microsoft Windows、SharePoint、および Visual Studio。 詳細については、「SharePoint ソリューションの開発要件」を参照してください。
Visual Studio 2010 SDK。 このチュートリアルでは、SDK の VSIX プロジェクト テンプレートを使用して、プロジェクト項目を配置するための VSIX パッケージを作成します。 詳細については、「Visual Studio の SharePoint ツールの拡張」を参照してください。
次の概念に関する知識があると役に立ちますが、チュートリアルを実行するうえで必須というわけではありません。
Visual Studio のプロジェクトおよび項目テンプレート用のウィザード。 詳細については、「方法 : プロジェクト テンプレートを組み合わせたウィザードを使用する」および IWizard インターフェイスを参照してください。
SharePoint のカスタム動作。 詳細については、「Custom Action (カスタム動作)」を参照してください。
ウィザード プロジェクトの作成
このチュートリアルを行うには、「チュートリアル: 項目テンプレートに基づくカスタム動作プロジェクト項目の作成 (パート 1)」で作成した CustomActionProjectItem ソリューションに新しいプロジェクトを追加する必要があります。 このプロジェクトで、IWizard インターフェイスを実装し、ウィザードの UI を定義します。
ウィザード プロジェクトを作成するには
CustomActionProjectItem ソリューションを Visual Studio で開きます。
ソリューション エクスプローラーでソリューション ノードを右クリックし、[追加] をクリックして、[新しいプロジェクト] をクリックします。
注意
Visual Basic プロジェクトでソリューション エクスプローラーにソリューション ノードが表示されるのは、[全般] ([オプション] ダイアログ ボックス - [プロジェクトおよびソリューション])の [常にソリューションを表示] チェック ボックスがオンになっている場合だけです。
[新しいプロジェクト] ダイアログ ボックスで、[Visual C#] ノードまたは [Visual Basic] ノードを展開し、[Windows] ノードをクリックします。
[新しいプロジェクト] ダイアログ ボックスの上部のコンボ ボックスで、[.NET Framework 4] が選択されていることを確認します。
[WPF ユーザー コントロール ライブラリ] プロジェクト テンプレートを選択します。
[名前] ボックスに「ItemTemplateWizard」と入力します。
[OK] をクリックします。
Visual Studio のソリューションに ItemTemplateWizard プロジェクトが追加されます。
プロジェクトから UserControl1 アイテムを削除します。
ウィザード プロジェクトの構成
ウィザードを作成する前に、WPF ウィンドウ、コード ファイル、およびアセンブリ参照をプロジェクトに追加する必要があります。
ウィザード プロジェクトを構成するには
ソリューション エクスプローラーで [ItemTemplateWizard] プロジェクト ノードを右クリックし、[プロパティ] をクリックします。
[プロジェクト デザイナー] で、ターゲット フレームワークを .NET Framework 4 Client Profile から .NET Framework 4 に変更します。 これは、Visual C# プロジェクトの場合は [アプリケーション] タブで、Visual Basic プロジェクトの場合は [コンパイル] タブで実行できます。 詳細については、「方法: 特定の .NET Framework のバージョンまたはプロファイルを対象として指定する」を参照してください。
注意
既定では、.NET Framework 4 をターゲットとする新しいプロジェクトを作成すると、プロジェクトで Client Profile がターゲットになります。 このチュートリアルでは、完全な .NET Framework 4 が必要です。
ItemTemplateWizard プロジェクトで、新しい [ウィンドウ (WPF)] アイテムをプロジェクトに追加します。 新しいアイテムに「WizardWindow」という名前を設定します。
次の名前の 2 つのコード ファイルを追加します。
CustomActionWizard
Strings
[プロジェクト] メニューの [参照の追加] をクリックします。
[.NET] タブで、Ctrl キーを押しながら次のアセンブリをクリックし、[OK] をクリックします。
EnvDTE
Microsoft.VisualStudio.Shell.10.0
Microsoft.VisualStudio.TemplateWizardInterface
ソリューション エクスプローラーで、ItemTemplateWizard プロジェクトの [参照設定] フォルダーの下の [EnvDTE] をクリックします。
注意
Visual Basic プロジェクトでは、[参照設定] フォルダーが表示されるのは、[全般] ([オプション] ダイアログ ボックス - [プロジェクトおよびソリューション]) で [常にソリューションを表示] チェック ボックスがオンになっている場合のみです。
[プロパティ] ウィンドウで、[相互運用型の埋め込み] プロパティを False に変更します。
カスタム動作の既定の場所と ID 文字列の定義
すべてのカスタム動作には場所と ID があり、Elements.xml ファイルの CustomAction 要素の GroupID 属性と Location 属性で指定されています。 この手順では、ItemTemplateWizard プロジェクトでこれらの属性に有効な文字列のいくつかを定義します。 このチュートリアルが完了した後、ユーザーがウィザードで場所と ID を選択すると、カスタム動作プロジェクト項目の Elements.xml ファイルに、これらの文字列が書き込まれます。
簡潔にするため、このサンプルでは使用可能な既定の場所と ID のサブセットのみをサポートしています。 完全な一覧については、「Default Custom Action Locations and IDs (カスタム動作の既定の場所と ID)」を参照してください。
既定の場所と ID の文字列を定義するには
ItemTemplateWizard プロジェクトで、Strings コード ファイルを開きます。
このファイル内のコードを次のコードに置き換えます。
' 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 の作成
ウィザードの UI を定義する XAML を追加し、ウィザードの一部のコントロールを ID 文字列にバインドするためのコードを追加します。 作成するウィザードは、Visual Studio 2010 の SharePoint プロジェクト用の組み込みウィザードに似ています。
ウィザードの UI を作成するには
ItemTemplateWizard プロジェクトで WizardWindow.xaml ファイルをダブルクリックして、ウィンドウをデザイナーで開きます。
デザイナーの 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 基本クラスから派生します。 カスタムの WPF ダイアログ ボックスを Visual Studio に追加する場合は、ダイアログ ボックスをこのクラスから派生し、スタイルを他の Visual Studio ダイアログ ボックスと一貫させ、発生する可能性のあるモーダル ダイアログの問題を回避することをお勧めします。 詳細については、「How to: Create and Manage Dialog Boxes」を参照してください。
Visual Basic プロジェクトを開発している場合は、Window 要素の x:Class 属性の WizardWindow クラス名から ItemTemplateWizard 名前空間を削除します。 これは XAML の 1 行目にあります。 変更後の 1 行目は次のようになります。
<Window x:Class="WizardWindow"
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); } } }
チェックポイント
この段階で、ウィザードに必要なすべてのコードがプロジェクトに揃ったことになります。 エラーが発生することなくプロジェクトをコンパイルできるかどうか、プロジェクトをビルドして確認してください。
プロジェクトをビルドするには
- [ビルド] メニューの [ソリューションのビルド] をクリックします。
ウィザードと項目テンプレートの関連付け
ウィザードの実装が済んだので、ウィザードとカスタム動作項目テンプレートを関連付ける必要があります。 これを行うために必要となる主要な手順は次の 3 つです。
ウィザード アセンブリに厳密な名前で署名します。
ウィザード アセンブリの公開キー トークンを取得します。
カスタム動作項目テンプレートの .vstemplate ファイルに、ウィザード アセンブリへの参照を追加します。
ウィザード アセンブリに厳密な名前で署名するには
ソリューション エクスプローラーで [ItemTemplateWizard] プロジェクト ノードを右クリックし、[プロパティ] をクリックします。
[署名] タブをクリックします。
[アセンブリの署名] チェック ボックスをオンにします。
[厳密な名前のキー ファイルを選択してください] ボックスの一覧の [<新規作成...>] を選択します。
[厳密な名前キーの作成] ダイアログ ボックスで、新しい名前を入力し、[キー ファイルをパスワードで保護する] チェック ボックスをオフにします。
[OK] をクリックします。
[ビルド] メニューの [ソリューションのビルド] をクリックします。
ウィザード アセンブリの公開キー トークンを取得するには
Visual Studio コマンド プロンプト ウィンドウを開きます。
次のコマンドを実行します。 path to wizard assembly は、開発コンピューター上で ItemTemplateWizard プロジェクト用にビルドされた ItemTemplateWizard.dll アセンブリへの完全パスに置き換えます。
sn.exe -T path to wizard assembly
ItemTemplateWizard.dll アセンブリに対する公開キー トークンが Visual Studio コマンド プロンプト ウィンドウに記述されます。
Visual Studio コマンド プロンプト ウィンドウは開いたままにします。 次の手順の間に、公開キー トークンが必要になります。
.vstemplate ファイルにウィザード アセンブリへの参照を追加するには
ソリューション エクスプローラーで、[ItemTemplate] プロジェクト ノードを展開し、ItemTemplate.vstemplate ファイルを開きます。
ファイルの末尾の近くで、次の WizardExtension 要素を </TemplateContent> タグと </VSTemplate> タグの間に追加します。 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 テンプレート)」を参照してください。
ファイルを保存して閉じます。
項目テンプレートの Elements.xml ファイルへの置き換え可能パラメーターの追加
複数の置き換え可能パラメーターを、ItemTemplate プロジェクトの Elements.xml ファイルに追加します。 これらのパラメーターは、前に定義した CustomActionWizard クラスの PopulateReplacementDictionary メソッドで初期化されます。 ユーザーがカスタム動作プロジェクト項目をプロジェクトに追加すると、Visual Studio によって自動的に、新しいプロジェクト項目の Elements.xml ファイル内のこれらのパラメーターが、ウィザードでユーザーが指定した値に置き換えられます。
置き換え可能パラメーターはトークンであり、先頭と末尾にはドル記号 ($) が付いています。 独自の置き換え可能パラメーターを定義するだけでなく、SharePoint プロジェクト システムによって定義されて初期化される組み込みパラメーターを使用することもできます。 詳細については、「置き換え可能パラメーター」を参照してください。
置き換え可能パラメーターを Elements.xml ファイルに追加するには
ItemTemplate プロジェクトの Elements.xml ファイルを開きます。
このファイルの内容を次の 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 の各属性の値が置き換え可能パラメーターに変更されます。
ファイルを保存して閉じます。
VSIX パッケージへのウィザードの追加
プロジェクト項目が含まれる VSIX パッケージと共にウィザードを配置するには、ウィザード プロジェクトへの参照を VSIX プロジェクトの source.extension.vsixmanifest ファイルに追加します。
VSIX パッケージにウィザードを追加するには
ソリューション エクスプローラーで、CustomActionProjectItem プロジェクトの source.extension.vsixmanifest ファイルをダブルクリックします。
Visual Studio によってマニフェスト エディターでファイルが開きます。
エディターの [コンテンツ] セクションで、[コンテンツの追加] をクリックします。
[コンテンツの追加] ダイアログ ボックスで、[コンテンツ タイプの選択] リスト ボックスの [テンプレート ウィザード] をクリックします。
[ソースの選択] の下にある [プロジェクト] をクリックし、その横のボックスの [ItemTemplateWizard] を選択します。
[OK] をクリックします。
[ビルド] メニューの [ソリューションのビルド] をクリックします。 エラーが発生することなくソリューションがコンパイルされることを確認します。
ウィザードのテスト
これで、ウィザードをテストする準備ができました。 まず、Visual Studio の実験用インスタンスで CustomActionProjectItem ソリューションのデバッグを開始します。 次に、Visual Studio の実験用インスタンスで、SharePoint プロジェクトのカスタム動作プロジェクト項目のウィザードをテストします。 最後に、SharePoint プロジェクトをビルドして実行し、カスタム動作が正常に機能することを確認します。
ソリューションのデバッグを開始するには
管理者特権で Visual Studio を再起動し、CustomActionProjectItem ソリューションを開きます。
ItemTemplateWizard プロジェクトで、CustomActionWizard コード ファイルを開き、RunStarted メソッド内のコードの 1 行目にブレークポイントを追加します。
[デバッグ] メニューの [例外] をクリックします。
[例外] ダイアログ ボックスで、[Common Language Runtime Exceptions] の [スローされるとき] チェック ボックスと [ユーザーにハンドルされていないとき] チェック ボックスがオフになっていることを確認します。
[OK] をクリックします。
F5 キーを押してデバッグを開始します。
Visual Studio によって、拡張機能が %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Custom Action Project Item\1.0 にインストールされ、Visual Studio の実験用インスタンスが開始されます。 このインスタンスの Visual Studio でプロジェクト項目をテストします。
Visual Studio でウィザードをテストするには
Visual Studio の実験用インスタンスで、[ファイル] メニューの [新規] をポイントし、[プロジェクト] をクリックします。
(項目テンプレートがサポートする言語に応じて) [Visual C#] または [Visual Basic] を展開し、[SharePoint] を展開して、[2010] をクリックします。
プロジェクト テンプレートの一覧で [空のプロジェクト] をクリックします。
[名前] ボックスに「CustomActionWizardTest」と入力します。
[OK] をクリックします。
SharePoint カスタマイズ ウィザードで、デバッグ用のサイトの URL を入力し、[完了] をクリックします。
ソリューション エクスプローラーで、プロジェクト ノードを右クリックし、[追加] をポイントして、[新しい項目] をクリックします。
[新しい項目の追加] ダイアログ ボックスで、[SharePoint] ノードの下の [2010] ノードをクリックします。
プロジェクト項目の一覧で、[カスタム動作] をクリックし、[追加] をクリックします。
Visual Studio のもう一方のインスタンスで、先ほど RunStarted メソッドに設定したブレークポイントで、コードが停止していることを確認します。 F5 キーを押して、プロジェクトのデバッグを続行します。
ウィザードで次の選択を行います。
[Location] で、[List Edit] をクリックします。
[Group ID] で、[Communications] をクリックします。
[Title] に、「SharePoint Developer Center」と入力します。
[Description] に、「Opens the SharePoint Developer Center Web site」と入力します。
[URL] に、「https://msdn.microsoft.com/sharepoint/default.aspx」と入力します。
[完了] をクリックします。
CustomAction1 という名前の新しい項目がプロジェクトに追加され、エディターに Elements.xml ファイルが表示されます。 Elements.xml にウィザードで指定した値が含まれることを確認します。
SharePoint のカスタム動作をテストするには
Visual Studio の実験用インスタンスで、F5 キーを押します。 カスタム動作がパッケージ化され、プロジェクトの [サイト URL] プロパティで指定された SharePoint サイトに配置されます。 Web ブラウザーには、このサイトの既定のページが表示されます。
注意
[スクリプト デバッグが無効] ダイアログ ボックスが表示された場合は、[はい] をクリックしてプロジェクトをデバッグします。
クイック起動領域で [タスク] をクリックします。
リボンの [リスト] タブをクリックします。
[設定] グループで、[リストの設定] をクリックします。
ページの上の方にある [Communications] という見出しの下で、[SharePoint Developer Center] というリンクが表示されることを確認します。
[SharePoint Services デベロッパー センター] リンクをクリックします。 ブラウザーに https://msdn.microsoft.com/sharepoint/default.aspx の Web サイトが表示されることを確認します。
Web ブラウザーを閉じます。
開発コンピューターのクリーンアップ
プロジェクト項目のテストが終わったら、プロジェクト項目テンプレートを Visual Studio の実験用インスタンスから削除します。
開発コンピューターをクリーンアップするには
Visual Studio の実験用インスタンスの [ツール] メニューで、[拡張機能マネージャー] をクリックします。
[拡張機能マネージャー] ダイアログ ボックスが表示されます。
拡張機能の一覧で、[Custom Action Project Item] をクリックし、[アンインストール] をクリックします。
確認のダイアログ ボックスが表示されたら、[はい] をクリックして、拡張機能をアンインストールします。
[今すぐ再起動] をクリックするとアンインストールは完了です。
Visual Studio の両方のインスタンス (CustomActionProjectItem ソリューションを開いた Visual Studio の実験用インスタンスとインスタンス) を閉じます。
参照
処理手順
チュートリアル: 項目テンプレートに基づくカスタム動作プロジェクト項目の作成 (パート 1)
方法 : プロジェクト テンプレートを組み合わせたウィザードを使用する