ユーザー カスタム アクションを操作する

最終更新日: 2011年4月30日

適用対象: SharePoint Foundation 2010

この記事の内容
リスト アイテムのユーザー カスタム アクションを追加する
ユーザー カスタム アクションを変更する
ユーザー カスタム アクションを Web サイトのサイト アクションに追加する

SharePoint Online で使用可能

クライアント オブジェクト モデルを使用して、カスタム アクションをユーザー インターフェイスに追加できます。UserCustomActions プロパティはサイト コレクション、Web サイト、またはリストのカスタム アクションのコレクションを返します。これらのコレクションの 1 つにカスタム アクションを作成するには、UserCustomActionCollection クラス (JavaScript: UserCustomActionCollection) の Add() メソッド (JavaScript: add()) を呼び出します。新しいアクションのプロパティを、返された UserCustomAction オブジェクト (JavaScript: UserCustomAction) に設定し、Update() メソッド (JavaScript: update()) を呼び出してから、ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) メソッドを呼び出してクエリを実行します。ユーザー カスタム アクションの位置は、その名前空間の場所、カスタム アクション グループ、およびシーケンスと他のユーザー カスタム アクションとの関連によって決定できます。カスタム アクションの場所とグループの可能な値の一覧については、「カスタム アクションの既定の場所および ID」を参照してください。

リスト アイテムのユーザー カスタム アクションを追加する

次の例では、リスト アイテムに対して表示されるドロップダウン メニューにユーザー カスタム アクションを追加します。新しいアクションをメニューに配置するには、Location プロパティ (JavaScript: location) に、EditControlBlock を指定し、Sequence (JavaScript: sequence) に、他のユーザー カスタム アクションに関連した配置の順番を指定し、Url (JavaScript: url) に、アクションを定義するページへの絶対パスを指定します。この例は、%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS に配置された .aspx ファイルの存在を前提とします。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateUserCustomActionList
    {
        static void Main()
        {
            string urlWebsite = "http://MyServer/sites/MySiteCollection";
            ClientContext clientContext = new ClientContext(urlWebsite);
            Web oWebsite = clientContext.Web;

            List oList = oWebsite.Lists.GetByTitle("My List");
            UserCustomActionCollection collUserCustomAction = oList.UserCustomActions;

            UserCustomAction oUserCustomAction = collUserCustomAction.Add();
            oUserCustomAction.Location = "EditControlBlock";
            oUserCustomAction.Sequence = 100;
            oUserCustomAction.Title = "My First User Custom Action";
            oUserCustomAction.Url = urlWebsite + @"/_layouts/MyPage.aspx";
            oUserCustomAction.Update();

            clientContext.Load(oList,
                list => list.UserCustomActions);

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class CreateUserCustomActionList

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim clientContext As New ClientContext(siteUrl)

            Dim oWebsite As Web = clientContext.Web

            Dim oList As List = oWebsite.Lists.GetByTitle("My List")
            Dim collUserCustomAction As UserCustomActionCollection = oList.UserCustomActions

            Dim oUserCustomAction As UserCustomAction = collUserCustomAction.Add()
            oUserCustomAction.Location = "EditControlBlock"
            oUserCustomAction.Sequence = 100
            oUserCustomAction.Title = "My First User Custom Action"
            oUserCustomAction.Url = siteUrl + "/_layouts/MyPage.aspx"
            oUserCustomAction.Update()

            clientContext.Load(oList, Function(list) list.UserCustomActions)

            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
siteUrl = '/sites/MySiteCollection';

function createUserCustomActionList() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.oList = oWebsite.get_lists().getByTitle('My List');
    var collUserCustomAction = oList.get_userCustomActions();
        
    var oUserCustomAction = collUserCustomAction.add();
    oUserCustomAction.set_location('EditControlBlock');
    oUserCustomAction.set_sequence(100);
    oUserCustomAction.set_title('My First User Custom Action');
    oUserCustomAction.set_url(siteUrl + '/_layouts/MyPage.aspx');
    oUserCustomAction.update();
        
    clientContext.load(oList, 'Title' ,'UserCustomActions');
    
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Custom action created for ' + this.oList.get_title());}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

ユーザー カスタム アクションを変更する

次の例では、リスト内のアイテムのドロップダウン メニューに対するユーザー カスタム アクションのコレクションからアクションを取得し、カスタム アクションを更新して、アクションを表すアイコンをメニューに含めます。この例は、%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\IMAGES に配置されたアイコン イメージ ファイルの存在を前提とします。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class ModifyUserCustomAction
    {
        static void Main()
        {
            string urlWebsite = "http://MyServer/sites/SiteCollection";
            ClientContext clientContext = new ClientContext(urlWebsite);
            Web oWebsite = clientContext.Web;

            List oList = oWebsite.Lists.GetByTitle("My List");
            UserCustomActionCollection collUserCustomAction = oList.UserCustomActions;

            clientContext.Load(collUserCustomAction,
                userCustomActions => userCustomActions.Include(
                    userCustomAction => userCustomAction.Title));

            clientContext.ExecuteQuery();

            foreach (UserCustomAction oUserCustomAction in collUserCustomAction)
            {
                if (oUserCustomAction.Title == "My First User Custom Action")
                {
                    oUserCustomAction.ImageUrl = "http://MyServer/_layouts/images/MyIcon.png";
                    oUserCustomAction.Update();

                    clientContext.ExecuteQuery();
                }
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class ModifyUserCustomAction

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            Dim oList As List = oWebsite.Lists.GetByTitle("My List")
            Dim collUserCustomAction As UserCustomActionCollection = oList.UserCustomActions

            clientContext.Load(collUserCustomAction, _
                               Function(userCustomActions) userCustomActions.Include( _
                                   Function(userCustomAction) userCustomAction.Title))

            clientContext.ExecuteQuery()

            Dim oUserCustomAction As UserCustomAction
            For Each oUserCustomAction In collUserCustomAction
                If oUserCustomAction.Title = "My First User Custom Action" Then
                    oUserCustomAction.ImageUrl = "http://MyServer/_layouts/images/MyIcon.png"
                    oUserCustomAction.Update()

                    clientContext.ExecuteQuery()
                End If
            Next oUserCustomAction

        End Sub
    End Class
End Namespace
siteUrl = '/sites/MySiteCollection';

function modifyUserCustomAction() {

    this.clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.oList = oWebsite.get_lists().getByTitle('My List');
    this.collUserCustomAction = oList.get_userCustomActions();
        
    clientContext.load(oList,'UserCustomActions','Title');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.SetImage), Function.createDelegate(this, this.onQueryFailed));
}

function SetImage() {

    var customActionEnumerator = collUserCustomAction.getEnumerator();

    while (customActionEnumerator.moveNext()) 
    {
        var oUserCustomAction = customActionEnumerator.get_current();
            
        if (oUserCustomAction.get_title() == 'My First User Custom Action') 
        {
            oUserCustomAction.set_imageUrl('http://MyServer/_layouts/images/MyIcon.png');
            oUserCustomAction.update();
                
            clientContext.load(oUserCustomAction);

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }
}

function onQuerySucceeded() {

    alert('Custom action changed for ' + this.oList.get_title());
}

function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

ユーザー カスタム アクションを Web サイトのサイト アクションに追加する

ユーザー カスタム アクションを Web サイトの [サイトの操作] メニューに作成する処理は、リスト アイテムのアクションを作成する処理に似ています。Add() メソッド (JavaScript: add()) を呼び出し、アクションのプロパティを設定して、Update() (JavaScript: update()) を呼び出します。次の例では、Microsoft.SharePoint.StandardMenu を Location (JavaScript: location) に指定し、SiteActions を Group (JavaScript: group) に指定して、新しいアクションを [サイトの操作] メニューに配置します。Sequence (JavaScript: sequence) の値に 101 を指定して、シーケンス番号が 100 のアクションの下に、アクションが表示されるようにします。この例は、%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS に配置された .aspx ファイルの存在を前提とします。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateUserCustomActionSite
    {
        static void Main()
        {
            string urlWebsite = "http://MyServer/sites/MySiteCollection";
            ClientContext clientContext = new ClientContext(urlWebsite);

            Web oWebsite = clientContext.Web;
            UserCustomActionCollection collUserCustomAction = oWebsite.UserCustomActions;

            UserCustomAction oUserCustomAction = collUserCustomAction.Add();

            oUserCustomAction.Location = "Microsoft.SharePoint.StandardMenu";
            oUserCustomAction.Group = "SiteActions";
            oUserCustomAction.Sequence = 101;
            oUserCustomAction.Title = "Website User Custom Action";
            oUserCustomAction.Description = "This description appears on the Site Actions menu.";
            oUserCustomAction.Url = urlWebsite + @"/_layouts/MyPage.aspx";

            oUserCustomAction.Update();

            clientContext.Load(oWebsite,
                webSite => webSite.UserCustomActions);

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class ModifyUserCustomAction

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web

            Dim collUserCustomAction As UserCustomActionCollection = oWebsite.UserCustomActions

            Dim oUserCustomAction As UserCustomAction = collUserCustomAction.Add()

            oUserCustomAction.Location = "Microsoft.SharePoint.StandardMenu"
            oUserCustomAction.Group = "SiteActions"
            oUserCustomAction.Sequence = 101
            oUserCustomAction.Title = "Website User Custom Action"
            oUserCustomAction.Description = "This description appears on the Site Actions menu."
            oUserCustomAction.Url = siteUrl + "/_layouts/MyPage.aspx"

            oUserCustomAction.Update()

            clientContext.Load(oWebsite, Function(webSite) webSite.UserCustomActions)

            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
siteUrl = '/sites/MySiteCollection';

function createUserCustomActionSite() {

    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();
    var collUserCustomAction = oWebsite.get_userCustomActions();
        
    var oUserCustomAction = collUserCustomAction.add();
    oUserCustomAction.set_location('Microsoft.SharePoint.StandardMenu');
    oUserCustomAction.set_group('SiteActions');
    oUserCustomAction.set_sequence(101);
    oUserCustomAction.set_title('ECMA Website User Custom Action ECMA');
    oUserCustomAction.set_description('This description appears on the Site Actions menu.');
    oUserCustomAction.set_url(siteUrl + '/_layouts/jstest2.aspx');
    oUserCustomAction.update();
        
    clientContext.load(oWebsite, 'Title', 'UserCustomActions');
    
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    alert('Custom action created for ' + this.oWebsite.get_title());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Microsoft SharePoint Foundation 2010 Silverlight オブジェクト モデルのコンテキスト内でのクライアント オブジェクトの使用に関する情報および使用例については、「Silverlight オブジェクト モデルを使用する」を参照してください。

関連項目

概念

カスタム アクションの既定の場所および ID

データ取得の概要

クライアント オブジェクトの作成

クライアント オブジェクト モデルのガイドライン

一般的なプログラミング作業

Web サイト ナビゲーション

その他の技術情報

クライアント クラス ライブラリ

ECMAScript クラス ライブラリ