How to set the people picker value in Sharepoint to the current logon user value

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.