【问题标题】:Can I programmatically replace one webpart with another in Sharepoint?我可以在 Sharepoint 中以编程方式将一个 Web 部件替换为另一个吗?
【发布时间】:2010-10-21 03:41:33
【问题描述】:

编辑:这已经有十年历史了,所以很可能与任何人都不相关,如果谷歌把你带到这里,我会怀疑内容是否与更现代的框架相悖!

我们有一个使用开箱即用的内容编辑器 Web 部件对内容进行大量开发的共享点网站。使用 IE 以外的任何其他浏览器时,这有点垃圾。

我们打算更新到免费的 Telerik 内容编辑器,但我们希望为自己节省大量的复制和粘贴,以及单击并选择交换 Web 部件。

似乎没有官方的升级路径,但在我看来,必须可以使用代码 (tm) 的力量来获取原始 webpart 的内容,新建一个 telerik webpart 并将内容放入新的 web 部件...然后删除旧的原始 web 部件。

有人做过这种事吗?如何最好地布置代码并实际运行将进行交换的代码(如果可能的话)。一个带有“交换”按钮的新 Web 部件?还是更复杂的东西?

我还需要浏览每个页面和子站点(以及子站点中的页面)并查看每个 Web 部件。有没有这样做的最佳实践方法?

【问题讨论】:

    标签: sharepoint web-parts


    【解决方案1】:

    我会为此编写一个控制台应用程序。

    打开根网站并遍历其子网站。 通过打开 WebPartManager 完成每个页面所需的工作。

    这里有一些伪代码可以帮助您入门。

    string SourceUrl = "http://ThisWeb";
    
    using (SPSite sourceSite = new SPSite(SourceURL))
    {
        using (SPWeb sourceWeb = sourceSite.OpenWeb(SourceURL))
        {
            IterateSubWebsProd(sourceWeb):
        }
    }
    
    private void IterateSubWebsProd(SPWeb sourceWeb)
    {
        // This is the migration function
        DoThingyWithThisWeb(sourceWeb);
    
        foreach (SPWeb subWeb in sourceWeb.Webs)
        {
          IterateSubWebsProd(subWeb);
          subWeb.Dispose();
        }
    }
    
    private void DoThingyWithThisWeb(SPWeb sourceWeb)
    {
        PublishingPage currentPage = null;
    
        string currentPageName = "<something>";
        // Find the pages that you want to modify, with a CAML query for example
        SPQuery query = new SPQuery();
        query.Query = string.Format("" +
        "<Where>" +
          "<Eq>" +
             "<FieldRef Name='FileLeafRef' />" +
             "<Value Type='File'>{0}</Value>" +
          "</Eq>" +
        "</Where>" +
        "", currentPageName);
    
    
         // This codesnippet is from a Publishing web example
         PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(sourceWeb);
        PublishingPageCollection pageColl = publishingWeb.GetPublishingPages(query);
        if (pageColl.Count > 0)
        {
            currentPage = pageColl[0];
        }
    
        using (SPLimitedWebPartManager wpMan = currentPage.ListItem.File.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
        {
            foreach (WebPart wp in wpMan.WebParts)
            {
                if (wp.GetType().Equals(typeof(Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)))
                {
                    Microsoft.SharePoint.WebPartPages.ContentEditorWebPart thisWebPart = wp as Microsoft.SharePoint.WebPartPages.ContentEditorWebPart;
    
                    // This is just dummy code, here you will do your content migration 
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlElement xmlElement = xmlDoc.CreateElement("RootElement");
                    xmlElement.InnerText = sourceItem[SourceField].ToString();
                    thisWebPart.Content = xmlElement;
    
                    wpMan.SaveChanges(thisWebPart);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我对此不是 100% 确定的,但是如果您的内容作为单独的字段保存到列表中,您能否将 Telerik 控件指向该内容的保存位置?这可能会导致 Telerik 控件在第一次编辑该内容时有点吓坏,但它们应该可以重新格式化内容并恢复。

      【讨论】:

        猜你喜欢
        • 2015-09-17
        • 1970-01-01
        • 2010-09-29
        • 2010-09-12
        • 2016-01-04
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多