💻

SharePoint で人の検索ボックスを使って簡易メール フォームを作ってみる

2022/01/01に公開

はじめに

SharePoint 2010 のコントロールで飛び抜けて使い勝手がいいのは 人の検索ボックス (PeopleEditor) と呼ばれるコントロールです。Active Directory のユーザーやグループを検索してくれる優れものです。見た目は以下のような感じで、標準でもいろいろなところで使われています。

今回はこれを使って簡易メール フォームを作ってみます。

サンプル コード

https://github.com/karamem0/samples/tree/main/sharepoint-farm-solution-mail-form

実装方法

プロジェクトは 視覚的 Web パーツ で作成します。

VisualWebPart1UserControl.ascx

メール フォームのデザインを行います。PeopleEditor は初期状態でツール ボックスには出てこないので アイテムの選択 から選択する必要があります。

<div style="width: 400px;">
    <div style="margin: 3px 0px 3px 0px;">
        <label for="ToPeopleEditor" runat="server">宛先:</label>
        <sharepoint:peopleeditor id="ToPeopleEditor" allowempty="false" validatorenabled="true" isclaimsdisabled="True" multiselect="false" runat="server" />
    </div>
    <div style="margin: 0px 0px 3px 0px;">
        <label for="SubjectTextBox" runat="server">件名:</label>
        <asp:textbox id="SubjectTextBox" cssclass="ms-input" textmode="SingleLine" width="100%" runat="server" />
        <asp:requiredfieldvalidator controltovalidate="SubjectTextBox" enableclientscript="false" errormessage="この必須フィールドに値を指定してください。" runat="server" />
    </div>
    <div style="margin: 0px 0px 3px 0px;">
        <label for="BodyTextBox" runat="server">本文:</label>
        <asp:textbox id="BodyTextBox" cssclass="ms-input" textmode="MultiLine" width="100%" runat="server" />
        <asp:requiredfieldvalidator controltovalidate="BodyTextBox" enableclientscript="false" errormessage="この必須フィールドに値を指定してください。" runat="server" />
    </div>
    <div style="margin: 0px 0px 3px 0px;">
        <asp:button id="SubmitButton" causesvalidation="true" text="送信" onclick="SubmitButton_Click" runat="server" />
    </div>
</div>

VisualWebPart1UserControl.ascx.cs

送信 ボタンをクリックしたときのイベントを定義します。PeopleEditor.ResolvedEntities プロパティには PickerEntity クラスのコレクションが入っています。PickerEntity クラスには選択されたユーザーに関する情報を格納するので、Email 要素を抜き出して To アドレスに設定します。[1] これは具体的には Active Directory の mail 属性を取ってきているようです。From アドレスには現在のユーザーのアドレスを設定します。

public partial class VisualWebPart1UserControl : UserControl
{

    protected void Page_Load(object sender, EventArgs e) { }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        this.ToPeopleEditor.Validate();
        if (this.Page.IsValid == false)
        {
            return;
        }
        var entity = this.ToPeopleEditor.ResolvedEntities.Cast<PickerEntity>().Single();
        var to = (string)entity.EntityData["Email"];
        if (to == null)
        {
            return;
        }
        var from = SPContext.Current.Web.CurrentUser.Email;
        var msg = new MailMessage(
            from,
            to,
            this.SubjectTextBox.Text,
            this.BodyTextBox.Text);
        var client = new SmtpClient();
        client.Host = "localhost";
        client.Send(msg);
    }

}

実行結果

実行してみるとこんな感じで表示されます。

人の検索ボックスの 参照 ボタン (辞書っぽいアイコン) をクリックすると検索ダイアログが別ウィンドウで開きます。

件名と本文を入力して 送信 ボタンをクリックすると、メールが送信されることを確認できます。

おわりに

人の検索ボックスは企業内アプリケーションを作る上ではいろいろな場面での活用が期待できる優れたコントロールです。また、機能を拡張してさらにいろいろなことができたりします。

脚注
  1. PeopleEditor.Entities プロパティというのもありますが、こちらは確定されていないユーザー (下線が付いてない) の情報が格納されます。 ↩︎

Discussion