javascript 取得当前用户的组和权限(上)...
时间:2010-08-18 来源:MECHEAL
最近需求要用javascript 取得当前用户的组和权限, 首先先要取得用户名
取得用户名有2种方法
1 通过页面上的welcome信息
2 通过sharepoint webservice
方法1:
<div>
UserName:
<input type="text" id = "userName"/>
</div>
<script language="JavaScript">
function getCurrentUser()
{
var tags = document.getElementsByTagName('a');
for (var i=0; i < tags.length; i++)
{
if(tags[i].innerText.substr(0,7) == 'Welcome')
document.getElementById('userName').value = tags[i].innerText.substr(8,tags[i].innerText.length);
}
}
getCurrentUser();
</script>
方法2:通过不同的webservice有两种方式
2.1 通过_sqUserId从User Infomation List中取得,
不过这个方法有个缺点,这个列表只存在于顶级网站中,而且需要用户有权限
<div>
UserName:
<input type="text" id = "userName"/>
</div>
<script type="text/javascript" src="Shared Documents/SPAPI_Core.js">
</script>
<script type="text/javascript" src="Shared Documents/SPAPI_Lists.js">
</script>
<script type="text/javascript">
function getCurrentUser()
{
var lists = new SPAPI_Lists('')
var items = lists.getListItems(
'User Information List',
'',
'<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">' + _spUserId + '</Value></Eq></Where></Query>', // query
'<ViewFields><FieldRef Name="Name"/></ViewFields>',
1, // rowLimit
'' // queryOptions
);
if (items.status == 200)
{
var rows = items.responseXML.getElementsByTagName('z:row');
document.getElementById('userName').value = rows[0].getAttribute('ows_Name');
}
}
getCurrentUser();
</script>
2.2直接通过如下方法
<div>
UserName:
<input type="text" id = "userName"/>
</div>
<script type="text/javascript" src="Shared Documents/SPAPI_Core.js">
</script>
<script type="text/javascript" src="Shared Documents/SPAPI_Lists.js">
</script>
<script type="text/javascript" src="Shared Documents/SPAPI_UserProfile.js">
</script>
<script type="text/javascript">
function getCurrentUser()
{
var Profile = new SPAPI_UserProfile('http://moss/Dalian/');
var Profiledata = Profile.getUserProfileByName(null);
if(Profiledata.status == 200)
{
var rows = Profiledata.responseXML.getElementsByTagName('PropertyData');
for(var i = 0; i<rows.length; i++)
{
var item = rows[i];
var itemName = item.getElementsByTagName('Name')[0].childNodes[0].nodeValue;
if(itemName == "AccountName")
{
document.getElementById('userName').value = item.getElementsByTagName('Value')[0].childNodes[0].nodeValue;
break;
}
}
}
}
getCurrentUser();
</script>