Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • keilo 568 posts 1023 karma points
    Nov 24, 2015 @ 14:27
    keilo
    0

    Retrieve name of all forms and their field names from Guids, via API

    Is there any api call/routine to retrieve the list of all Contour Forms' "Name" with corresponding "FormId" in a razor script?

    Or given the "FormId" and fieldGuid (input field's guid), how can I retrieve the name of the form and input fields?

    Example: (given)

    FormId = fa25a3c0-c025-4dc2-a7a5-e961e23418e0

    512c22f2-0396-45bc-bc25-2a25d4b84a71 = [email protected]

    f13c22f2-0396-41bc-fc21-2a25d4b82af2 = Name of the person


    Goal: Retrieve the Form Name from FormId and field name from field-guids.

    Just to clarify; I have no recordId, so cant use RecordService, only raw information with guid of Formid and field-guids given- from which I would like to translate to their actual names.

    I have checked the Developer document but couldnt find any way api/method.

  • Steve 140 posts 321 karma points
    Nov 24, 2015 @ 20:06
    Steve
    100

    Hi Keilo,
    Here is a basic loop which should help you:

      Umbraco.Forms.Data.Storage.FormStorage fs = new Umbraco.Forms.Data.Storage.FormStorage();
    
    
    foreach (var f in fs.GetAllForms())
    {
        Response.Write(f.Name + " " + f.Id + "<br/>");
    
        foreach (var field in f.AllFields) {
    
            Response.Write(field.Alias + "<br/>");
    
        }
    
    }
    

    The above was hacked together based a more complete (complex) listing that I found recently here on GitHub
    Hope that is helpful!
    Steve

  • keilo 568 posts 1023 karma points
    Nov 25, 2015 @ 09:03
    keilo
    0

    Hi Steve

    Thanks a million! Exactly what i was looking for.

    One minor point is field.Alias doesnt exist, the name of the field is in field.Caption instead. You may wish to edit the post for others stumbling across for the same.

    I found out about field.Caption from Nicholas Westby's posted link (another great find) as I didnt have access to VS or WM.

    cheers!

  • Steve 140 posts 321 karma points
    Nov 25, 2015 @ 17:26
    Steve
    0

    You're welcome - great to hear it was helpful.
    Interesting that you don't see field.Alias - are you using Contour?

  • keilo 568 posts 1023 karma points
    Nov 25, 2015 @ 18:05
    keilo
    0

    Hey Steve

    Yes on Contour latest (not Forms) on umbraco 7.1.8

  • Steve 140 posts 321 karma points
    Nov 26, 2015 @ 16:18
    Steve
    0

    That explains it. I don't see the field.Alias property in Contour, but it is available in Forms.
    This is a minor difference between Contour and Forms but I think this is a good example of how a more clear division between Contour and Forms in both documentation and in the forum would be beneficial. Although many methods and techniques are similar (even the same) between Contour and Forms, there are many significant differences which makes learning how to use Forms very challenging. In some cases code from older Contour examples works 'as is' in Forms and sometimes not at all.
    Perhaps this has been suggested previously but I feel there should be a dedicated Forum category 'Umbraco 7 - Using Forms' to provide a dedicated place for Forms specific discussion.
    Does anyone know how to make such a request?
    Steve

  • keilo 568 posts 1023 karma points
    Nov 26, 2015 @ 16:40
    keilo
    0

    Cant agree more, that would definitely help to bring learned Contour knowledge through the years and assist in transition.

    On related topic, is there any Save event handler for Contour like we have for Content;

    ..FormSaved += new EventHandler ...

    that I can execute code whenever a form is created/saved?

  • Steve 140 posts 321 karma points
    Nov 26, 2015 @ 17:00
    Steve
    1

    Haven't tried this but it appears to be what you are looking for:

     FormStorage.FormUpdated += new EventHandler<FormEventArgs>(FormStorage_FormUpdated);
    
  • keilo 568 posts 1023 karma points
    Nov 26, 2015 @ 17:32
    keilo
    0

    Just did a quick test by putting a .cs to App_Code

     public FormSaveListener()
        {
            FormStorage.FormUpdated += new EventHandler

    When I go to any form and click Save, the I get 2 log entries "A form is Saved" for 1 save action; i.e. the routine is called 2 times.

    Not sure if i am missing something, or its not the right event

  • Steve 140 posts 321 karma points
    Nov 26, 2015 @ 18:39
    Steve
    1

    This works in Forms, logging a single entry. Note the method: FormStorage.Saved (may be specific to Forms)

    public class FormArchiveListener : ApplicationBase
    {
        public FormArchiveListener()
        {
    
            FormStorage.Saved += new EventHandler<FormEventArgs>(FormStorage_FormUpdated);
        }
    
        void FormStorage_FormUpdated(object sender, FormEventArgs e)
        {
            FormStorage storage = (FormStorage)sender;
            LogHelper.Info(typeof(FormArchiveListener), "A form is Saved!");
    
        }
    }
    


    Do you think we should start a new topic with this?
    Cheers
    Steve

  • Manasa 41 posts 161 karma points
    Apr 03, 2018 @ 04:46
    Manasa
    0

    Hello Steve,

    is there any way to get field data? i could fetch the Alias of fields , but i need to get the values of the fields. can you please help,Thanks in advance.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Nov 24, 2015 @ 20:22
    Nicholas Westby
    1

    You can find some examples of iterating over forms/fields here: https://github.com/rhythmagency/rhythm.umbraco.extensions/blob/master/trunk/Rhythm.Extensions/Rhythm.Extensions/Utilities/ContourUtility.cs

    The function GetFieldValues has code very similar to that posted by Steve above.

  • keilo 568 posts 1023 karma points
    Nov 25, 2015 @ 09:04
    keilo
    0

    Thanks Nicholas!

    This is a great find. Helped me to get it up and running.

    cheers!

  • Steve 140 posts 321 karma points
    Nov 24, 2015 @ 20:24
    Steve
    1

    Keilo,
    For your specific example, there is also a FormStorage.GetForm(guid) method which returns a Form object.
    Interestingly, I see that Intellisense is not active when calling Umbraco.Forms.Data.Storage.FormStorage - this could be unique to my install of VS but makes using the class difficult (hard to know it's even there!)
    Steve

  • keilo 568 posts 1023 karma points
    Nov 25, 2015 @ 09:12
    keilo
    0

    Not having access to intellisense will be an issue, as I see myself digging the FormStorage.GetForm(guid) when importing entries.

    What happened is that I have been given formguids (where each contain fieldguids with values).

    I can decode and present the Name, fieldname to the end-user with the first posted routine.

    After which I will need to "import" those entries into an Umbraco Instance where the exact forms (guids) exist in the instance.

    Not sure how to go about that yet.., but Im sure it will start with FormStorage.GetForm(guid) to get handle to the form to start an import job(?).

    given: formguid and their fieldguids and values

    goal: import values for each form, to target instance Contour (i.e. creating new entries programmatically)

  • Steve 140 posts 321 karma points
    Nov 26, 2015 @ 17:08
    Steve
    1

    Not sure why but the Intellisense doesn't seem to kick in with the class Umbraco.Forms.Data.Storage.FormStorage until after instantiating the variable.

Please Sign in or register to post replies

Write your reply to:

Draft