If you’ve read some of my earlier posts you’ll know that I’ve been working on 3 commands to enable replacing content throughout the site in order to deal with the broken links I’ve encountered as a result of moving so many sites around for our upgrade.
The first command was gl-replacefieldvalues and the second command was gl-applyupgradeareaurlmappings. This third command, gl-replacewebpartcontent, is focused on replacing content in various built-in web part types. The code for the command, and even the usage of the command, is similar in many respects to the gl-replacefieldvalues command. There are a series of methods used to handle the different scope options that can be passed in and then a primary method which handles the processing of the various files.
Once the web parts have been identified on the page this primary method calls another method based on the web part type. These methods that take in a web part as a parameter are fairly similar in structure with the core difference being what properties are evaluated and modified as each web part type is considered independently. I’m currently only considering the following web parts: ContentEditorWebPart, PageViewerWebPart, ImageWebPart, SiteDocuments, SummaryLinkWebPart, DataFormWebPart, and ContentByQueryWebPart. The last two are only really helpful when the value you are replacing is a List ID – I use the methods that handle these two web part types in my gl-repairsitecollectionimportedfromsubsite command, gl-convertsubsitetositecollection command, and my gl-moveweb command in order to retarget web parts that were hard coded to point to a specific List ID which would have changed as a result of the move.
There’s quite a bit of code for this command but most of it is more or less cookie cutter so I’ll only show a portion of it. The following code shows the primary decision maker method which determines which web part method should be called. I’ve also included one method showing how I handle the ContentEditorWebPart – remember that each web part will have it’s own way of setting its content.