开发者

How to delete hidden/closed multiple webparts programmatically that falls under a site in Sharepoint 2007

My requirement is开发者_StackOverflow中文版 to delete hidden/unused webparts under a site. Also am not able to get list of all pages under a site. Need Help on this


Folks, find partially code for above question

  • Create object of site and web.

using (SPSite oSite = new>SPSite(cbWebSite.SelectedItem.ToString())) using (SPWeb web = oSite.OpenWeb())

  • Create a datatable to store list of stale,closed/hidden webparts.

dtWebParts = (System.Data.DataTable)CreateDataTableForUnusedWebParts();

 System.Data.DataTable dtWebPartReport = new
  System.Data.DataTable();

        DataColumn dtCol = new DataColumn();
        dtCol.ColumnName = COLUMN_WEBAPP_NAME;
        dtCol.DataType = System.Type.GetType("System.String");
        dtWebPartReport.Columns.Add(dtCol);
        .
        .
        .
  • Call the function to generate report.

        private void generateWebPartReportForWeb(SPWeb oWeb, SPSite oSite)
    {
    
        try
        {
            //If team Site then read homepage
            foreach (SPFile rootFile in oWeb.Files)
            {
                checkPageForWebParts(oWeb, oSite,rootFile);
            }

            foreach (SPList oList in oWeb.Lists)
            {
                if (oList.BaseType == SPBaseType.DocumentLibrary)
                {
                    lstName = oList.Title.ToString();
                    spDocumentLibrary =(SPDocumentLibrary)oWeb.Lists[lstName];
                    if (spDocumentLibrary.Title.ToString() !="Master Page Gallery" &&
                        spDocumentLibrary.Title.ToString() !="dataconnectionLib" &&
                        spDocumentLibrary.Title.ToString() !="Form Templates" &&
                        spDocumentLibrary.Title.ToString() !="Images" &&
                        spDocumentLibrary.Title.ToString() !="List Template Gallery" &
                        spDocumentLibrary.Title.ToString() !="Scripts" &&
                        spDocumentLibrary.Title.ToString() !="Site Template Gallery" &
                        spDocumentLibrary.Title.ToString() !="Style Library" &&
                        spDocumentLibrary.Title.ToString() !="Web Part Gallery" &&
                        spDocumentLibrary.Title.ToString() !="Workflows")
                    {

                        foreach (SPListItem spListItem in spDocumentLibrary.Items)
                        {
                            spFile = spListItem.File;
                            checkPageForWebParts(oWeb, oSite,spFile);
                        }
                    }
                }
            }

            if (oWeb.Webs.Count > 0)
            {
                foreach (SPWeb subsite in oWeb.Webs)
                {
                    generateWebPartReportForWeb(subsite, oSite);
                }
            }

            DataView dv = dtWebParts.DefaultView;
            dv.Sort = "Document Library Path  ASC";
            dataGridView1.DataSource = dtWebParts;
            dataGridView1.Update();

        }
  • Code to Check for webpart in PAGES :
           private void checkPageForWebParts(SPWeb oWeb,SPSite oSite, SPFile spFile)
         {
            try
             {
            string[] strFileName = spFile.Name.Split('.');
            string fileExtension = strFileName[strFileName.Length - 1].ToUpper();
            if (fileExtension.ToUpper() == "ASPX")
            {
                using (SPLimitedWebPartManager wpm = oWeb.GetLimitedWebPartManager(spFile.Url, PersonalizationScope.Shared))
                {
                    foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
                    {
                        if (wp.IsClosed || wp.Hidden)
                        {
                            DataRow dr = dtWebParts.NewRow();
                            if (oSite.WebApplication.Name != null)
                                dr[COLUMN_WEBAPP_NAME] = oSite.WebApplication.Name;
                            if (oSite.Url != null)
                                dr[COLUMN_SITECOLLECTION_NAME] = oSite.Url;
                            if (oWeb.Name != null)
                                dr[COLUMN_SUBSITE_NAME] = oWeb.Name;
                            if (oWeb.Url != null)
                                dr[COLUMN_DOCLIB_PATH] =cbWebApp.SelectedItem.ToString() + spFile.ServerRelativeUrl;//oWeb.Url + "/" + spDocumentLibrary.ToString();
                            if (spFile.Name != null)
                                dr[COLUMN_DOCNAME] = spFile.Name;
                            if (wp.Title != null)
                                dr["WebPart Name"] = wp.Title;
                            if (wp.IsClosed)
                                dr["Hidden/Closed"] = "Closed";
                            else if (wp.Hidden)
                                dr["Hidden/Closed"] = "Hidden";

                            dtWebParts.Rows.Add(dr);
                            dtWebParts.AcceptChanges();
                        }
                    }
                }
            }
        }


This link will be helpful to you.

protected override void CreateChildControls()
    {
      list = new BulletedList();
      WebPartZoneCollection zones = this.WebPartManager.Zones;
      foreach (WebPartZone zone in zones)
      {
        WebPartCollection webparts = zone.WebParts;
        foreach (WebPart webpart in webparts)
        {
          **You can check webpart.hidden property here.**
          list.Items.Add(
          string.Format("{0} ({1}), {2}",
            webpart.Title, webpart.GetType().Name,
            zone.DisplayTitle));
        }
      }
      this.Controls.Add(list);
    } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜