I think you are passing the wrong field name in your code.
Note:
In order to get the field using SPUtility
, the field should be available on list form. You cannot get the field on list form using SPUtility
if it is hidden from Content Type.
Solution:
- Do not make your fields hidden from content type settings.
- Use below codes to first set the value in your field and then make them hidden using
SPUtility
.
According the documentation given at: SPUtility - Common SPField functions,
- SPUtility.GetSPField(strFieldName) - Get a field on DispForm, EditForm, or NewForm by the field's display name. Returns an SPField object.
- SPUtility.GetSPFieldByInternalName(strFieldName) - Get a field by its internal column name. Returns an SPField object.
- In
GetSPField()
method you need to pass the Display name of your field (Which you can see in the list form or list view).
- In
GetSPFieldByInternalName()
method you need to pass the Internal name of your field.
Set Single Line of Text Field:
<script type="text/javascript">
$(document).ready(function(){
var date = new Date();
var dateString = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear(); //dd/mm/yyyy format
SPUtility.GetSPField('Date2').SetValue(dateString);
});
</script>
Set Date & Time Field:
<script type="text/javascript">
$(document).ready(function(){
var date = new Date();
SPUtility.GetSPField('Date2').SetDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
});
</script>
Hide a field from list form:
SPUtility.GetSPField('Date2').Hide();
Assuming, where Date2 is display name of your field.
Update from Comments:
Follow below steps:
- Upload
jQuery
and SPUtility
files into a Document Library (Probably Site Assets) in your SharePoint site.
- Edit the SharePoint form you wish to modify and add a Content Editor Web Part with the following script inside of it:
<script type="text/javascript" src="/sites/SiteName/SiteAssets/jquery.js"></script>
<script type="text/javascript" src="/sites/SiteName/SiteAssets/sputility.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// TODO: Your scripts go here!
// EXAMPLE: Set the value of the Title field to Hello world!
SPUtility.GetSPField('Title').SetValue('Hello world!');
});
</script>
Reference: SPUtility - Usage.