Search This Blog

Wednesday, May 4, 2016

How to remove the webpart in the site pages of sharepoint 2013 progrmmatically

Here i have given a sample code to remove the webpart in the pages of sharepoint 2013 programmatically


 RemoveWebPart(oSPWeb, "http://TestServer/sites/Test/Sitepages/Home.aspx","Test details");

///<summary> /// Remove a web part based on its title
        /// </summary>
        ///Web site to remove the web part from
        ///Web page to remove the web part within
        ///Title of the web part(s) to remove
        public static void RemoveWebPart(SPWeb web, string page, string webPartTitle)
        {
            try
            {
                using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(web.Url + page, PersonalizationScope.Shared))
                {
                    IEnumerable webPartList = from System.Web.UI.WebControls.WebParts.WebPart webPart in webPartManager.WebParts
                                              where webPart.Title == webPartTitle
                                              select webPart;

                    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartList)
                    {
                        Console.WriteLine("Removing Webpart" + webPartTitle + " in " + web.Url + page + " Please wait...");
                        webPartManager.DeleteWebPart(webPart);
                        break;
                    }

                    web.Update();
                }

            }
            catch (Exception ex)
            {
                     Console.WriteLine("Exception =" + ex.ToString() );
            }
        }

No comments:

Post a Comment