admin 管理员组文章数量: 1086019
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.
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 :
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(); }
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...
Any help would be great !
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.
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 :
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(); }
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...
Any help would be great !
Share Improve this question asked Aug 29, 2012 at 8:56 MademoiselleLenoreMademoiselleLenore 5691 gold badge10 silver badges26 bronze badges2 Answers
Reset to default 3Here is how I do it.
<script type="text/javascript" src="http://ajax.googleapis./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.
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.
Hope this helps.
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);
}
本文标签: javascriptSet peoplepicker default value with current user with JSStack Overflow
版权声明:本文标题:javascript - Set peoplepicker default value with current user with JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744083511a2530693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论