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
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar