Tuesday, August 23, 2011

Sharepoint 2010 : Create or Provisioning Custom EditForm.aspx

below post is useful for below scenarios
=============================================

suppose if you are in a situation like, you rolled out hundreds of sites to production environment, after that if u get any new requirements for editform.aspx, then we have to upgrade existing editform.aspx. in our situation (need to apply javascript code for 6000 sites) we could not figrued out how to upgrade existing editform.aspx so we decided to create a new custom editform.aspx and changed existing list defaultediturl to customeditform.aspx

to acheive this we have to create a feature and where ever we want, we can activate this feature.

once it is activated our list automatically pointing to customeditform.aspx page instead of editform.aspx

namespace TestUpdateEditForm.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("8dd12231-23e6-4b86-94c9-56b1143cdd40")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPSite oSite = (SPSite)properties.Feature.Parent)
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {

                        SPList oList = oWeb.Lists.TryGetList("CustomDocuments");
                        SPFolder oRootFolder = oList.RootFolder;
                        if (oList != null)
                        {
                            string customFormUrl = oWeb.Url + "/" + oRootFolder.Url + "/CustomEditForm.aspx";
                            SPFile CustomEditform = ListExtension.ProvisionDisplayForm(oList, "CustomEditForm.aspx", PAGETYPE.PAGE_EDITFORMDIALOG, "/_layouts/XSL/formxml.xsl");

                            oList.DefaultEditFormUrl = CustomEditform.Url;
                            oList.Update();
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}

Add ListExtension Class as mentioned in the picture.




namespace TestUpdateEditForm.Features.Feature1
{
    static class ListExtension
    {
        public static SPFile ProvisionDisplayForm(this SPList list, string formName, PAGETYPE pageType, string xslUrl)
        {

            SPWeb currentWeb = list.ParentWeb;
            Uri webUrl = new Uri(currentWeb.Url);
            if (!formName.Contains(".aspx")) formName += formName + ".aspx";

            //get into the list form folder and provision a new form
            SPFolder root = list.RootFolder;
            string customFormUrl = webUrl.LocalPath + "/" + root.Url + "/Forms/" + formName;
            SPFile file = root.Files.Add(customFormUrl, SPTemplateFileType.FormPage);

            //Provision IList Web Part in this case DataForm web part onto the new form
            SPLimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
            ListFormWebPart OlistFormWPEdit = new ListFormWebPart();
           
            OlistFormWPEdit.ListName = list.ID.ToString("B").ToUpper();
            OlistFormWPEdit.ListId = list.ID;     

            OlistFormWPEdit.PageType = PAGETYPE.PAGE_EDITFORMDIALOG;
            OlistFormWPEdit.Title = "";
            OlistFormWPEdit.ListTitle = list.Title;
            OlistFormWPEdit.TemplateName = "ListForm";
            OlistFormWPEdit.AllowClose = false;
            OlistFormWPEdit.AllowConnect = false;
            OlistFormWPEdit.ControlMode = SPControlMode.Edit;
            OlistFormWPEdit.FormType = 6;
            OlistFormWPEdit.AllowZoneChange = false;          

            //add the webpart onto the page
            limitedWebPartManager.AddWebPart(OlistFormWPEdit, "Main", 0);

            //this code is usefull when u want to append javascript
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement element = xmlDoc.CreateElement("p");
            XmlAttribute align = xmlDoc.CreateAttribute("align");
            align.Value = "right";
            element.Attributes.Append(align);
            element.InnerText = "<script type='text/javascript'>_spBodyOnLoadFunctionNames.push('TestJavascript');"+
                                "function TestJavascript() {" +
                                "alert('SuryaTesting');}" +
                                "</script>";
            ContentEditorWebPart oCWP = new ContentEditorWebPart();
            oCWP.ChromeType = PartChromeType.None;
            oCWP.Content = element;

            limitedWebPartManager.AddWebPart(oCWP, "Main", 1);

           
            //set form as custom display form
            switch (pageType)
            {

                case PAGETYPE.PAGE_DISPLAYFORM:
                    list.DefaultDisplayFormUrl = customFormUrl;
                    break;
                case PAGETYPE.PAGE_EDITFORM:
                    list.DefaultEditFormUrl = customFormUrl;
                    break;
                case PAGETYPE.PAGE_NEWFORM:
                    list.DefaultNewFormUrl = customFormUrl;
                   
                    break;
                default:
                    break;
            }
            return file;

        }
    }
}

once you build and deploy this feature, go to site settings > site collection features > 
you can see your feature and click on activate button.
go to you library when you edit existing item , now you can see  customeditform .aspx

8 comments:

  1. need help to implement what you have shown in blog. but find it difficult

    i want to change my list default display url to customer page created in visual studio in _layout folder. can i do it using your method.

    ReplyDelete
  2. Ram, when you activate this feature it creates a new customeditform.aspx and changes the editform url. please follow this post

    http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/thread/ad569953-417c-4a23-bddd-f36c0216e770

    ReplyDelete
  3. This is nice.
    How do I make "CustomEditForm.aspx" with code behind? Use Application page in the solution?
    I figure out I can add my own webPart to it.
    But when I make changes, I don't see a way to overwrite the exiting form.

    ReplyDelete
  4. Ofer, As you said, you can create a webpart and add that webpart to editform.aspx page.
    for ex :
    go to your list > in the ribbon you click on "List" option > on the right side you can see "Modify form webparts" option > click on Default Edit form there u can add your custom webpart.

    If you follow above process i think when you deploy your webpart it automatically updates your changes.

    ReplyDelete
  5. one more option is you can create a usercontrol and register in your editform.aspx page.

    if you want to create a custom application page then you have to create a custom list definition. this is little bit complex.




    _layouts/mynewform.aspx
    _layouts/mynewform.aspx
    _layouts/myeditform.aspx




    please refer below post well

    http://www.codeproject.com/Articles/223431/Custom-SharePoint-List-Forms

    ReplyDelete
    Replies
    1. http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/6e7d8c9c-fdc9-462c-890a-1af8b1b629c4/

      Delete
  6. This comment has been removed by the author.

    ReplyDelete

Followers

Blog Archive