在Sharepoint列表中获取用户职位

时间:2018-08-06 11:28:50

标签: sharepoint sharepoint-online

因此,我有一个SP Online列表,每个用户都可以提交其条目。我有一个默认列“ created by”,其中列出了提交条目的用户。我想另设两列,以便从用户帐户中自动复制用户部门和职务。我该怎么办?

1 个答案:

答案 0 :(得分:0)

  1. 方法1: 您可以使用JSOM设置Department / JobTitle。因为我们不确定您的用户来自(AD还是其他)

首先,您需要在SharePoint页面上加载“ SP.UserProfiles.js,SP.Runtime.js和SP.js” js文件,然后使用以下代码段加载这些文件, $(document).ready(function(){

var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";

$.getScript(scriptbase + "SP.Runtime.js", 
    function () { 
        $.getScript(scriptbase + "SP.js", 
           function () { 
               $.getScript(scriptbase + "SP.UserProfiles.js", GetUserInformation);
            }); 
    });

});

var userProfileProperties;

function GetUserInformation() {


// Get the current client context.

    var clientContext = SP.ClientContext.get_current();

    //Get Instance of People Manager Class    

    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var targetUser = "domain\\userId";
    userProfileProperties = peopleManager.getPropertiesFor(targetUser);



    //Execute the Query.   

    clientContext.load(userProfileProperties);

    clientContext.executeQueryAsync(onSuccess, onFail);    }



function onSuccess () { 
    // get user information from userProfileProperties and set to your target column. 
   userProfileProperties.get_userProfileProperties()['Department'];
   userProfileProperties.get_userProfileProperties()['SPS-JobTitle'];

//要做:列出项目替换操作     }

function onFail (sender, args) { 
    alert("Error: " + args.get_message()); 
}

参考:https://msdn.microsoft.com/en-us/library/office/jj679700.aspx

  1. 方法2:工作流程

    当用户添加/编辑项目时,我们触发工作流将用户的部门/职位名称复制到目标列

获取部门/职务的参考。 https://sharepoint.stackexchange.com/questions/97971/get-current-user-information-and-add-to-email-action-in-workflow