how to deploy a custom list form using Visual Studio 2010, when the list is created in the feature EventReceiver?

1 Answer 1


When I do this I go in and manually set the urls and/or modify the existing page and update it with my custom web part that I wrote via the object model inside a feature receiver. You can write a new one or use the existing one.

    //In this case I have a custom web part that I want to use on both the new and edit form
    private void SetNewAndEditForm(Microsoft.SharePoint.SPList list, SPWeb web)
        SPContentType ct = list.ContentTypes[contentTypeName];

        //I'll be using the edit form for both item creation and updating so I build the edit form url (This specific set of strings is for a documentLibrary
        string fileUrl = web.Url + "/" + documentLibraryName + "/Forms/EditForm.aspx";
        SPFile page = web.GetFile(fileUrl);

        //This is how the form URLs get set
        ct.NewFormUrl = page.ServerRelativeUrl;
        ct.EditFormUrl = page.ServerRelativeUrl;
        web.AllowUnsafeUpdates = true;

        using (SPLimitedWebPartManager wpartman = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
            //Loop through and hide the existing web parts 
            //You could also remove them but this causes some weirdness in SharePoint designer so only hide them if you need to work with attachments is my recommendation
            for (int i =  wpartman.WebParts.Count - 1; i >=0; i--)
                System.Web.UI.WebControls.WebParts.WebPart wp = wpartman.WebParts[i];
                wp.Hidden = true;

            //Add your web part to the page and set its properties
            MyWebPart webpart = new MyWebPart();
            webpart.ChromeState = PartChromeState.Normal;
            webpart.ChromeType = PartChromeType.None;
            wpartman.AddWebPart(webpart, "Main", 1);


        web.AllowUnsafeUpdates = false;
  • Thanks for your commend and after reading it I realized I didn't describe my question well. I created the form in SharePoint Designer, I tried to create a module and add the file in that way ,but it failed. I just want to be able to include the file in visual studio and have the file deployed under /Lists/list name/
    – lopezcs
    Sep 12, 2012 at 18:23
  • Can you include more details in how it failed? The big problem I see is that if you are going from dev->test->production the unique IDs that SharePoint designer uses to reference the list will be different in every environment so you would have to swap those out in the file upon creation of the list. Sep 13, 2012 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.