2

I need to retrieve the set of fields (specifically, their internal names) that would show in the EditForm.aspx of a list, using JavaScript, preferably JSOM. In the SP.Field documentation, there is a setShowInEditForm, but no getShowInEditForm. So this is effectively a "write-only" property, which is both strange and unhelpful.

I tried using an ajax call to get the text of the EditForm.aspx and use regex to extract the internal names from the conveniently placed comments near each field, but these actually are only present in the Classic UI Experience, and the modern UI forms don't seem to have any relevant info in their source. So, that fell apart, but it was pretty hacky anyway.

So it seems there is no way to get the edit form fields with JavaScript. Please, someone, tell me I'm wrong!

2

Yes, there is no property like getShowInEditForm, it's only setShowInEditForm that used to control the display of a specific field in edit form by setting true to show it or false to hide it in the edit form.

But based on your requirement, I suggest doing the following:

  1. In your list, Create a new view "Edit Fields View"
  2. Add all the fields in the edit form in this view.
  3. use the below JSOM code to get the fields name of this view.

[JSOM]

function retrieveFieldsOfListView(listTitle,viewName){

   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var view = list.get_views().getByTitle(viewName);
   var viewFields = view.get_viewFields();
   context.load(viewFields);
   context.executeQueryAsync(printFieldNames,onError);


   function printFieldNames() {
      var e = viewFields.getEnumerator();
      while (e.moveNext()) {
         var fieldName = e.get_current();
         console.log(fieldName);
      }
   }

   function onError(sender,args)
   {
      console.log(args.get_message());
   }

}

I think the above workaround could match your requirement

  • Hi, thanks for the suggestion, but this solution requires manually finding the fields in the edit form and then adding them to a view, while my requirement is to programmatically find the fields in the edit form with no manual steps. – broken-e Sep 22 '17 at 23:18
  • I don't think there is a way to get the edit form fields with JSOM directly, but I hope to find a good workaround, by the way, it's not purely manual steps, you just do it only one (in case you don't change this setting) and get the fields by code from view instead the edit form, – Mohamed El-Qassas MVP Sep 22 '17 at 23:26
  • If I was going to manually find the list fields, I could just put them directly into a JavaScript array right in the code, and avoid the unnecessary network request. But this needs to work on many arbitrary lists with different schema, so a manual setup step is exactly what I'm trying to avoid. Thanks anyway though. – broken-e Sep 25 '17 at 18:20
  • Agree, but if you don't found any dynamic solution and only the manual solution is the available one so it's not preferred to add it in JS because if you changed the field name for any reason you will force to change it in your code! Right? so what I have mentioned in the good one for the manual solution, and again hope to find a dynamic solution for your issue :) – Mohamed El-Qassas MVP Sep 25 '17 at 18:24
0

Just a followup and basically an answer to my question, I've found that if you append ?isdlg=1 to the DefaultEditFormUrl and use that to retrieve the form with ajax, it will always be returned in the Classic UI Experience format. As I mentioned in my question, this format always seems to have those comments containing the internal names, which I can scrape with regex. Still a hacky solution since it's not official API, but it's the only solution I have found.

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.