【问题标题】:Modify Sharepoint non publishing default page content programmaticaly以编程方式修改 Sharepoint 非发布默认页面内容
【发布时间】:2014-08-16 16:53:45
【问题描述】:

我需要修改 100 多个 SharePoint 2010 网站的 default.aspx 页面的内容,以对页面内容进行轻微更改。

由于站点数量众多,我想以编程方式进行,无论是在 PowerShell 中,还是通过编写控制台应用程序或类似的方式。

我发现很多网站/博客描述了如何在页面库中查找默认页面,但这对我不起作用,因为即使这些网站激活了发布功能,也没有文件在 Pages 库中,据我所知,它不是 wiki 页面。

我要解决的问题是主页上列表的 ListViewXml 标记中的格式不正确。它在 url 中有两个?(查询字符串),我只想将 "?RootFolder=" 的每个实例替换为 "&RootFolder="

到目前为止,我已经尝试运行一个 c# 控制台,并且已经能够找到 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 中手动打开页面,进行查找替换,然后单击保存按钮,非常繁琐。

【问题讨论】:

    标签: c# powershell sharepoint sharepoint-2010


    【解决方案1】:

    经过更多的搜索和头部撞击后,我能够解决问题。

    发现我无法实际修改页面的代码。我需要修改页面本身的列表的xml,特别是列表工具栏的xml,更具体地说是视图的工具栏。

    以下大部分代码来自http://www.sharepointchick.com/archive/2009/07/24/programmatically-adjusting-the-toolbar-of-a-listviewwebpart.aspx

    对我有用的最终代码是:

            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", "&amp;RootFolder");
    
                            webPartView.Update();
    
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      相关资源
      最近更新 更多