Forms Duplication in Sitefinity CMS

Forms Duplication in Sitefinity CMS

November 29, 2013 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

We have a lot of requests form clients about duplication of existing Sitefinity forms.

Please find a sample showing how to duplicate a specific form programmatically:

protected void Page_Load(object sender, EventArgs e)
        {
            var fromManager = FormsManager.GetManager();
 
            //Get an existing form
            var firstForm = fromManager.GetForms().Where(f => f.Title == "Test").FirstOrDefault();
            if (firstForm.LockedBy != null)
            {
                fromManager.UnlockForm(firstForm.Id, false);
            }
            var firstDraft = fromManager.EditForm(firstForm.Id);
 
             //Duplicate the form
            DuplicateForm(firstForm.Title, firstDraft, fromManager);
            fromManager.Lifecycle.CheckIn(firstDraft);
        }
  
        void DuplicateForm(string title, FormDraft draftToDuplicate, FormsManager receivingManager)
        {
            var dateString = DateTime.Now.ToString("MMddHHmm");
            var name = "sf_adave" + "_" + dateString;
            var formManager = receivingManager;
 
            //Create a new form
            var newForm = formManager.CreateForm(name);
  
            newForm.Title = title + "_" + dateString;
            var draft = formManager.EditForm(newForm.Id);
            var master = formManager.Lifecycle.CheckOut(draft);
  
            formManager.CopyControls(draftToDuplicate.Controls, master.Controls);
  
            master.SuccessMessage = draftToDuplicate.SuccessMessage;
            master.Owner = draftToDuplicate.Owner;
            master.LastModified = DateTime.Now;
  
            foreach (var control in master.Controls)
            {
                control.Published = false;
            }
              
            master = formManager.Lifecycle.CheckIn(master);
  
            formManager.Lifecycle.Publish(master);
  
            formManager.SaveChanges(true);
        }

In the code we get an existing form and duplicate its controls and create a new form. You could change the name and title of the form to be different than the one we are using above. Execute the form in a new web form, created in Visual Studio.

Note that the sample works in monolingual environment.

Stefani Tacheva

View all posts from Stefani Tacheva on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
More From Progress
570x321_ResourceImage_RITM0089185
See What's New in Sitefinity 13.1
Read More
 
232x131_ResourceImage
Creating Epic Digital Experiences for B2B Companies
Read More
 
dev_thumb370x200
Five Reasons Why Developers Love Sitefinity Marketplace
Read More