Javascript 在Sharepoint web部件中显示用户图片 我在SharePoint基金会的WebPoint中有这个代码,它显示用户信息,但是现在我需要显示一个用户图片,我不知道怎么做……我对编程很陌生。 提前谢谢
Javascript 在Sharepoint web部件中显示用户图片 我在SharePoint基金会的WebPoint中有这个代码,它显示用户信息,但是现在我需要显示一个用户图片,我不知道怎么做……我对编程很陌生。 提前谢谢,javascript,sharepoint,sharepoint-2013,Javascript,Sharepoint,Sharepoint 2013,ExecuteOrderLayUntilScriptLoaded(init,'sp.js'); 无功电流用户; 函数init(){ this.clientContext=new SP.clientContext.get_current(); this.oWeb=clientContext.get_web(); currentUser=this.oWeb.get_currentUser(); this.clientContext.load(currentUser); this.clientCont
ExecuteOrderLayUntilScriptLoaded(init,'sp.js');
无功电流用户;
函数init(){
this.clientContext=new SP.clientContext.get_current();
this.oWeb=clientContext.get_web();
currentUser=this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this.onQuerySucceed),Function.createDelegate(this.onQueryFailed));
}
函数onQuerySucceeded(){
document.getElementById('userLoginName')。innerHTML=currentUser.get_loginName();
document.getElementById('userTitle')。innerHTML=currentUser.get_title();
document.getElementById('userEmail')。innerHTML=currentUser.get_email();
}
函数onQueryFailed(发送方,参数){
警报('请求失败。\n错误:'+args.get_message()+'\n堆栈跟踪:'+args.get_stackTrace());
}
当前用户:
用户名:
电邮:
您想要的属性称为PictureURL。我还建议在将来寻找这类工作,这会使这项工作更加常规。使用html图像标记,并根据用户显示所需的图像
getCurrentUser
方法不包括任何图片信息。
有关更多信息,请参阅MSDN:
相反,您需要从用户配置文件服务
您可以使用内置SharePoint变量获取当前用户id:\u spPageContextInfo.userId
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getUserProfileImage,'sp.js');
var CamlResult;
function getUserProfileImage(){
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var userInfoList = web.get_siteUserInfoList();
var camlQuery = new SP.CamlQuery();
var userID = _spPageContextInfo.userId;
// define the query to retrieve the given user's details
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="ID"/><Value Type="Number">' + userID + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
CamlResult = userInfoList.getItems(camlQuery);
clientContext.load(CamlResult);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args){
var profile, title, login, email, pic, picUrl;
// There should be only result. Get the item at index 0
profile = CamlResult.itemAt(0);
// read all the properties out
title = profile.get_item('Title');
login = profile.get_item("Name");
email = profile.get_item("EMail");
pic = profile.get_item('Picture');
if (pic) {
picUrl = pic.get_url();
}
// Setup HTML
document.getElementById('userLoginName').innerHTML = login;
document.getElementById('userTitle').innerHTML = title;
document.getElementById('userEmail').innerHTML = email;
if (picUrl) {
// create the image
imgMyPicture = document.createElement('img');
imgMyPicture.src = '/sites/bms/bms/styles/img/OvalMenuImages/processes.png';
imgMyPicture.setAttribute('title', 'Image from Current User's Profile');
imgMyPicture.setAttribute('alt', 'Profile Picture');
// append the image
document.getElementById('userPicture').appendChild(iconElement);
}
else {
document.getElementById('userPicture').innerHTML = "No image found in user profile";
}
}
</script>
<div>Current User:
<span id="userLoginName"></span></br>
</div>
<div>Username:
<span id="userTitle"></span></br>
</div>
<div>Email:
<span id="userEmail"></span></br>
</div>
<div>Picture:
<span id="userPicture"></span></br>
</div>
ExecuteOrderLayUntilScriptLoaded(getUserProfileImage,'sp.js');
var CamlResult;
函数getUserProfileImage(){
var clientContext=new SP.clientContext.get_current();
var web=clientContext.get_web();
var userInfoList=web.get_siteUserInfoList();
var camlQuery=new SP.camlQuery();
var userID=_spPageContextInfo.userID;
//定义查询以检索给定用户的详细信息
camlQuery.set_viewXml(''+userID+'1');
CamlResult=userInfoList.getItems(camlQuery);
clientContext.load(CamlResult);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceed),Function.createDelegate(this,this.onQueryFailed));
}
函数onquerysucceed(发送方,参数){
var配置文件、标题、登录、电子邮件、pic、picUrl;
//应该只有结果。获取索引0处的项
profile=CamlResult.itemAt(0);
//读出所有属性
title=profile.get_项目(“title”);
login=profile.get_项目(“名称”);
email=profile.get_项目(“email”);
pic=profile.get_项目(“图片”);
如果(pic){
picUrl=pic.get_url();
}
//设置HTML
document.getElementById('userLoginName')。innerHTML=login;
document.getElementById('userTitle')。innerHTML=title;
document.getElementById('userEmail')。innerHTML=电子邮件;
if(picUrl){
//创建图像
imgMyPicture=document.createElement('img');
imgmypacture.src='/sites/bms/bms/styles/img/OvalMenuImages/processs.png';
setAttribute('title','Image from Current User's Profile');
setAttribute('alt','Profile Picture');
//附加图像
document.getElementById('userPicture').appendChild(iconeElement);
}
否则{
document.getElementById('userPicture').innerHTML=“在用户配置文件中找不到图像”;
}
}
当前用户:
用户名:
电邮:
图片:
Hello Jason,这对我不起作用。我是否需要对脚本进行任何更改以填充信息?@octopus您使用的SharePoint版本是什么?您的用户档案服务(ups)正在运行吗?您是否添加了图片?UPS可能很难使用,因此可能需要创建一个新的问题,提供您的详细信息……我正在使用SharePoint 2013,我的UPS正在运行,同步正在积极运行?@octopus我发现UPS只会为访问过您网站的用户完成配置文件。因此,如果您试图检索其他用户的详细信息,它可能无法工作。就像我说的,你最好问一个新的问题,详细说明你的具体情况,这样人们才能给你一个更详细的答案