MOSS Recursive Search and Replace

23 Aug

Ever find yourself needing to make an edit across every page and every site in your SharePoint 2007 portal? It does seem a bit of a brutal operation, but it has saved me many times doing everything from fixing navigation links to fixing upgrade errors.

I’ve put together a bit of code to recursively iterate through all the site collections and pages in your farm (or below the specified URL), check out the page, do a replace in the page source, and check it back in. This is a heck of a lot faster than sitting in front of SharePoint Designer for days at a time!

The actual find and replace is done something like this. Keep in mind we’ve anticipated the publishing features are enabled, if your not using them you’ll likely have to make some tweaks.

The constructor for the class looks like this, and takes 4 parameters:

BaseURL is the root node at which we will start the replace. Anything below this will be processed. WebApp is the actual string name of the web application in IIS. This was the best way I could find to handle load-balanced farms. For example “SharePoint – 80”, etc. You can modify this to get your Web object however you like.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

And here is the full class I’m using, hopefully it will get you headed towards your own solution. I’m hesitant to provide an executable or full project, as someone is likely to really fry their installation 😉

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Leave a Reply

Your email address will not be published. Required fields are marked *