Summary:
I believe many friends have encountered the need to set the field value of a people type to the current logged-in user situation and has given a solution, so this article has nothing new, the algorithm is also more redundant, only in the blog as a knowledge of the backup.
Ideas:
In the top right corner of the screen there is a welcome control, the reality of the content of "Welcome, login username ", our code is to take this user name out, and then take the Peoplepicker field corresponding Div, call the innerHTML property to make a worthwhile setting.
Steps:
The first step is to obtain the landing user, through the Welcome to find:
function Getcurrentuser ()
{
var tags = document.getelementsbytagname (' a ');
for (Var i=0 i < Tags.length i++)
{
if (tags[i].innertext.substr (0,7) = = ' Welcome ')
{
return Tags[i].innertext.substr (8,tags[i].innertext.length);}}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/web/sharepoint/
The second step, find the Peoplepicker field corresponding Div, one layer to the back, the algorithm is not good, the last break will continue to cycle, although the final results have no effect, but not a good algorithm, Interested friends find good algorithms or algorithms that can be found in jquery to welcome sharing!
function Getpickerinputelement (fieldsinternalname) {var result = "";
var divs = document.getElementsByTagName ("DIV"); for (Var i=0 i < Divs.length i++) {if (divs[i].id== "WebPartWPQ2") {var
TDS = Divs[i].getelementsbytagname ("TD");
For (Var j=0 j < Tds.length; J + +) {var cellhtml = tds[j].innerhtml; if (Cellhtml.indexof (' fieldinternalname= ' + fieldsinternalname + ' "') >= 0) {var i
Nnerdivs = Tds[j].getelementsbytagname ("DIV"); For (Var k=0 k < Innerdivs. length; k++) {if (Innerdivs[k].id.indexof ("User
Field_upleveldiv ") > 0) {result = Innerdivs[k];
Break
}}}} return to result; }
The third step, set the value, we set it through a method called Fillpeoplepickerwithcurrentuser, which calls the SharePoint library method _spbodyonloadfunctionnames, Make sure that the method executes after the page content load completes.
_spbodyonloadfunctionnames.push ("Fillpeoplepickerwithcurrentuser");
function Fillpeoplepickerwithcurrentuser ()
{
var currentUser = Getcurrentuser ();
if (currentUser!= null)
{
var pp = getpickerinputelement ("Setme");
if (pp!= null)
pp.innerhtml = CurrentUser
}
}
Description
This method is not debugged in the Firefox browser.
Author: cnblogs Johnsonwang