Using the Client object model to assign an initial value to a user control in SharePoint SharePoint2013

Source: Internet
Author: User

This article is to implement the function: Create a new list record, open the new record screen, the "applicant" is automatically assigned to the current logged on user.

In SharePoint2010, you can use the Spservices Spfindpeoplepicker method to set the value of the user Selection control.
Where Spgetcurrentuser is used to obtain the user name of the current login.

$(). Spservices.spfindpeoplepicker ({     "applicant",     Valuetoset: $ (). Spservices.spgetcurrentuser (),     true});

But Spservices's Spfindpeoplepicker method has no effect in SharePoint2013.

Later found this article sp2013:setting people picker value in newform.aspx
The original code to hide the deletion icon has a bug, the following is the modified code:

functionSetanddisablepeoplepicker (FieldName, useraccountname) {varControlName =FieldName;varPeoplepickerdiv = $ ("[id$= ' Clientpeoplepicker '][title= '" + controlname + "']");varPeoplepickereditor = Peoplepickerdiv.find ("[title= '" + controlname + "']");varSppeoplepicker = spclientpeoplepicker.spclientpeoplepickerdict[peoplepickerdiv[0].id];    Peoplepickereditor.val (UserAccountName); Sppeoplepicker.addunresolveduserfromeditor (true);    //Disable the fieldSppeoplepicker.setenabledstate (false);    //hide the Delete/remove use image from the people pickerPeoplepickerdiv.find ('. Sp-peoplepicker-delimage '). CSS (' Display ', ' none '));} Setanddisablepeoplepicker ("Applicant", $ (). Spservices.spgetcurrentuser ({fieldName: "UserName"}));

Originally wanted to use the Spgetcurrentuser method default Name property value, but in SharePoint2013 will become "i:0#.w|domain\username" appearance, directly use can not be retrieved to the user.

The Username property value is used directly here, and the value of the following part of the vertical bar in the Name property value can also be used.

Executing the above code alone can result in a successful assignment, but the following error occurs when the screen is executed after loading.

uncaught referenceerror:spclientpeoplepicker is not defined
Or
Uncaught Typeerror:cannot Read Property ' get_current ' of undefined

Debug discovery is caused by the code loading order. Although the JS code uses the _spbodyonloadfunctionnames.push ("FuncName") method to execute the above JS code after the page is loaded, but because of the use of the Registersod Delayed loading, which causes the relevant JS file to not be executed even after the page has been loaded.

<Scripttype= "Text/javascript">Registersod ("Clientpeoplepicker.js", "\u002f_layouts\u002f15\u002fclientpeoplepicker.debug.js?rev=1g1easpdnqkf5utwy7wwma\u00253d\u00253d");</Script><Scripttype= "Text/javascript">Registersod ("Sp.js", "\u002f_layouts\u002f15\u002fsp.debug.js?rev=ir\u00252fvhmqbtniuqecke3hazw\u00253d\u00253d"); REGISTERSODDEP ("Sp.js", "Sp.runtime.js"); REGISTERSODDEP ("Sp.js", "Sp.ui.dialog.js"); REGISTERSODDEP ("Sp.js", "Sp.res.resx");</Script>

Refer to the SharePoint JS Lazy load Class (SP.SOD). Using the Executeordelayuntilscriptloaded method, the Sp.js file is loaded, and then the initial processing is performed.

executeordelayuntilscriptloaded (init, "sp.js"); function init () {    setanddisablepeoplepicker ("Applicant", $ (). Spservices.spgetcurrentuser ({fieldName: "UserName"});}

Using the Client object model to assign an initial value to a user control in SharePoint SharePoint2013

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.