Freigeben über


SharePoint 2007 (MOSS/WSS) how to change the template link within a InfoPath Form under a Form Library to point it to a New Form Template

Requirement: You have designed an InfoPath Form Template and published it as a Content Type. You have a Form Library where you have made this Content Type as the Default Content Type. You have added few Forms which are associated with this Form Template or Content Type. Now you want to add few more fields into the Template and want to publish the new version and make it as Default Content Type. Also you want to change the old Form Instances available in the Library to point to this new Template so that newly added fields will remain blank in the old Forms. Here is a code for a Web Part to do that. You have to enter the name of the Form Library (not the full path) in the textbox available and hit the submit button.

 

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace ChangeTemplateWP

{

    [Guid("790c62d1-cb13-4cb3-bc55-78b8fc2093cf")]

    public class ChangeTemplateWP : System.Web.UI.WebControls.WebParts.WebPart

    {

        System.Web.UI.WebControls.Button submitButton;

        System.Web.UI.WebControls.TextBox textFormLib;

        protected override void Render(HtmlTextWriter writer)

        {

            EnsureChildControls();

            textFormLib.RenderControl(writer);

            submitButton.RenderControl(writer);

        }

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            submitButton = new System.Web.UI.WebControls.Button();

            textFormLib = new System.Web.UI.WebControls.TextBox();

            submitButton.Text = "Change Template";

            submitButton.Click += new EventHandler(submitButton_Click);

            Controls.Add(textFormLib);

            Controls.Add(submitButton);

        }

        void submitButton_Click(object sender, EventArgs e)

        {

            SPSite _site = SPContext.Current.Site;

            SPWeb _web = SPContext.Current.Web;

            SPList _list = _web.Lists[textFormLib.Text];

            for (int i = 0; i < _list.ContentTypes.Count; i++)

            {

                string _oldURL = _list.ContentTypes[i].DocumentTemplateUrl;

                string _newURL = _list.ContentTypes[0].DocumentTemplateUrl;

                foreach (SPListItem _item in _list.Items)

                {

                    _item.ReplaceLink(_oldURL, _newURL);

                }

            }

           

        }

    }

}

Comments

  • Anonymous
    September 27, 2007
    PingBack from http://www.artofbam.com/wordpress/?p=3423

  • Anonymous
    October 10, 2007
    hai Pranab, I was looking for this functionality. thanks for the excellent post. can i do the same thing even for document libraries? thing is i have some documtents in document library woth particular template..now can i change the existing docs to other template. like...there is a dropdown mentioning available templates for this document library..when i select a template and create or upload new doc in doc library.it should be created with the mentioned template. please guide me... thanks, Aravind Kathiroju.

  • Anonymous
    October 25, 2007
    Hi noticed that you are using the ReplaceLink function. I am trying to use it in a migration scenario. The problem is i want to update the links in some documents with new addresses. Unfortunately the ReplaceLink function does not work the moment there are QueryString parameters in the link... eg: www.example.com?id=20. Would you know of any work around for this problem?

  • Anonymous
    November 27, 2007
    If you keep the fields (text field, drop down list etc) in separate sections within the Form Templates,

  • Anonymous
    November 27, 2007
    If you keep the fields (text field, drop down list etc) in separate sections within the Form Templates

  • Anonymous
    February 27, 2008
    This is interesting.  I am looking to do this in a workflow. I have two documents library the first a user can fill out a form and submit it for approval.  Once the admin approves the form a workflow copies it to the 2nd document library and removes it for the 1st.  Now I would like the workflow to change the template link in the 2nd doc lib. Any Ideas?