JavaScriptを利用して、列に既定値を設定する(2)

前回の続きです。今度は、ユーザーとグループ列に既定値を設定します。

基本的な手順は、前回と同様です。
NewForm.aspxを編集モードで開き、コンテンツエディタWebパーツを挿入。
そこにスクリプトを記述します。
しかし、今回は「現在のユーザー」情報を取得するため、少し複雑です。

[code]<script type="text/javascript">
_spBodyOnLoadFunctionNames.push( "setDefault" );

function getNodeValue( nodeInst, name )
{
return ( nodeInst.selectSingleNode( name ) != null )
? nodeInst.selectSingleNode( name ).text : ”;
}

function setDefault()
{

var loginDisplayName = document.getElementById( "<font color="red">zz7_Menu</font>" ).innerText;
var loginName = loginDisplayName.substring( 4, loginDisplayName.lastIndexOf( "さ", loginDisplayName.length ) );

var httpInst = window.XMLHttpRequest ? new XMLHttpRequest() : ( function() {
try { return new ActiveXObject( "Msxml2.XMLHTTP" ); }
catch( e ) { return new ActiveXObject( "Microsoft.XMLHTTP" ); }
})();

httpInst.open( "POST", "<font color="red">http://server/_vti_bin/People.asmx</font>", false );
httpInst.setRequestHeader( ‘Content&#45;Type’,’text/xml; charset=utf&#45;8′ );
httpInst.setRequestHeader( ‘SOAPAction’,’http://schemas.microsoft.com/sharepoint/soap/SearchPrincipals’ );
var postBody = ‘[?xml version="1.0" encoding="utf&#45;8"?][soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema&#45;instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"][soap:Body][SearchPrincipals xmlns="http://schemas.microsoft.com/sharepoint/soap/"][searchText]’ + loginName + ‘[/searchText][maxResults]1[/maxResults][principalType]All[/principalType][/SearchPrincipals][/soap:Body][/soap:Envelope]';
httpInst.onreadystatechange = function()
{
if ( httpInst.readyState == 4 ){
xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
xmlDoc.async = false;
xmlDoc.loadXML( httpInst.responseText );
rowsItem = xmlDoc.selectSingleNode( ‘/soap:Envelope/soap:Body/SearchPrincipalsResponse/SearchPrincipalsResult/PrincipalInfo’ );
accountNameFld = getNodeValue( rowsItem, ‘<font color="red">AccountName</font>’ );
}
}
httpInst.send( postBody );

document.getElementById( "<font color="red">ctl00_m_g_9d58ea13_8111_4bd6_aa0d_c2905fded564_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv</font>" ).innerText = accountNameFld;

}
</script>[/code]

このスクリプトは、まず、サイト右上にある「ようこそ ○○ さん」から、ユーザー名を抜き取ります。
次にWebサービスにそのデータを渡し、ユーザーのADアカウントを取得します。
ユーザー名を特定するためのタグ(zz7_Menu)とWebサービスのアドレス(http://server/_vti_bin/People.asmx)は環境により異なりますので、適宜修正して下さい。

また、ユーザーとグループ列のIDもソースから取得する必要があります。
ただし、この時、列名直後のinputタグではなく、そのかなり後にあるdivタグのidを習得して下さい。
末尾が「_UserField_upLevelDiv」のidです。

このWebサービス経由で、アカウント以外にも、以下の情報を取得することが可能です。
特に所属は利用価値が高いでしょう。

AccountName アカウント
Department 所属
Email メールアドレス
UserInfoID 管理番号
Title 役職

まだ続きます。

JavaScriptを利用して、列に既定値を設定する(1)
JavaScriptを利用して、列に既定値を設定する(2)
JavaScriptを利用して、列に既定値を設定する(3)


Author

中村 和彦(シンプレッソ・コンサルティング株式会社 代表)が「ユーザ視点の SharePoint 情報」を発信します。元大手製造業 SharePoint 運用担当。現SharePoint コンサルタント。お仕事のお問い合わせはこちらまでお願いします。当ブログにおける発信内容は個人に帰属し所属組織の公式発信/見解ではありません。
FB : 中村 和彦
blog: Be・Better!
MS MVP SharePoint 2009/10-2011/9
MS MVP Office 365 2012/10-2014/9