sharepoint获取用户属性

1.通过Jsom获取用户属性


<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script>


var currentUser,ctx,oweb,personProperties;
function getUserpropery()
{ 
    ctx = new SP.ClientContext.get_current();
    oweb=ctx.get_web();
    currentUser=oweb.ensureUser("a84d4zz@mmm.com");
    ctx.load(currentUser);
    ctx.executeQueryAsync(onRequestSuccess,onRequestFail);
}
function onRequestSuccess()
{
     var loginName=currentUser.get_loginName();
    console.log(loginName);
     var peopleManager = new SP.UserProfiles.PeopleManager(ctx);
     personProperties = peopleManager.getPropertiesFor(loginName); //获取指定用户
   //personProperties = peopleManager.getMyProperties(); 获取当前登录用户 clientContext.load(personProperties); clientContext.executeQueryAsync(
function() { console.log(personProperties.get_userProfileProperties()["Manager"]); },function(){}); } function onRequestFail(sender, args) { console.log(args.get_message()); }

2.通过Csom获取用户属性
需要引用

Microsoft.SharePoint.Client
Microsoft.SharePoint.ClientRuntime
Microsoft.SharePoint.Client.UserProfiles

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;

namespace UserProfilesCSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            const string serverUrl = "http://serverName/";  
            const string targetUser = "domainName\\\\userName";  
            ClientContext clientContext = new ClientContext(serverUrl);
            PeopleManager peopleManager = new PeopleManager(clientContext);
            PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);
            clientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
            clientContext.ExecuteQuery();

            foreach (var property in personProperties.UserProfileProperties)
            {
                Console.WriteLine(string.Format("{0}: {1}", 
                    property.Key.ToString(), property.Value.ToString()));
            }
            Console.ReadKey(false);

        }
    }
}

 3.通过Rest API获取用户属性
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

$.ajax({  

        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",  
     // url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor('i:0#.f|membership|account@domin')" headers: { Accept:
"application/json;odata=verbose" }, success: function (data) { try { var properties=data.d.UserProfileProperties.results; properties.forEach(function(result){ if(result.Key =="Manager") console.log(result.Value); }) } catch (err) { //alert(JSON.stringify(err)); } }, error: function (errorThrown) { alert(errorThrown); } });

 

更多详细内容可以参照:https://docs.microsoft.com/en-us/sharepoint/dev/general-development/work-with-user-profiles-in-sharepoint



 

posted on 2019-04-18 17:14  赢在当下_Victor  阅读(476)  评论(0编辑  收藏  举报

导航