如何以编程方式删除 Sharepoint 2007 中某个网站下隐藏/关闭的多个 Web 部件

发布于 2024-10-21 22:50:08 字数 59 浏览 0 评论 0原文

我的要求是删除网站下隐藏/未使用的 Web 部件。 我也无法获取网站下所有页面的列表。 需要这方面的帮助

My requirement is 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-10-28 22:50:08

的部分代码

  • 伙计们,找到上述问题创建网站和网络对象

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

  • 创建一个数据表来存储陈旧、关闭/隐藏的 Web 部件列表。

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

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

        DataColumn dtCol = new DataColumn();
        dtCol.ColumnName = COLUMN_WEBAPP_NAME;
        dtCol.DataType = System.Type.GetType("System.String");
        dtWebPartReport.Columns.Add(dtCol);
        。
        。
        。
  • 调用函数生成报告。

     private voidgenerateWebPartReportForWeb(SPWeb oWeb, SPSite oSite)
    {
    

<前><代码>尝试
{
//如果是团队站点则读取主页
foreach(oWeb.Files 中的 SPFile rootFile)
{
checkPageForWebParts(oWeb, oSite,rootFile);
}

foreach(oWeb.Lists 中的 SPList oList)
{
if (oList.BaseType == SPBaseType.DocumentLibrary)
{
lstName = oList.Title.ToString();
spDocumentLibrary =(SPDocumentLibrary)oWeb.Lists[lstName];
if (spDocumentLibrary.Title.ToString() !="母版页库" &&
spDocumentLibrary.Title.ToString() !="dataconnectionLib" &&
spDocumentLibrary.Title.ToString() !="表单模板" &&
spDocumentLibrary.Title.ToString() !="图像" &&
spDocumentLibrary.Title.ToString() !="列表模板库" &
spDocumentLibrary.Title.ToString() !="脚本" &&
spDocumentLibrary.Title.ToString() !="站点模板库" &
spDocumentLibrary.Title.ToString() !="样式库" &&
spDocumentLibrary.Title.ToString() !="Web 部件库" &&
spDocumentLibrary.Title.ToString() !="工作流程")
{

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

if (oWeb.Webs.Count > 0)
{
foreach(oWeb.Webs 中的 SPWeb 子站点)
{
生成WebPartReportForWeb(子站点,oSite);
}
}

数据视图 dv = dtWebParts.DefaultView;
dv.Sort = "文档库路径ASC";
dataGridView1.DataSource = dtWebParts;
dataGridView1.Update();

}

  • 检查 PAGES 中的 web 部件的代码:
 private void checkPageForWebParts(SPWeb oWeb,SPSite oSite, SPFile spFile)
         {
            尝试
             {
            string[] strFileName = spFile.Name.Split('.');
            字符串文件扩展名 = strFileName[strFileName.Length - 1].ToUpper();
            if (fileExtension.ToUpper() == "ASPX")
            {
                使用 (SPLimitedWebPartManager wpm = oWeb.GetLimitedWebPartManager(spFile.Url, PersonalizationScope.Shared))
                {
                    foreach(wpm.WebParts 中的 System.Web.UI.WebControls.WebParts.WebPart wp)
                    {
                        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 名称"] = wp.Title;
                            if (wp.IsClosed)
                                dr["隐藏/关闭"] = "关闭";
                            else if (wp.Hidden)
                                dr["隐藏/关闭"] = "隐藏";

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

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();
                        }
                    }
                }
            }
        }
季末如歌 2024-10-28 22:50:08

这个链接会对您有所帮助。

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);
    } 

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);
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文