0

I created a custom web part for SharePoint 2013 and I would like it to be added to a blank page that is created for it.

I looked it up and found out, that the I can add an Event Receiver to the Feature, so I added one, but the event receiver is never fired.

The event that I tried was the FeatureActivated and FeatureInstalled.

Do I have to set some kind of special properties for the project to make the event handlers work?

Is there any other way to create a blank sharepoint web page and add a custom web part to it programmatically?

I used this tutorial : Create a Web part page with web part

1

You can deploy it with module, follow below article https://amalhashim.wordpress.com/2013/02/14/sharepoint-deploy-webpart-page-using-module/

As well you can create feature in your solution,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);
        }
    }
}
  • The first one did not work, it just deployed the web part as it did the first time I ran it. No page created. The second one, as I said in my first post, is not firing. The debugger is not getting into that point of the code neither. – Laureant Jan 30 '15 at 13:20
0

The previous answer worked, with one minor adjustment

    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.Zone.ID = "Top";
            webpart.Title = "My Webpart";
            wpManager.AddWebPart(webpart,"Top" , 0);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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