Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

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;
        ct.Update();
        list.Update();

        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;
                wpartman.SaveChanges(wp);
            }

            //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);

            page.Update();
        }

        web.Update();
        web.AllowUnsafeUpdates = false;
    }
share|improve this answer
    
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 '12 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. –  ToddersLegrande Sep 13 '12 at 13:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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