javascript 使用 JS 为当前用户设置 peoplepicker 默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12174181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:29:48  来源:igfitidea点击:

Set peoplepicker default value with current user with JS

javascriptsharepoint

提问by MademoiselleLenore

I'm using sharepoint services 3.0 and I've created a custom list form. I have in this form a peoplepicker field and need it to be set by default to the current user.

我正在使用 sharepoint services 3.0 并且我已经创建了一个自定义列表表单。我在这个表单中有一个 peoplepicker 字段,需要将它默认设置为当前用户。

This is what I've got so far:

这是我到目前为止所得到的:

  • First my peoplepicker fonction:

    function PeoplePicker(){

    this.context = null;
    this.web = null;
    this.currentUser = null;
    this.parentTagId = null
    
    this.SetParentTagId = function(id){
        this.parentTagId = id;
    }
    
    this.SetLoggedInUser = function(){
        if(this.parentTagId != null){
            this.getWebUserData();
        }
    }
    
    this.getWebUserData = function(){
        this.context = new SP.ClientContext.get_current();
        this.web = this.context.get_web();
        this.currentUser = this.web.get_currentUser();
        this.currentUser.retrieve();
        this.context.load(this.web);
        this.context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
                                       Function.createDelegate(this, this.onFailureMethod));
    }
    
    this.onSuccessMethod = function(){
        this.setDefaultValue(this.currentUser.get_title());
    }
    
    this.onFailureMethod = function(){
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    
    this.setDefaultValue = function(value){
        var parentTag = document.getElementById(this.parentTagId);
        if (parentTag != null) {
            var peoplePickerTag = this.getTagFromIdentifierAndTitle(parentTag, 'div',
                                                    'UserField_upLevelDiv', 'People Picker');
            if (peoplePickerTag) {
                peoplePickerTag.innerHTML = value;
            }
        }
    }
    
    this.getTagFromIdentifierAndTitle = function(parentElement, tagName, identifier, title){
        var len = identifier.length;
        var tags = parentElement.getElementsByTagName(tagName);
        for (var i = 0; i < tags.length; i++) {
            var tempString = tags[i].id;
            if (tags[i].title == title && (identifier == "" ||
                                tempString.indexOf(identifier) == tempString.length - len)) {
                return tags[i];
            }
        }
        return null;
    }
    

    }

  • secondly the way I use it in my asp content :

  • 首先是我的 peoplepicker 功能:

    函数 PeoplePicker(){

    this.context = null;
    this.web = null;
    this.currentUser = null;
    this.parentTagId = null
    
    this.SetParentTagId = function(id){
        this.parentTagId = id;
    }
    
    this.SetLoggedInUser = function(){
        if(this.parentTagId != null){
            this.getWebUserData();
        }
    }
    
    this.getWebUserData = function(){
        this.context = new SP.ClientContext.get_current();
        this.web = this.context.get_web();
        this.currentUser = this.web.get_currentUser();
        this.currentUser.retrieve();
        this.context.load(this.web);
        this.context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
                                       Function.createDelegate(this, this.onFailureMethod));
    }
    
    this.onSuccessMethod = function(){
        this.setDefaultValue(this.currentUser.get_title());
    }
    
    this.onFailureMethod = function(){
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    
    this.setDefaultValue = function(value){
        var parentTag = document.getElementById(this.parentTagId);
        if (parentTag != null) {
            var peoplePickerTag = this.getTagFromIdentifierAndTitle(parentTag, 'div',
                                                    'UserField_upLevelDiv', 'People Picker');
            if (peoplePickerTag) {
                peoplePickerTag.innerHTML = value;
            }
        }
    }
    
    this.getTagFromIdentifierAndTitle = function(parentElement, tagName, identifier, title){
        var len = identifier.length;
        var tags = parentElement.getElementsByTagName(tagName);
        for (var i = 0; i < tags.length; i++) {
            var tempString = tags[i].id;
            if (tags[i].title == title && (identifier == "" ||
                                tempString.indexOf(identifier) == tempString.length - len)) {
                return tags[i];
            }
        }
        return null;
    }
    

    }

  • 其次,我在我的 asp 内容中使用它的方式:

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "PeoplePicker.js");

function SetWebUserData() { var pplPicker = new PeoplePicker(); // Set the parent tag id of the people the picker. pplPicker.SetParentTagId('ctl00_PlaceHolderMain_g_ec44fef5_c1a6_4ade_b7e4_2b80803724a1_ff6_1_ctl00_ctl00_UserField'); pplPicker.SetLoggedInUser(); }

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "PeoplePicker.js");

函数 SetWebUserData() { var pplPicker = new PeoplePicker(); // 设置选择器的人的父标签id。pplPicker.SetParentTagId('ctl00_PlaceHolderMain_g_ec44fef5_c1a6_4ade_b7e4_2b80803724a1_ff6_1_ctl00_ctl00_UserField'); pplPicker.SetLoggedInUser(); }

But nothing happens when I load the form...Am I using the wrong parent tag ID? I took it from the source code of the page...

但是当我加载表单时没有任何反应......我是否使用了错误的父标签 ID?我从页面的源代码中获取了它...

Any help would be great !

任何帮助都会很棒!

回答by AxGryndr

Here is how I do it.

这是我如何做到的。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript"> 
$(window).load(function(){
var columnName = 'Manager Name';   
var searchText = RegExp("FieldName=\"" + columnName + "\"", "gi");
$("td.ms-formbody").each(function() {  
if(searchText.test($(this).html())) {  
$(this).find("div[Title='People Picker']").html(getUserName());  
return false;  
}  
});
function getUserName() { 
var wshell=new ActiveXObject("wscript.shell"); 
var userName=wshell.ExpandEnvironmentStrings("%username%");  
return userName;}   
});
</script>

My people picker field is named 'Manager Name' but it is a variable so you can change it. I use the function getUserName() to set the value of the people picker. getUserName uses the Environment String username. Some people will tell you that is not the best thing to use because it can be 'faked'. I am in a corporate environment where this is tightly controlled. Personally I don't think most user will even know how to do that.

我的人员选择器字段名为“经理姓名”,但它是一个变量,因此您可以更改它。我使用函数 getUserName() 来设置人员选择器的值。getUserName 使用环境字符串用户名。有些人会告诉您这不是最好的使用方法,因为它可以“伪造”。我在一个严格控制的公司环境中。我个人认为大多数用户甚至不知道如何做到这一点。

I think the issue you are having is the value a people picker field needs is not just a string. I believe it is looking for a key value pair which would mean you need the user name and ID number assigned in the SharePoint site. To get around this the code I use just puts the username sting in the html form field.

我认为您遇到的问题是人员选择器字段需要的值不仅仅是一个字符串。我相信它正在寻找一个键值对,这意味着您需要在 SharePoint 站点中分配的用户名和 ID 号。为了解决这个问题,我使用的代码只是将用户名字符串放在 html 表单字段中。

Hope this helps.

希望这可以帮助。

回答by Ishan

Here is how i have implemented, here i had passed the username as a querystring and if not than populated with the current user. Modify as per your needs.

这是我的实现方式,这里我将用户名作为查询字符串传递,如果没有,则填充当前用户。根据您的需要进行修改。

        var Dname = queryString('DName'); 
        if(Dname!= null)
        {       
            var pp = getPickerInputElement('Employee');
            if(pp != null)
            {
                pp.innerHTML =Dname;
                document.getElementById('Emp').innerHTML=Dname;
            }
        }
        else
        {   
            fillPeoplePickerWithCurrentUser('Employee');
        }   
function fillPeoplePickerWithCurrentUser(pickerName)
    {
        var currentUser =getUserData();
    }
function getUserData() 
    {   
        context = new SP.ClientContext.get_current();             
        web = context.get_web(); 
        currentUser = web.get_currentUser(); 

        currentUser.retrieve(); 
        context.load(web);
        context.executeQueryAsync(onSuccessMethod, onFaiureMethodl);
    }