Abstract:
I believe many of my friends have met the need to set the field value of the people type to the current login user and have provided a solution. Therefore, this article has nothing to do with it,AlgorithmIt is also redundant. It is counted as a backup of knowledge only in the blog.
Ideas:
There is a welcome control in the upper-right corner of the screen. The actual content is "Welcome,Login Username", OurCodeThis is to get the username, and then get the DIV corresponding to the javaseconicker field, and call the innerhtml attribute to set the value.
Steps:
Step 1: Obtain the login user and use welcome to search for it:
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 );}}}
Step 2: Search for the DIV corresponding to the javaseconicker field. The algorithm is not good, and the loop continues after the break. Although it has no effect on the final result, but it is not a good algorithm. If you are interested, you can find a good algorithm or an algorithm that can be found through jquery. You are welcome to share it!
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 innerdivs = TDS [J]. getelementsbytagname ("Div"); For (var k = 0; k <innerdivs. length; k ++) {If (innerdivs [K]. id. indexof ("userfield_upleveldiv")> 0) {result = innerdivs [k]; break ;}}}} return result ;}
Step 3: set the value through a method called fillpeoplepickerwithcurrentuser. Call the SharePoint library method _ spbodyonloadfunctionnames to ensure that the method is executed after the page content is loaded.
_ Spbodyonloadfunctionnames. Push ("fill?#ickerwithcur=user"); function fill=#ickerwithcur=user () {var currentuser = getcurrentuser (); If (currentuser! = NULL) {var pp = getpickerinputelement ("setme"); If (PP! = NULL) pp. innerhtml = currentuser ;}}
Note:
This method is not debugged in the Firefox browser.