原文地址:点击打开链接
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|[email protected]
1. 查询站点的User Information List,这个用户名保存在LoginName这个属性中:
https://yoursite.sharepoint.com/sites/pubsite/_api/Web/GetUserById(17)
https://yoursite.sharepoint.com/sites/pubsite/_api/SP.UserProfiles.PeopleManager/GetMyProperties
废话少说,直接上代码:
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|[email protected]"; //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);
(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|[email protected]"; //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);
(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);
(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|[email protected]","i:0#.f|membership|[email protected]"]; //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);
<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|[email protected]</Parameter> <Parameter Type="String">PreferredName</Parameter> </Parameters> </Method> <Method Name="GetUserProfilePropertyFor" Id="3" ObjectPathId="0"> <Parameters> <Parameter Type="String">i:0#.f|membership|[email protected]</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。