Thursday 9 March 2017

Delete a Webpart in sharepoint 2007

 public static void RemoveWebPart()
//        {
//            using (SPSite spSite = new SPSite(""))
//            {
//                using (SPWeb spWeb = spSite.OpenWeb())
//                {
//                    SPList lstdocking = spWeb.Lists["Projects - general"];
//                    SPQuery spQrydocking = new SPQuery();
//                    spQrydocking.Query = @"<Where>
//      <And>
//         <Eq>
//            <FieldRef Name='Project_x0020_type_x002e_' />
//            <Value Type='Text'>Dry docking</Value>
//         </Eq>
//         <IsNotNull>
//            <FieldRef Name='Link' />
//         </IsNotNull>
//      </And>
//   </Where>";
//                    SPListItemCollection itemColl = lstdocking.GetItems(spQrydocking);
//                    foreach (SPListItem item in itemColl)
//                    {
//                        SPFieldUrlValue value = new SPFieldUrlValue(Convert.ToString(item["Link"]));
//                        string URL = value.Url;
//                        if (URL != "" && URL != null)
//                        {
//                            using (SPSite site = new SPSite(URL))
//                            {
//                                using (SPWeb web = site.OpenWeb())
//                                {
//                                    web.AllowUnsafeUpdates = true;
//                                    Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;
//                                    mgr = web.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
//                                    SPLimitedWebPartCollection wpc = mgr.WebParts;
//                                    try
//                                    {
//                                        foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in wpc)
//                                        {

//                                            {
//                                                if (webPart.Title == "IQS_DocumentViewer")
//                                                {
//                                                    using (webPart)
//                                                    {
//                                                        // mgr.DeleteWebPart(webPart);
//                                                        Console.WriteLine("Webpart Deleted : " + web.Name);
//                                                    }
//                                                }
//                                            }
//                                        }
//                                    }
//                                    catch { }
//                                }
//                            }
//                        }
//                    }
//                }
//            }



//        }

No comments:

Post a Comment