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-Type’,’text/xml; charset=utf-8′ );
httpInst.setRequestHeader( ‘SOAPAction’,’http://schemas.microsoft.com/sharepoint/soap/SearchPrincipals’ );
var postBody = ‘[?xml version="1.0" encoding="utf-8"?][soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-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)
login