jeudi 22 janvier 2015

How to set the value for a custom field in a list?



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://ift.tt/1IJUpmW Us/NewForm.aspx?RootFolder=&#39;);">


Can anyone see the issue?








0 commentaires:

Enregistrer un commentaire