+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Forms Duplication in Sitefinity CMS

by Stefani Tacheva
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.

2 comments

Leave a comment
  1. Markus Nov 29, 2013
    It is nice how Telerik provides code samples.

    So if a client want a form copied
    - he calles up the developer
    - the developer goes and looks for the form name
    - the developer creates or edit the page with the given code
    - the developer builds the project
    - the new build is deployed
    - of course the site restarts

    Am I correct?

    If yes - how practical do you think that is?

    And don't tell us to build something that would make this more convenient because then every one (instead of Telerik) would have to do this.

    Don't get me wrong - I don't know much about programming.

    Markus
  2. Craig Dec 09, 2013
    Stefani,

    This is a great blog post, but I wondering if you could go one step further and show how a command could be added to the backend forms list.  I noticed that if you go to the Advanced Settings and then go to Forms > Controls > FormsBackend > Views > FormsBackendList > View Modes > Grid > Columns > ActionsLinkText > MenuItems you can add a new menu item, but I am not sure what properties I would have to complete to get a duplicate command to display here.  Thoughts?

    Craig

    Leave a comment