使用客户端对象模型访问User Profile

原文地址:点击打开链接

SharePoint 2013提供了很多的客户端API,其中的一个就是从User Profile中获取数据。现在你可以在客户端直接通过查询来获取user profile的数据了,这在创建App的时候非常有用。

如果你想使用REST API访问user profile,请参考MSDN的文章:点击打开链接

在这篇博客中我介绍一下如果使用JS 客户端对象模型来访问user profile。

在开始编码之前,首先要做的就是引用一些必须的js文件,例如JQuery, SP.UserProfiles.js等等:

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
<script src="/_layouts/15/SP.Runtime.js"></script>
<script src="/_layouts/15/SP.js"></script>
<script src="/_layouts/15/SP.UserProfiles.js"></script>
然后,为了从user profile中获取某个用户的信息,我们需要用户的用户名username,这个信息在本地部署环境(om-premises farm)中,可以方便的获取是domain\username的形式。但是在SharePoint Online 环境中,获取username这个信息需要一个技巧,因为这online的环境里,用户的username是如下形式:

i:0#.f|membership|vardhaman@yoursite.onmicrosoft.com

因此如果需要获取domain\username形式的数据,有两个方法:

1. 查询站点的User Information List,这个用户名保存在LoginName这个属性中:

https://yoursite.sharepoint.com/sites/pubsite/_api/Web/GetUserById(17)

2. 从User Profile Service中查询,这个信息保存在AccountName属性中:

https://yoursite.sharepoint.com/sites/pubsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties

一个需要注意的地方是,如果你使用REST API,在使用username之前,你需要使用encodeURIComponent()方法来encode这个username。

废话少说,直接上代码:

1) 获取多个User Profile Proerties:

(function($){
 
  $(document).ready(function(){	  				
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');	
  });
	
  var userProfileProperties = [];
  
  function loadUserData(){
		
    //Get Current Context	
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Properties to fetch from the User Profile
    var profilePropertyNames = ["PreferredName","PictureURL"];
    
    //Domain\Username of the user (If you are on SharePoint Online) 
    var targetUser = "i:0#.f|membership|username@yoursite.onmicrosoft.com";	
    
    //If you are on On-Premise:
    //var targetUser = domain\\username
    
    //Create new instance of UserProfilePropertiesForUser
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
    //Execute the Query.
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onSuccess, onFail);
    
  }
    
  function onSuccess() {
    
    var messageText = "\"Preffered Name\" property is " + userProfileProperties[0];
    messageText += "\"PictureURL\" property is " + userProfileProperties[1];
    
    alert(messageText);
      
  }
	
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  }	
    							
})(jQuery);

2)获取一个User Profile Property

(function($){
 
  $(document).ready(function(){  		
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');	
  });
	
  var userProfileProperty;
  
  function loadUserData(){
  
    //Get Current Context	
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Property to fetch from the User Profile
    var propertyName = "PreferredName";
    
    //Domain\Username of the user (If you are on SharePoint Online) 
    var targetUser = "i:0#.f|membership|username@yoursite.onmicrosoft.com";
    
    //If you are on On-Premise:
    //var targetUser = domain\\username
    
    //Create new instance of UserProfileProperty
    userProfileProperty = peopleManager.getUserProfilePropertyFor(targetUser, propertyName)
    
    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);
  
  }
    
  function onSuccess() {
  
    var messageText = "\"Preferred Name\" property is " + userProfileProperty.get_value();	    
    
    alert(messageText);
  
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  }	
    							
})(jQuery);

3)获取当前用户的User Profile Properties:

(function($){
 
  $(document).ready(function(){      	
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');	
  });
	
  var userProfileProperties;
  
  function loadUserData(){
  
    //Get Current Context	
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Get properties of the current user
    userProfileProperties = peopleManager.getMyProperties()
    
    clientContext.load(userProfileProperties);
    
    //Execute the Query.
    clientContext.executeQueryAsync(onSuccess, onFail);
  
  }
    
  function onSuccess() {	    
    
    alert(userProfileProperties.get_displayName());
  
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  }	
    							
})(jQuery);

4)再一次请求中获取多个用户的properties

(function($){
 
  $(document).ready(function(){
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');  
  });
 
  var userProfileProperties = [];
  
  //Array containing domain\usernames of multiple users. You can get the usersnames any way you want.
  var targerUsers = ["i:0#.f|membership|username@yoursite.onmicrosoft.com","i:0#.f|membership|username1@yoursite.onmicrosoft.com"];
  
  //If you are on On-Premise:
  //var targerUsers = ["domain\\username","domain\\demouser1"];
  
  function loadUserData(){
  
    //Get Current Context	
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Property to fetch from the User Profile
    var propertyName = "PreferredName";	    
    
    for(var i=0;i<targerUsers.length;i++){
    
      //Create new instance of UserProfileProperty
      userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);
    }
    
    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);
  
  }
  
  function onSuccess() {
    var messageText = "";
  
    for(var i=0;i<userProfileProperties.length;i++){
      messageText += "\"Preffered Name\" property is " + userProfileProperties[i].get_value();
    }
    alert(messageText);
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  }	
 
})(jQuery);

对于最后一个例子,我们看一下发送给server的xml:

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName="Javascript Library">
  <Actions>
      <ObjectPath Id="1" ObjectPathId="0"></ObjectPath>
      <Method Name="GetUserProfilePropertyFor" Id="2" ObjectPathId="0">
        <Parameters>
          <Parameter Type="String">i:0#.f|membership|username@yoursite.onmicrosoft.com</Parameter>
          <Parameter Type="String">PreferredName</Parameter>
        </Parameters>
      </Method>
      <Method Name="GetUserProfilePropertyFor" Id="3" ObjectPathId="0">
        <Parameters>
          <Parameter Type="String">i:0#.f|membership|username1@yoursite.onmicrosoft.com</Parameter>
          <Parameter Type="String">PreferredName</Parameter>
        </Parameters>
      </Method>
  </Actions>
  <ObjectPaths>
    <Constructor Id="0" TypeId="{D54B5BFA-F89D-4FA7-B3AD-737C8B86385D}"></Constructor>
  </ObjectPaths>
</Request>
从这个xml文件可以看出,一次server请求确实可以获取多个用户的properties,使用这种方法,也可以在一次request中获取多个用户的多个properties。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值