簡體   English   中英

我可以在 Sharepoint 中以編程方式將一個 webpart 替換為另一個嗎?

[英]Can I programmatically replace one webpart with another in Sharepoint?

編輯:這已經有十年歷史了,所以很可能與任何人都不相關,如果谷歌把你帶到這里,我會對更現代框架的內容持懷疑態度!

我們有一個使用開箱即用的內容編輯器 webpart 進行大量內容開發的 sharepoint 網站。 當涉及到使用除 IE 之外的任何其他瀏覽器時,這有點垃圾。

我們打算更新到免費的 Telerik 內容編輯器,但是我們希望為自己節省大量的復制和粘貼,以及單擊和選擇來交換 Web 部件。

似乎沒有官方的升級路徑,但在我看來,必須可以使用代碼的力量(tm)來獲取原始 webpart 的內容,新建一個 Telerik webpart 並將內容放入新的 webpart。 .. 然后刪除舊的原始 Web 部件。

有沒有人做過這種事情? 如何最好地布置代碼並實際運行將進行交換的代碼(如果可能的話)。 一個帶有“Do Swap”按鈕的新 webpart? 或者更復雜的東西?

我還需要遍歷每個頁面和子站點(以及子站點中的頁面)並查看每個 Web 部件。 有沒有這樣做的最佳實踐方法?

我會為此編寫一個控制台應用程序。

打開根 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);
            }
        }
    }
}

我對此不是 100% 確定,但是如果您的內容作為單獨的字段保存到列表中,您能否將 Telerik 控件指向保存該內容的位置? 這可能會導致 Telerik 控件在第一次編輯該內容時出現一些問題,但它們應該可以重新格式化內容並恢復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM