Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created a webpart, now i need to add that webpart programmatically into the EditForm.aspx page. Can anyone help me on this.

  • Padmaraj.
share|improve this question

3 Answers

up vote 9 down vote accepted

You can create a feature and on the activation of the feature you can add the webpart to the page.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    using(SPWeb web = site.OpenWeb())
    {
        using(SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(page url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
        {
            YourWebPart webpart = new YourWebPart();
            webpart.ZoneID = "Top";
            webpart.Title = "My Webpart";
            wpManager.AddWebPart(webpart,"Top" , 0);
        }
    }
}

Also have a look at the following link for reference.

http://www.keirgordon.com/post/Add-Web-Part-to-Page-Programmatically.aspx

Hope it helps.

Regards, Geetanjali

share|improve this answer
If suppose my webpart is not in the same solution, How will I be able to create an object and add it to a page? - Padmaraj. – Paddy Aug 22 '11 at 8:59
4  
DISPOSAL WARNING: You need to check the disposals here! OpenWeb() returns a SPWeb object that needs disposal. SPLimitedWebPartManager must be disposed properly as well. – Wictor Wilen MCA MCM MVP Aug 22 '11 at 9:04
I would like to add web part to default.aspx automaticlly exact when the web is created by using a feature. but I don't know when I must do it. because when feature is activated (within onet.xml) default.aspx doesn't exist – Medes Sep 14 '12 at 14:52
@Wictor Wilen MCA MCM MVP If you see a mistake in someone's post, you should fix the mistake. – MgSam Jan 24 at 15:05

Take a look at the SPLimitedWebPartManager.

Grab the SPFile object related to the EditForm.aspx page and then execute the GetLimitedWebPartManager method on the SPFile object. Use the limited web part manager to modify the web part page.

share|improve this answer

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.