2

The script does not work. I have to write the date value in the field (I tried to do both a regular text field and a Date field) Date2 As it seems to me because of the wrong name. But maybe something else.

    <script type="text/javascript">
      var dat = new Date();
      document.writeln(dat);
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
       var dateValue= SPUtility.GetSPField('Date2').SetValue(dat);
    });
    </script>

At the moment there is this usual representation of the LIST. It has columns with the name, date of creation, notes and date2 (it was planned that the column would be hidden).

I add the script through the implementation of the code. At first I did everything according to the instructions - but there was no result.

enter image description here

2
  • What is the display name of your field? Commented Feb 3, 2020 at 13:27
  • Date2 - Plain text box.
    – AlexGR
    Commented Feb 3, 2020 at 13:32

1 Answer 1

1

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:

  1. Do not make your fields hidden from content type settings.
  2. 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.
  1. In GetSPField() method you need to pass the Display name of your field (Which you can see in the list form or list view).
  2. 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:

  1. Upload jQuery and SPUtility files into a Document Library (Probably Site Assets) in your SharePoint site.
  2. 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.

15
  • Thanks. But it didn’t work. do not understand why. Created a new column in the same test list. Just named differently. In the scripts you proposed, the name changed accordingly. In the debugger, it produces the same error. ~sputility.min.js:10 Uncaught Unable to get a SPField named Data2
    – AlexGR
    Commented Feb 3, 2020 at 14:42
  • There are scripts for testing on the utility site. Do you think it makes sense, and how, to launch?
    – AlexGR
    Commented Feb 3, 2020 at 14:48
  • How you are adding this code in your list form? Are you referencing jQuery or SP Utility files multiple times on your page? Also try to create a new single line text with simple name like "MyTextField"(without spaces), do not make it hidden, do not change its display name and then check getting the field using above code. And then try setting its value or making it hidden. Commented Feb 3, 2020 at 15:04
  • I connect the script as in the instructions. link to the jquery + link to the utility. after the script itself. This is all adding to the page. A page of type List, and I add it through the "Web Part" - "Script Editor". I refresh the page (f5) and look in the debugger, which confirms that there is an error
    – AlexGR
    Commented Feb 4, 2020 at 6:30
  • 1
    Go to your list --> Click on LIST from top riibon --> Click on Forms Web Parts under Customize List section --> Select Default New Form or Default New Form --> When new page opens select Gear(settings) icon --> Edit Page --> Add Webpart --> Use content editor/script webpart --> add your code in it. Commented Feb 4, 2020 at 7:25

Your Answer

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

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