![](/img/trans.png)
[英]Modify the content of the home page programmatically in SharePoint 2010
[英]Modify Sharepoint non publishing default page content programmaticaly
我需要修改100多個SharePoint 2010網站的default.aspx頁面的內容,以對該頁面的內容進行一些更改。
由於站點數量眾多,我想以編程方式(在PowerShell中)或通過對控制台應用程序或類似方式進行編程。
我發現很多網站/博客都描述了如何在頁面庫中查找默認頁面,但這對我不起作用,即使這些站點已激活發布功能,但頁面中也沒有文件圖書館,據我所知這不是維基頁面。
我要解決的問題是首頁列表的ListViewXml標記格式不正確。 它在網址中有兩個?(查詢字符串),我只想將“?RootFolder =”的每個實例替換為“&RootFolder =”
到目前為止,我已經嘗試運行ac#控制台,並且能夠找到default.aspx文件,但是我得到的內容不是全部內容,而只是結構。
using (SPSite siteCollection = new SPSite("my_site_url"))
{
SPWebCollection sites = siteCollection.AllWebs;
foreach (SPWeb site in sites)
{
try
{
SPFile file = site.GetFile(site.Url + "/default.aspx");
System.IO.Stream myStream = file.OpenBinaryStream();
System.IO.StreamReader reader = new System.IO.StreamReader(myStream);
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
finally
{
if (site != null)
site.Dispose();
}
我只是將文本輸出到控制台以進行測試。 我希望我可以做一個字符串替換,然后將內容寫回到文件中並以某種方式保存。
解決方案不必使用C#,我只需要一些自動化的方法即可在許多站點主頁上進行更改,這似乎是最有希望的途徑。 當前,我正在SharePoint Designer中手動打開頁面,進行查找替換,然后單擊“保存”按鈕,這非常繁瑣。
經過更多的搜索和敲打后,我得以解決此問題。
發現我實際上無法修改頁面的代碼。 我需要修改頁面本身上的列表的xml,特別是列表工具欄的xml,甚至是視圖的工具欄。
對我有用的最終代碼是:
SPSite siteCollection1 = new SPSite(<site>);
SPWeb myWebs = siteCollection1.OpenWeb();
foreach (SPWeb webItem in myWebs.Webs)
{
SPFile myFile = webItem.GetFile("default.aspx");
SPLimitedWebPartManager limitedWebPartManager = myFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
// Get all web parts that are available on the default.aspx page
SPLimitedWebPartCollection webParts = limitedWebPartManager.WebParts;
// Loop through the collection of all web parts on the page
foreach (System.Web.UI.WebControls.WebParts.WebPart webPartOnPage in webParts)
{
// Only try to set the toolbar type is the web part is a ListViewWebPart, other web part types don't have toolbars
if (webPartOnPage.GetType().Name == "ListViewWebPart")
{
ListViewWebPart listViewWebPart = webPartOnPage as ListViewWebPart;
// Get the view used in the web part by using reflection
PropertyInfo viewProp = listViewWebPart.GetType().GetProperty("View", BindingFlags.NonPublic | BindingFlags.Instance);
SPView webPartView = viewProp.GetValue(listViewWebPart, null) as SPView;
// This is necessary after the infrastructure update, without it you can't get to the XML of the view
webPartView.GetType().InvokeMember("EnsureFullBlownXmlDocument", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, webPartView, null, System.Globalization.CultureInfo.CurrentCulture);
PropertyInfo nodeProp = webPartView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;
// Get the toolbar node from the XML of the view used in the web part
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
toolbarNode.InnerXml = toolbarNode.InnerXml.Replace("?RootFolder", "&RootFolder");
webPartView.Update();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.