Remove Web parts from Page Programmatically

Leave a Comment
Hi All,

The below example shows how to delete the web parts from page Programmatically
 . Initially i thought ki it is simple, but I have faced some issues.
Let me share my experience.

My requirement is, I need to delete the existing web parts from the page and need to add the new web parts as per the user selection.

I have faced an issue like I got the count the web parts which are exists in the page,but I didn't get the web part title and all other properties instead I got Error Web part(got to know while debugging the solution)



below code snippet will resolve all issues , which you will face while removing web parts Programmatically.

/// <summary>
        /// Used to delete the existing home page webparts
        /// </summary>
        /// <param name="web">Web Object</param>
        /// <returns>true/false</returns>
        public bool DeletePageWebparts(SPWeb web)
        {
            bool isContextNull = false;
            bool isWebpartsDeelted = false;
            try
            {
                SPFile pageFile = SPContext.GetContext(web).Web.GetFile("Pages/PageName.aspx");
                if (pageFile != null)
                {
                    //checkout the page
                    if (pageFile.CheckOutType == SPFile.SPCheckOutType.None)
                        pageFile.CheckOut();

                    //***************
                    if (HttpContext.Current == null)
                    {
                        isContextNull = true;
                        HttpRequest request = new HttpRequest("", web.Url, "");
                        HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
                        HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
                    }
                    //***************
                    // Removing the webparts from the page by comparing webpart title
                    using (SPLimitedWebPartManager wpm = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
                    {
                        SPLimitedWebPartCollection wpCollection = wpm.WebParts;
                        if (wpCollection.Count > 0)
                        {
                            for (int i = wpCollection.Count; i > 0; i--)
                            {
                                System.Web.UI.WebControls.WebParts.WebPart wp = wpCollection[i - 1];
                                if (wp.Title.Trim().Equals("Title of wp1") ||
                                    wp.Title.Trim().Equals("Title of wp1"))
                                {
                                    wpm.DeleteWebPart(wp);
                                    web.Update();
                                }
                            }
                            isWebpartsDeelted = true;
                        }
                        //Checking the Page
                        pageFile.CheckIn("CheckedIn After webparts deleted");
                        pageFile.Publish("publihsed After webparts deleted");
                    }
                }
                return isWebpartsDeelted;
            }
            catch (Exception ex)
            {
                //Exception Handling
            }
            finally
            {
                if (isContextNull)
                    HttpContext.Current = null;
            }
        }

Related Post

0 comments:

Post a Comment