カスタムの SharePoint プロジェクト項目を作成する場合は、プロジェクト項目の種類を定義します。 詳細については、「SharePoint プロジェクト項目の種類の定義」を参照してください。
プロジェクト項目の種類を定義するには
クラス ライブラリ プロジェクトを作成します。
次のアセンブリへの参照を追加します。
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
ISharePointProjectItemTypeProvider インターフェイスを実装するクラスを作成します。
クラスに次の属性を追加します。
System.ComponentModel.Composition.ExportAttribute. Visual Studio は、この属性を頼りに ISharePointProjectItemTypeProvider の実装を発見し、読み込みます。 この属性のコンストラクターには ISharePointProjectItemTypeProvider 型を渡します。
SharePointProjectItemTypeAttribute. プロジェクト項目の種類を定義する際は、新しいプロジェクト項目の文字列識別子をこの属性によって指定します。 プロジェクト項目の名前が重複しないよう、「<会社名>.<機能名>」という形式を使用することをお勧めします。
SharePointProjectItemIconAttribute. ソリューション エクスプローラーに表示されるプロジェクト項目のアイコンは、この属性によって指定されます。 この属性は省略可能です。この属性をクラスに適用しなかった場合は、既定のアイコンを使用してプロジェクト項目が表示されます。 この属性を設定する場合は、アセンブリに埋め込まれているアイコンまたはビットマップの完全修飾名を渡します。
InitializeType メソッドの実装では、projectItemTypeDefinition パラメーターのメンバーを使用してプロジェクト項目の種類の動作を定義します。 このパラメーターは、ISharePointProjectItemEvents インターフェイスおよび ISharePointProjectItemFileEvents インターフェイスに定義されているイベントにアクセスできる ISharePointProjectItemTypeDefinition オブジェクトです。 プロジェクト項目の種類の特定インスタンスにアクセスするには、ProjectItemAdded、ProjectItemInitialized などの ISharePointProjectItemEvents イベントを処理します。
例
次のコード例は、単純なプロジェクト項目の種類を定義する方法を示します。 このプロジェクト項目の種類では、ユーザーがこの種類のプロジェクト項目をプロジェクトに追加すると、[出力] ウィンドウと [エラー一覧] ウィンドウにメッセージを書き込みます。
Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint
Namespace Contoso.ExampleProjectItemType
<Export(GetType(ISharePointProjectItemTypeProvider))> _
<SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
<SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
Friend Class ExampleProjectItemType
Implements ISharePointProjectItemTypeProvider
Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
Implements ISharePointProjectItemTypeProvider.InitializeType
projectItemTypeDefinition.Name = "ExampleProjectItemType"
projectItemTypeDefinition.SupportedDeploymentScopes = _
SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All
AddHandler projectItemTypeDefinition.ProjectItemAdded, AddressOf ProjectItemAdded
End Sub
Private Sub ProjectItemAdded(ByVal Sender As Object, ByVal e As SharePointProjectItemEventArgs)
Dim Message As String = String.Format("An example project item named {0} was added to the {1} project.", _
e.ProjectItem.Name, e.ProjectItem.Project.Name)
e.ProjectItem.Project.ProjectService.Logger.WriteLine(Message, LogCategory.Message)
End Sub
End Class
End Namespace
using System;
using Microsoft.VisualStudio.SharePoint;
using System.ComponentModel.Composition;
namespace Contoso.ExampleProjectItemType
{
[Export(typeof(ISharePointProjectItemTypeProvider))]
[SharePointProjectItemType("Contoso.ExampleProjectItemType")]
[SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")]
internal class ExampleProjectItemType : ISharePointProjectItemTypeProvider
{
public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
{
projectItemTypeDefinition.Name = "ExampleProjectItemType";
projectItemTypeDefinition.SupportedDeploymentScopes =
SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;
projectItemTypeDefinition.ProjectItemAdded += projectItemTypeDefinition_ProjectItemAdded;
}
void projectItemTypeDefinition_ProjectItemAdded(object sender, SharePointProjectItemEventArgs e)
{
string message = String.Format("An example project item named {0} was added to the {1} project.",
e.ProjectItem.Name, e.ProjectItem.Project.Name);
e.ProjectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
}
}
}
この例では、SharePoint プロジェクト サービスを使用して、出力ウィンドウおよび [エラー一覧] ウィンドウにメッセージを書き込みます。 詳細については、「SharePoint プロジェクト サービスの使用」を参照してください。
コードのコンパイル
この例は、次のアセンブリへの参照を必要とします。
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
プロジェクト項目の配置
自分が定義したプロジェクト項目を他の開発者が使用できるようにするには、プロジェクト テンプレートまたはプロジェクト項目テンプレートを作成します。 詳細については、「SharePoint プロジェクト項目の項目テンプレートとプロジェクト テンプレートの作成」を参照してください。
プロジェクト項目を配置するには、同梱する必要のあるアセンブリやテンプレート、各種ファイルの Visual Studio 拡張機能 (VSIX) パッケージを作成します。 詳細については、「Visual Studio での SharePoint ツールの拡張機能の配置」を参照してください。
参照
処理手順
チュートリアル: 項目テンプレートに基づくカスタム動作プロジェクト項目の作成 (パート 1)
その他の技術情報
SharePoint プロジェクト項目の項目テンプレートとプロジェクト テンプレートの作成
チュートリアル: プロジェクト テンプレートに基づくサイト列プロジェクト項目の作成 (パート 1)