1

I'm trying to set default values in a new row of a SharePoint Online list in "Quick Edit" mode using code that looks like the below. The date column works as expected: date value is added but the "Init" and "Project" columns get the value but then behave like there is no value, i.e. I get the message that the fields are required and when I click in them the value disappears.

EntryDateTime, Init, and Project appear to be populated as expected: Date, Init, and Project appear to be populated as expected Try to save the row, and get error that Init and Project are required, although they appear to have values: Try to save the row, and get error that Init and Project are required, although they appear to have values Have to enter the values again manually to get the row to save: Have to enter the values again manually to get the row to save

EntryDateTime is working as expected.

Code:

$( document ).ready(function() {
  var t = setInterval(function() {
  if($("#spgridcontainer_WPQ2_leftpane_mainTable").length) {
    clearInterval(t);

    var d = formatDate(new Date());
    $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(1)").text(d);

    $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(2)").text("TS");

    $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(7)").text("307");
  }
  }, 1000);
});
  • 1
    No help from me. This waisted lots of my time. You have to remember most of this code goes back to SharePoint 2003, maybe even 2001. And then in SP2013 they added Client Side Rendering (which by the signals they are not going to support it in the same way in Modern Experiences) I eventually gave up on getting CSR code running in QuickEdit.. dusted off my Angular Skills and wrote my own QuickEdit... and now Microsoft for the Modern Experience QuickEdit first used KnockOut and are now 100% React... alas Sharing is Caring does not apply to the new QuickEdit code yet. Wait a couple of months... – Danny '365CSI' Engelman Dec 26 '16 at 17:09
  • Thanks for your input. This isn't really Client-side rendering though right? I'm just injecting some JavaScript to manipulate a webpage like any other website. The code is embedded with a script editor web part. – Michael Dec 26 '16 at 17:24
  • 1
    And all this is happening in the Browser Client.. you might not be using the Microsoft way of messing around with Client Side content. On every change you make Events get fired and processed by Microsoft code. So you might inject Project number in the UI, it is not recorded by Microsofts internal representation of that grid. So you then open up F12 and the debugger and "go in" step-by-step into Microsofts code.... well, that was my follow-the-Microsoft-brick road adventure.. – Danny '365CSI' Engelman Dec 26 '16 at 17:44
2

Have you try with JSLink?? Create a js file with SharePoint Designer, for example on: Style Library/es-es/js/jslink_yourFile.js

Inside of this file write this:

(function () {
    var overrideContext= {};

    overrideContext.Templates = overrideContext.Templates || {};

    overrideContext.Templates.Fields = {
        "your_internal_name_column": {
            "View": setDefaultValue
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideContext);
})();

function setDefaultValue(ctx) {
    if(ctx.CurrentItem.your_column == "")
        return "your default value";
}

Once the file is written and published, go to the page of the view, edit the page and view the webpart properties (yes the view is a webpart), in the properties look for the section called Miscellaneous, expand it and look for te field JS Link. On that field add the call for you js file like this: ~sitecollection/Style Library/es-es/js/jslink_yourFile.js

This javascript will be executed when you enter on the view form, no matter if it is default view or quick edit mode and will set the value if the column does not have value set it.

According to your answer, quick edit always leave the last tr as new row, so you can get it with JQuery, with this:

$(".ms-listviewgrid tr:last td")

That JQuery will return an array of td's from the last tr of the quick edit grid.

greetings :)

  • JS Link modifies existing list values; I'm trying to set default values for a new row – Michael Dec 27 '16 at 15:14
  • 1
    answer improved – Marco Dec 27 '16 at 15:53
  • I cannot get the new row to populate with JSLink. – Michael Dec 28 '16 at 16:58
  • I can add values to the row with $(".ms-listviewgrid tr:last td") or $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(2)").text("TS"); but SharePoint then acts like the cell is blank. I'm giving up on this for now. – Michael Dec 28 '16 at 16:59
  • did you call it on document ready? Did you add the value into an span? the td format is <td><span title="your value" class="jsgrid-control-text">your value</span></td>. You must maintein this format no matter what. – Marco Dec 28 '16 at 18:15

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.