0

I am editing the script and HTML for a Content Editor web part. My code opens up a Modal Dialog when a user clicks a link on my page. In the dialog, the custom fields I've defined for the list appear.

I use the follow code to get the current logged in user's name:

function getWebUserData() 
{  
    context = new SP.ClientContext.get_current();  
    web = context.get_web();  
    currentUser = web.get_currentUser();  
    currentUser.retrieve();  
    context.load(web);  
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));  
    var userObject = web.get_currentUser(); 
    // this is how to get the user name --> userObject.get_title();
}

This code works fine and I only post it here as part of my question.

So now that I can get the current logged in user's name, I need to add it to a custom field in the dialog window. In other words, when the dialog appears, and if there are three fields presented for the user (such as "Your Name", "Priority" and "Message") I need to auto populate the "Your Name" field in the dialog so the user does not need to fill it out.

So, I can get the user data, but how do I set the field in the dialog window with it? I would like to do this in Javascript.

Please help. Thanks

----UPDATE----

Here is part of my code, which might help resolve the issue:

<script type="text/javascript" src="/Extra%20Documents/jquery-1.11.2.js"></script>
<script type="text/javascript" src="/Extra%20Documents/spjs-utility.js"></script>
<script type="text/javascript"> 

ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");  
var context = null;  
var web = null;  
var currentUser = null; 

function getWebUserData() 
{  
    context = new SP.ClientContext.get_current();  
    web = context.get_web();  
    currentUser = web.get_currentUser();  
    currentUser.retrieve();  
    context.load(web);  
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),     Function.createDelegate(this, this.onFailureMethod));  
}

function ContactUsOpenDialog(strPageURL)
{
    var userObject = web.get_currentUser(); 
    var options = 
    {
        title: "Contact Us",
        width: 750,
        height: 550,
        url: strPageURL
    };

    //
    // I suspect that there is a problem in the code below
    //
    fields = init_fields_v2();
    setFieldValue('Your_x0020_Name','My custom value');
    SP.UI.ModalDialog.showModalDialog(options);
}
</script>

// After several lines of HTML

<a href="#" onclick="javascript:ContactUsOpenDialog(&#39;http://example.company.domain/Lists/Contact Us/NewForm.aspx?RootFolder=&#39;);">

Can anyone see the issue?

2 Answers 2

1

You can get the textbox with JavaScript and set it's value..

Use the below script to set the value of different types of fields..

Get or Set value for SharePoint field in NewForm, EditForm and DispForm (Get only in DispForm)

9
  • How do I find out the name of my text box? How are they identified or where do I look for this info?
    – user35589
    Jan 15, 2015 at 23:40
  • the name of the textbox would be Internal name of the field, go to List Settings -> Under columns -> Click the column you want to set.. In the URL, you will get the Internal Name of the Field. Jan 15, 2015 at 23:42
  • Or you can simply use developer toolbar, check the textbox it will have a Title attribute or similar which should contain the field name. That is the textbox name! Jan 15, 2015 at 23:44
  • Ok, great, I can get the name of the text box now. It is Your_x0020_Name
    – user35589
    Jan 15, 2015 at 23:51
  • I am now using this code: document.getElementById('Your_x0020_Name').value = userObject.get_title(); But id does not work. The textbox is empty when I open the window.
    – user35589
    Jan 15, 2015 at 23:52
0

What you may want to do is edit the NewForm.aspx page itself and add JavaScript there for accessing the field you want to update.

To do this, open the NewForm in a new window (not a modal dialog) and choose Site Actions -> Edit Page. Add a content editor web part below the list form web part, and add your JavaScript there. (For best results, add your JavaScript to a text file, upload it to a library on the same site, and reference the file address in the Content Link property of the content editor web part.)

Code such as this, running from a content editor web part below the form itself, should accomplish what you're looking for:

<script>
var userName = "my custom value";
var fieldName = "Your_x0020_Name";
var rows = document.querySelectorAll("td.ms-formbody");
for(var i = 0; i < rows.length; i++){
     if(rows[i].innerHTML.indexOf('FieldInternalName="'+fieldName+'"')>=0){
          var textbox = rows[i].querySelector("div.ms-inputuserfield");
          textbox.innerHTML = userName;
          break;
     }
}
</script>

Edit: Note that the above code is just for a people picker field. It's even easier to update a single line of text field using the same technique.

var customValue = "my custom value"
var fieldName = "Display name of my field";
var textbox = document.querySelector("[title='"+fieldName+"']");
textbox.setAttribute("value",customValue);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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