November Happy Hour will be moved to Thursday December 5th.

Does anyone know if there is possible to copy form elements from a form to another?

Vote:
 

What I have tried is something like this:

writeableForm.ElementsArea = fromForm.ElementsArea.Copy();


Where I have found both forms created. And when I look at the new form all the elements are there, the problem is not a new copy, it is the same element in both forms. So if I make a change in one of the forms, it changes in both. Is there anyway to f.ex loop through all the items of the ElementsArea and create a new one based on the Element?

#268232
Dec 10, 2021 23:41
Vote:
 

Hi Kenneth,

Check out the information on this blog post https://www.aperturelabs.biz/blog/creating-episerver-forms-v.

Optimizely Forms are based upon blocks and therefore can be created programmatically. 

It may take a bit more coding than we would like, however it is possible.

Paul

#268234
Edited, Dec 11, 2021 22:53
Vote:
 

@Paul McGann

Thank you for this.

Ended up with something like this, if it's the best approach I don't know, but it works for me now.

foreach (var item in fromForm.ElementsArea.Items)
{
    if (item.ContentLink.ID == 0)
    {
          continue;
    }
    //create a new form element in that folder
    var content = _contentLoader.Get<IContent>(item.ContentLink);
    var contentType = _contentTypeRepository.Load(content.ContentTypeID);
                
    var formElement = _contentRepository.GetDefault<IContent>(formAssetFolder.ContentLink, contentType.ID);
    formElement.Name = content.Name;

    var from = content as ElementBlockBase;
    var to = formElement as ElementBlockBase;
    to.Label = from.Label;
    to.Description = from.Description;

    //save the edited form element
    _contentRepository.Save((IContent)to, SaveAction.Publish, AccessLevel.NoAccess);

    writeableForm.ElementsArea.Items.Add(new ContentAreaItem { ContentLink = formElement.ContentLink });
}

Kenneth

#268385
Dec 14, 2021 12:31
Vote:
 

@Kenneth For using the GUI, there is also this:

Gotcha’s with reusing Episerver Forms ElementBlocks (optimizely.com)

Nils

#321962
May 13, 2024 11:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.