我需要修改100多个SharePoint 2010网站的default.aspx页面的内容,以对页面内容进行细微更改。
由于网站的数量,我想以编程方式进行,无论是在PowerShell中,还是通过编程控制台应用程序或类似的东西。
我发现很多网站/博客描述了如何在页面库中查找默认页面,但这对我来说并不起作用,即使网站已激活发布功能, #39;在页面库中没有文件,据我所知,它不是维基页面。
我试图解决的问题是,主页上列表的ListViewXml标记未正确形成。它在url中有两个?(查询字符串),我只想替换"?RootFolder ="的每个实例。 to"& RootFolder ="
到目前为止,我已尝试运行ac#console并且我已经能够找到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中打开页面,执行查找替换,然后单击“保存”按钮,这非常繁琐。
答案 0 :(得分:0)
经过更多搜索和敲击后,我能够解决问题。
发现我无法实际修改页面代码。我需要修改页面本身的列表的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();
}
}
}
}