0
votes

I need to modify the content of the default.aspx page of over 100 SharePoint 2010 sites to make a slight change in the content of the page.

Because of the number of sites, I would like to do it programmatically, either in PowerShell, or through programming a console application or something along those lines.

I've found plenty of sites/blogs that describe how to look up the default page in the pages library, but that doesn't work for me as even though the sites have the publishing feature activated, there're no files in the Pages library, and as far as I can tell it's not a wiki page.

The issue I'm trying to fix is that in the ListViewXml tag of lists on the homepage isn't formed right. It has two ?'s (Query strings) in the url, and I just want to replace every instance of "?RootFolder=" to "&RootFolder="

So far I've tried running a c# console and I've been able to locate the default.aspx file, but the content I get isn't all of the content, just the structure.

 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();

            }

I'm just outputting the text to the console for testing purposes. I'm hoping that I can just do a string replace, and the write the content back to the file and save it somehow.

The solution doesn't have to be in C#, I just need some automated way of making the changes over many site home pages and this seemed like the route with the most promise. Currently I'm manually opening the page in SharePoint Designer, doing a find replace and then clicking the save button, which is very tedious.

1

1 Answers

0
votes

After a lot more searching and head banging I was able to resolve the issue.

Found out I'm not able to actually modify the code of the page. I needed to modify the xml of the lists on the pages themselves, specifically the xml of the toolbar of the list, and even more specifically the toolbar of the view.

Most of the following code came from http://www.sharepointchick.com/archive/2009/07/24/programmatically-adjusting-the-toolbar-of-a-listviewwebpart.aspx

Final code that did the trick for me is:

        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();

                    }
                }
            }
        }