コンテンツ サービス オブジェクトを作成する
最終更新日: 2009年9月30日
適用対象: SharePoint Foundation 2010
この記事の内容
ContentService クラス ファイル
ContentLocationCollection クラス ファイル
ContentLocation クラス ファイル
カスタム アクションと ASPX ページの追加
このウォークスルーでは、SharePoint Foundation 展開内のサイト コンテンツを管理するために、カスタム サービスのロジックを定義する方法について説明します。例では、Microsoft.SharePoint.Administration 名前空間内のクラスから派生するクラスを作成して、データベース内にカスタムの管理設定を維持する方法を示します。例に含まれるクラス ファイルは、場所のリストに関するメタデータを含むデータ構造を実装します。各コンテンツの場所は、保持する必要がある URL を指定しています。
例には、次のクラスに関するコード ファイルの作成が含まれています。
ContentServiceSPService クラスから派生し、展開内のコンテンツの場所を管理するロジックを提供する、トップレベルのオブジェクトを定義します。このクラスには、ContentService オブジェクトを初期化するためのコンストラクター、およびコンテンツ サービスとそのコンテンツの場所を取得するためのメンバーが含まれます。
ContentLocationCollectionSPPerisistedChildCollection クラスから継承されたコレクション クラスを定義し、コンテンツの場所を含むオブジェクトを提供します。このクラスにはコンストラクターと Add メソッドが含まれます。
ContentLocationSPPersistedObject から継承され、コンテンツの場所を定義します。このクラスには、カスタム プロパティ設定のためのコンストラクターとメンバーが含まれます。
以前のコード ファイルでのアセンブリの作成に加え、この例では、管理者がサービスにアクセスできるよう、カスタム アクションの機能を使って [サーバー構成の管理] ページにリンクを追加する方法を示します。またこの例では、カスタムの .aspx ページを使って、コンテンツ サービスとそのコンテンツの場所の項目を管理するのに必要なユーザー インターフェイスを提供する方法も示します。Microsoft Visual Studio 2005 での SharePoint Foundation プロジェクトの作成については、「Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio」を参照してください。
ContentService クラス ファイル
次のコード例では、FormatType 列挙体、およびコンストラクターと次のメンバーを含むトップレベルの ContentService クラスを定義します。
Local 現在のコンテンツの場所のサービスを取得するための静的プロパティ。
Locations コンテンツの場所のコレクションにアクセスするための静的プロパティ。
GetAllContentLocations サービスで提供されるすべてのコンテンツの場所を返すためのメソッド。
Format コンテンツ サービスの形式タイプを取得または設定するためのプロパティ。
次の例に従って、以降に記載する各コード ファイルの先頭に、Microsoft.SharePoint.Administration 名前空間をインポートするためのディレクティブを追加してください。Persisted 属性を使用して、データベース内にプロパティの設定を維持します。例で使用しているオブジェクトを含むカスタム名前空間の名前は、MS.Samples.SharePoint.ContentManager です。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint.Administration
Namespace MS.Samples.SharePoint.ContentManager
Public Enum FormatType
HTML
XML
End Enum 'ContentLocationType
<System.Runtime.InteropServices.Guid("BB69A6EB-3230-43ca-B4F5-752EAC39C74A")> _
Public Class ContentService
Inherits SPService
<Persisted()> _
Private formatType As FormatType
Private Shared locations As ContentLocationCollection
Private Shared local As ContentService = Nothing
' A static property that retrieves the content service.
Public Shared ReadOnly Property Local() As ContentService
Get
If ContentService.local Is Nothing Then
ContentService.local = _
SPFarm.Local.Services.GetValue < ContentService > "ContentService"
End If
Return ContentService.local
End Get
End Property
' A static property for accessing the location collection.
Public Shared ReadOnly Property Locations() As ContentLocationCollection
Get
If Me.locations Is Nothing Then
Me.locations = New ContentLocationCollection(Me)
End If
Return Me.locations
End Get
End Property
' An empty public constructor required for serialization.
Public Sub New()
End Sub 'New
Public Sub New(farm As SPFarm)
MyBase.New("ContentService", farm)
' Add code to initialize as needed.
End Sub 'New
' A method to retrieve the content location collection.
Public Function GetAllContentLocations() As ContentLocationCollection
Dim service As ContentService = ContentService.Local
If service Is Nothing Then
Return Nothing
End If
Return service.Locations
End Function 'GetAllContentLocations
Public Property Format () As FormatType
Get
Return Me.formatType
End Get
Set
Me.formatType = value
End Set
End Property
End Class 'ContentService
End Namespace 'MS.Samples.SharePoint.ContentManager
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
namespace MS.Samples.SharePoint.ContentManager
{
public enum FormatType
{
HTML,
XML
}
[System.Runtime.InteropServices.Guid("BB69A6EB-3230-43ca-B4F5-752EAC39C74A")]
public class ContentService : SPService
{
[Persisted]
private FormatType formatType;
private static ContentLocationCollection locations;
private static ContentService local = null;
/* A static property that retrieves the content service. */
public static ContentService Local
{
get
{
if (ContentService.local == null)
{
ContentService.local =
SPFarm.Local.Services.GetValue<ContentService>("ContentService");
}
return ContentService.local;
}
}
/* A static property for accessing the location collection. */
public static ContentLocationCollection Locations
{
get
{
if (this.locations == null)
{
this.locations = new ContentLocationCollection(this);
}
return this.locations;
}
}
/* An empty public constructor required for serialization. */
public ContentService()
{;}
public ContentService(SPFarm farm)
: base("ContentService", farm)
{/* Add code to initialize as needed. */}
/* A method to retrieve the content location collection. */
public ContentLocationCollection GetAllContentLocations()
{
ContentService service = ContentService.Local;
if (service == null)
{
return null;
}
return service.Locations;
}
public FormatType Format
{
get
{
return this.formatType;
}
set
{
this.formatType = value;
}
}
}
}
以下の例のように、カスタムの Provision メソッドを定義し、ContentService オブジェクトをデータベースに追加します。このメソッドは前の ContentService クラスに含めることもできますし、または別個に含めてモジュール性を高め、サービスのインストール用に作成する機能のインストーラー、コマンド ラインの操作など、さまざまなコンテキストから呼び出せるようにすることもできます。Provision メソッドで ContentService オブジェクトをデータベースに追加する場合、アセンブリやクラスを指定する必要はありません。
注意
定義するカスタムの Provision メソッドは、基本 SPPersistedObject クラスの Provision メソッドではありません。
Public Sub Provision()
' Add the ContentService administration object to the database.
Dim contentService As ContentService = ContentService.Local
If contentService Is Nothing Then
contentService = New ContentService(SPFarm.Local)
contentService.Update()
End If
End Sub
public void Provision()
{
/* Add the ContentService administration object to the database. */
ContentService contentService = ContentService.Local;
if (contentService == null)
{
contentService = new ContentService(SPFarm.Local);
contentService.Update();
}
}
ContentLocationCollection クラス ファイル
次の例は、ContentLocationCollection クラスを定義します。このクラスには、コンストラクター、およびコレクションに新しいコンテンツの場所を追加するための Add メソッドがあります。
Public Class ContentLocationCollection
Public Sub New(parent As SPPersistedObject)
MyBase.New(parent)
End Sub
Public Sub Add(url As String)
Dim location As New ContentLocation(String.Empty, Me.Parent)
location.Url = url
location.Update()
End Sub
End Class
public class ContentLocationCollection : SPPersistedChildCollection<ContentLocation>
{
public ContentLocationCollection(SPPersistedObject parent) : base(parent)
{;}
public void Add(String url)
{
ContentLocation location = new ContentLocation(String.Empty, this.Parent);
location.Url = url;
location.Update();
}
}
ContentLocation クラス ファイル
次の例は、コンテンツの場所のタイプを指定する列挙型の ContentLocationType と、コンテンツの場所のプロパティ (表示名、タイプ、URL、および出力パス) を定義するクラス ContentLocation を定義します。Persisted 属性を指定すると、データベース内にプロパティ設定が維持されます。
ContentLocation クラスには次のメンバーが含まれます。
DisplayName 場所の表示名を取得するためのプロパティ。
Url コンテンツの場所の URL を取得または設定するためのプロパティ。
LocationType 場所のタイプを取得または設定するためのプロパティ。
ContentOutputPath 出力パスを取得または設定するためのプロパティ。
Public Enum ContentLocationType
Web
List
End Enum 'ContentLocationType
Public Class ContentLocation
Inherits SPPersistedObject
<Persisted()> _
Private locationType As ContentLocationType
<Persisted()> _
Private contentOutputPath As String
<Persisted()> _
Private url As String
Public Sub New()
End Sub 'New
Public Sub New(name As String, parent As SPPersistedObject)
MyBase.New(name, parent)
End Sub 'New
Public Overrides ReadOnly Property DisplayName() As String
Get
Return Me.url
End Get
End Property
Public Property Url() As String
Get
Return Me.url
End Get
Set
If Me.url = value Then
Return
End If
Me.url = value
' The Name property must be unique among multiple children in a
' collection. Use the URL to ensure uniqueness.
Me.Name = Me.url
End Set
End Property
Public Property LocationType() As ContentLocationType
Get
Return Me.locationType
End Get
Set
Me.locationType = value
End Set
End Property
Public Property ContentOutputPath() As String
Get
Return Me.contentOutputPath
End Get
Set
Me.contentOutputPath = value
End Set
End Property
End Class 'ContentLocation
public enum ContentLocationType
{
Web,
List
}
public class ContentLocation : SPPersistedObject
{
[Persisted]
private ContentLocationType locationType;
[Persisted]
private String contentOutputPath;
[Persisted]
private String url;
public ContentLocation()
{}
public ContentLocation(string name, SPPersistedObject parent)
: base(name, parent)
{;}
public override string DisplayName
{
get
{
return this.url;
}
}
public String Url
{
get { return this.url; }
set
{
if (this.url == value)
{
return;
}
this.url = value;
/* The Name property must be unique among multiple children in a collection. Use the URL to ensure uniqueness. */
this.Name = this.url;
}
}
public ContentLocationType LocationType
{
get { return this.locationType; }
set { this.locationType = value; }
}
public String ContentOutputPath
{
get { return this.contentOutputPath; }
set { this.contentOutputPath = value; }
}
}
カスタム アクションと ASPX ページの追加
.aspx ページを \Admin フォルダーに追加し、その背後のコードをアセンブリに定義します。管理用 .aspx ページについての詳細は、「サーバーの全体管理ページ」を参照してください。機能を使用して、[サーバーの全体管理] ページにカスタム アクションを追加します。 カスタム アクションの追加についての詳細は、「[方法] カスタム アクションを使用してユーザー インターフェイスを変更する」を参照してください。
次の Elements.xml ファイルは、Content export カスタム アクションを [サーバー構成の管理] ページの [グローバル構成] セクションに追加します。このアクションは、コンテンツの場所を管理する contentman.aspx ページにリンクしています。
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="ContentManagement"
GroupId="GlobalConfiguration"
Location="Microsoft.SharePoint.Administration.Operations"
Sequence="31"
Title="Content export">
<UrlAction Url="/_admin/contentman.aspx" />
</CustomAction>
</Elements>
カスタム アクションが指している contentman.aspx ファイルには、コンテンツの場所の項目を作成、表示、編集するためのフォーム ページへのリンクを含めることができます。たとえば、[新しいアイテム] ページの背後で実行されるコードには、ページ上の [urlImportExport] ボックスに入力された URL の場所項目を追加し、contentman.aspx ページにリダイレクトする、次のようなロジックを含めることができます。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint.WebControls
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace MS.Samples.SharePoint.ContentManager
Public Class NewContentImportExportPage
Inherits Page
Protected urlImportExport As InputFormTextBox
Protected Sub SaveNewImportExport(sender As Object, eventArgs As EventArgs)
Dim service As ContentService = ContentService.Local
service.Locations.Add(Me.urlImportExport.Text)
Response.Redirect("/_admin/contentman.aspx", True)
End Sub 'SaveNewImportExport
End Class 'NewContentImportExportPage
End Namespace 'MS.Samples.SharePoint.ContentManager
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MS.Samples.SharePoint.ContentManager
{
public class NewContentImportExportPage : Page
{
protected InputFormTextBox urlImportExport;
protected void SaveNewImportExport(object sender, EventArgs eventArgs)
{
ContentService service = ContentService.Local;
service.Locations.Add(this.urlImportExport.Text);
Response.Redirect("/_admin/contentman.aspx", true);
}
}
}
カスタム サービスを実装した後は、そのサービスのインスタンスを作成してメンバーを呼び出すことにで、サービスのデータにアクセスできます。また、Microsoft.SharePoint.Administration 名前空間にある他の永続的オブジェクトから継承された、他のカスタム オブジェクトを作成することもできます。たとえば、SPJobDefinition クラスから派生したクラスを作成してタイマー ジョブを実装し、カスタム サービスを通して指定時刻に操作を実行することができます。または、SPFeatureReceiver クラスから継承してイベント処理を定義し、サービスがインストールされた時、または機能としてアクティブ化されたときに、そのサービスを登録することもできます。
永続的オブジェクトを使用すれば、カスタムの管理用オブジェクトを構成データベースに追加し、SharePoint Foundation プラットフォーム上に構築された Web アプリケーション用のロジックとデータを保持できます。