0

I have been using Mark Rackley's code to move fields into custom forms and it has been working quite nicely in 2010/2013, but we just upgraded the database on our internal portal from 2010 to 2013 and then to 2016 and I've noticed some strange stuff going on.

In list forms that were already existing, everything works as before, but when I create a new list form, the table that contains the formlabel and formbody cells does not have the ms-formtable class applied. This, of course, breaks the code. I have written a workaround that tests for ms-formtable and handles things if not found, but it's not pretty and I would just like to know if there is something I could be doing wrong. Has anyone else seen this issue and better yet, does anyone have any idea what's the cause and solution?

4
  • Just to be clear, I have kludged my code to make it work. My question is whether this is a known issue or is my environment somehow hosed up...? I Googled for missing ms-formtable and no one else is complaining about it that I could find, so it makes me wonder if the problem is isolated to my instance.
    – Charlie
    Commented Jan 24, 2018 at 12:27
  • I'm having the same issue. Can you post your workaround? Commented Apr 2, 2018 at 16:13
  • I answered down below. Sorry it took so long - I've been swamped this week...
    – Charlie
    Commented Apr 6, 2018 at 13:12
  • Thanks! I actually solved it, but this is a good approach too. Commented Apr 6, 2018 at 16:13

2 Answers 2

0

I have checked the script created in the link you provide, and I see that no CSS is applied by code.

I think there are two options:

  1. In the second line of the script, there is a display:none on .ms-formtable, so maybe, you should delete that line because the script isn't finding that class.
  2. Maybe, in SharePoint 2016, .ms-formtable doesn't exist anymore, so the script doesn't work as expected. If this is the cause, you should be receiving an error in your browser Console (Web Inspector), reporting that .ms-formtable isn't found, failing in the line 11:

    $(“table.ms-formtable td”).each(function(){

    So, somehow you should have to insert the .ms-formtable CSS class, so that the script wouldn't fail, or modify the script to take another CSS class as reference.

4
  • I have fixed the code so that if ms-formtable cannot be found, it instead goes to ms-formlabel and ms-formbody and moves the fields. The problem then is that I used to have CSS on the page setting .ms-formtable's display to none and the user never saw the old table, but now in the cases where that class doesn't exist, I have to go to the first occurrence of ms-formlabel and then get closest table and hide it - by doing that, the user sees the old table flash in for a split second and then disappear. Functionally OK, but annoying.
    – Charlie
    Commented Jan 24, 2018 at 12:23
  • So, If I undersantd correctly, second option I say is the cause. By what you are saying, you are using JavaScript to hide the corresponding table. You may try using CSS, specifically :nth-child(), which is much faster and user won't see that "flash". Please mark as correct the answer :)
    – Sergio
    Commented Jan 24, 2018 at 15:59
  • Sergio, nth-child works only to find children - I need to find the parent which can only be accomplished through JavaScript. CSS can only look down from a selector, not up.<br /><br />In any case, it does not address my question, which is has SP2016 stopped applying ms-formtable, or is this a problem unique to my instance?
    – Charlie
    Commented Jan 24, 2018 at 18:21
  • I dont have access to a SP 2016 (soon I will) but it seems there is no doc about css classes used in the 2016 version. Try searching in corev15.css file and see if you find that class to discover if it's been removed (reference). Here you have a list of classes used in the 2010 version with .ms-formtoolbar and its location. You may try creating a new SP instance and check if the class exists, like this, to know if your instance is "corrupted"
    – Sergio
    Commented Jan 24, 2018 at 19:05
0

Here's the work-around:

function moveFormFields(checkForOrphans) {
/////////////////////////////////////////////////////////////////////////////////////////////////
// Loop through all the spans in the custom layout and move all SP fields into the custom form //
// if they have a corresponding span with a data-displayName attribute specifying their id.    //
/////////////////////////////////////////////////////////////////////////////////////////////////
$("span.replacementForm").each(function() {
    //get the display name from the custom layout
    var displayName = $(this).attr("data-displayName");
    var elem = $(this);
    //find the corresponding field from the default form and move it
    //into the custom layout
    if ($("table.ms-formtable td").length > 0) {
        $("table.ms-formtable td").each(function(){
            if (this.innerHTML.indexOf('FieldName="' + displayName + '"') != -1){
                $(this).contents().appendTo(elem);
            }
        });
    } else {
        // SP2016 has stopped applying the ms-formtable class to the form fields table, thus breaking the code above.
        $("td.ms-formlabel nobr").each(function(){
            // Not using indexOf because we do NOT want to hit on partial matches.
            if (this.innerHTML == displayName || this.innerHTML == displayName + '<span class="ms-formvalidation"> *</span>') {
                $(this).closest("td").next("td").contents().appendTo(elem);
            }
            // Hide this row even if there was no destination to move to.
            // This allows us to hide fields by not moving them to a visible location.
            $(this).closest("tr").css("display", "none");
        });

        $("[id*='gobackbutton1']").closest("table").parent().parent().css("display", "none");
        $("[id*='savebutton1']").closest("table").parent().parent().css("display", "none");
        // The next line was removed because it was hiding the file upload selector, so
        // instead we hide each row in the loop above rather than hiding hte entire table.
        // $("td.ms-formlabel").closest("table").css("display", "none");
    }
});

////////////////////////////////////////////////////////////////////////////
// For setup and troubleshooting, enable checkForOrphans to see if we     //
// correctly set up all of our fields.  If set up correctly, there should //
// only be popups for fields intentionally left off of the form.          //
////////////////////////////////////////////////////////////////////////////
if (typeof(checkForOrphans) == undefined || checkForOrphans == null)
    checkForOrphans = false;
if (checkForOrphans) {
    $("table.ms-formtable td").each(function() {
        if (this.innerHTML.indexOf('FieldName=') != -1){
            var start = this.innerHTML.indexOf('FieldName="') + 11;
            var displayName = this.innerHTML.substring(start, this.innerHTML.indexOf('"', start+1));
            alert(displayName);
        }
    });
}

}

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.