我可以在 Sharepoint 中以编程方式将一个 Web 部件替换为另一个 Web 部件吗?

发布于 2024-07-17 22:29:55 字数 501 浏览 2 评论 0原文

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

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

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

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

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

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

Edit: This is a decade old so very likely not to be relevant to anyone, if google has brought you here I would be sceptical of the content against more modern frameworks!

We have a sharepoint website that has been quite heavily developed with content using the out of the box content editor webpart. This is a bit rubbish when it comes to using any other browser than IE.

We have in mind to update to the free Telerik content editor, however we would like to save ourselves a large amount of copy and pasting, and clicking and selecting to swap the web parts over.

There seems to be no official upgrade path but it seems to me that it must be possible to use the power of code (tm) to grab the content of the original webpart, new up a telerik webopart and put the content into the new webpart... then delete the old original web part.

Has anyone done this sort of thing? How is it best to lay out the code and actually run the code that will do the swap (if it is possible). A fresh webpart with a "Do Swap" button on? or something more sophisticated?

I also would need to walk through every page and subsite (and page in subsite) and look at every web part. Is there a best practice way of doing this?

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

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

发布评论

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

评论(2

终遇你 2024-07-24 22:29:55

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

打开根 Web 并迭代其子 Web。
通过打开 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);
            }
        }
    }
}

I would write a console application for this.

Open the root Web and iterate through its sub webs.
Do the work needed for each page, by opening the WebPartManager.

Here are some pseudo code to get you started.

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);
            }
        }
    }
}
暖伴 2024-07-24 22:29:55

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

I'm not 100% certain on this, but if your content is saved into a list as individual fields, could you not point the Telerik controls at where that content is saved? This may cause Telerik controls to freak out a bit on the first edit of that content, but they should be ok to re-format the content and recover.

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