How to set the people picker value to the current Logged-on user value in SharePoint

Source: Internet
Author: User

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

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.