2

I have Sharepoint 2010 with a IT Help Desk type site. I'm trying to get the current user auto-populated into a people picker field (Business Owner).

I've been googling and trying to nut this out but I'm missing some basics. I've been developing ASP.NET apps and websites for nearly 15 years, familiar with javascript, sharepoint designer, but am missing something.

I've seen How to get current user with javascript? , but exactly where on my custom NewItem form do I place the java script code? So using sharepoint designer, open form in Advanced Mode? And then where?

Following site provided a good sample of how to populate a people picker (once you the current users value...); http://sympmarc.com/2008/06/06/set-a-people-pickers-value-on-a-form/

1

Your NewItem.aspx shoud have a Content like this:

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

I strongly recommend you yo put your js scripts in there. And the code that I use is this:

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
    function init(){
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.clientContext.load(currentUser);
        this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
    }

    function onQuerySucceeded() {
        document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
        document.getElementById('userId').innerHTML = currentUser.get_id();
        document.getElementById('userTitle').innerHTML = currentUser.get_title();
        document.getElementById('userEmail').innerHTML = currentUser.get_email();
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    }
    </script>
</asp:Content>
0

I've seen How to get current user with javascript? , but exactly where on my custom NewItem form do I place the java script code? So using sharepoint designer, open form in Advanced Mode? And then where?

First save your JavaScript into a text file or HTML file and upload it to a document library on your site.

You can add it from the browser.

  1. Open the form in a new window (not a modal dialog)
  2. Click Site Actions -> Edit Page
  3. Add a content editor web part below the list form web part
  4. Edit the web part and set the content link property to the path to your file.
0

I now understand the technique used to embed js into a form;

  1. upload a js file to site assets
  2. add content editor web part (CEWP) to form.
  3. then link the js file to CEWP.

The full js code used follows the asp:Content line;

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
    // Following js code is used to auto-populate Business Owner (ff31) with current user.
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
    function init(){
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.clientContext.load(currentUser);
        this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
    }

    function onQuerySucceeded() {
        // alert(currentUser.get_loginName());
        // populate Business Ower People picker control id 'ff31'
        setPickerInputElement('ff31',currentUser.get_loginName());
    }

    function onQueryFailed(sender, args) {
        // alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    }

    // Marc D anderson - http://sympmarc.com/2008/06/06/set-a-people-pickers-value-on-a-form/
    // setPickerInputElement: Set a People Picker's value using its identifier to find it in the page
    // Arguments:
    // identifier: The identifier for the instance of the fieldName (ff1, ff2, etc.)
    // value:      Set the fieldName to this value
    //
    function setPickerInputElement(identifier, value) {
        var tags = document.getElementsByTagName('DIV');
        for (var i=0; i < tags.length; i++) {
            var tempString = tags[i].id;
            //alert('tags[' + i + '].id = ' + tempString);
            if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0)){
                // alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value + ' tags[i].innerHTML:' + tags[i].innerHTML );
                // Only popoulate on initial load. Empty field will contain '&nbsp;', but once populated, full html is approx 130 chars long.
                // so thi scaters for user changing, but script NOT overwriting during form validation failure postback.
                if ( tags[i].innerHTML.length < 10 ) { 
                    tags[i].innerHTML = value;
                }
                break;
            }
        }
    }

</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.