Add Web Part Programatically on Home / Default Page

Hi,


  • Create a SharePoint 2010 Solution in Visual Studio.
  • Add Feature & its scope.
  • Add Feature Receiver Class & the add below mentioned code :



        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // Get a reference to a web and a list
            SPWeb web = properties.Feature.Parent as SPWeb;
            string pageURL=web.RootFolder.WelcomePage;
            if(pageURL=="")
                pageURL="default.aspx";
            SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL  , System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
            
            // Add the web part
            mgr.AddWebPart(CreatePageWebPart(web.Url), "Left", 1);
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                // Get a reference to a web and a list
                SPWeb web = properties.Feature.Parent as SPWeb;
                string pageURL = web.RootFolder.WelcomePage;
                if (pageURL == "")
                    pageURL = "default.aspx";
                SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                

                for (int i = 0; i < mgr.WebParts.Count; i++ )
                {
                    if (mgr.WebParts[i].ID == "Tasks-My_Tasks")
                    {
                        mgr.DeleteWebPart(mgr.WebParts[i]);
                        break;
                    }
                }
            }
            catch (Exception ex) { string s = ex.ToString(); }
        }


        private XsltListViewWebPart CreatePageWebPart(string webURL)
        {
            // create our List View web part
            XsltListViewWebPart wp = new XsltListViewWebPart();
            using (SPSite site = new SPSite(webURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Tasks"];
                    wp.ID = "Tasks-My_Tasks";
                    wp.ListId = list.ID;
                    wp.ListName = list.ID.ToString();
                    wp.ViewGuid = list.Views["My Tasks"].ID.ToString();
                    wp.XmlDefinition = list.Views["My Tasks"].GetViewXml();

                    // set basic properties along with the title
                    wp.AllowConnect = false ;
                    wp.Title = "My Tasks :";
                    wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.Default ;
                }
            }
            return wp;
        }

Enjoy Codding!!!





Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Change Language for current user with JSOM in SharePoint Online

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

SharePoint 2010 CSS Chart