Replace Web Part Content

Posted on Posted in SharePoint 2007, STSADM Commands

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.

   1: /// <summary>

29 thoughts on “Replace Web Part Content

  1. Thanks a lot 🙂
    It’s just what I was looking for – to replace my List IDS in Dataviews after import.
    Nelly.

  2. Great commands! I’ve used several and they have really saved me a lot of time!

    Is there a way to convert the “grouped listings” web part created by migration to the summary links web part? Seems like it would be a simple matter of copying links and groups into the new web part using the listings list as the source.. This would be helpful because we want to be able to provide one set of instructions to our users for updating and maintaining those links..

  3. I don’t have anything that will do that explicitly but you’re right – it would be a fairly simple matter to get the list of links and then just update the summary links web part.

  4. I am having some trouble accessing the content of the summarylink publishing fields that are included on the WelcomeLinks publishing pages.

    I can add and remove links and groups from a summarylinks web part just fine but I have not been able to find a way to manipulate the controls in the “WelcomeLinks” layout page… is this even possible?

  5. Yes – it is possible to manipulate this data. It’s just stored as a page field (a column or field of the page’s list item) and is a pretty simple xml value. If you want to see what the field values look like use the gl-exportlistitem2 command and then you can use something like gl-replacefieldvalues or gl-setmetadata to manipulate that xml.

  6. Hi Gary,

    First of all, thanks for this magnificient tool.

    My question is related to updating List IDs after importing a site.

    Generally I develop POCs. I have sort of a template feature for creating publishing sites with different layouts and themes.

    Steps I follow are as follow:

    1. Create a publishing portal site from template.

    2. Enable Publishing site feature (simply creates master pages, page layouts, web-parts)

    3. Import exported lists (using gl-import, gl-export for this)

    4. Update List IDs in DataForm Web Parts — right at this point I need your help since I tried to use replacecontentwebpart command, but for this you need to know list ids to be replaced.

    In order to update list ids in all web parts (which were created at step 2), what can I do?

    Is there any command that I can make use of for this?

    Many thanks in advance

    zieglers

  7. Thank you very much for the nice commands
    I have an issue with this command. Whenever I issue STSADM -o gl-replacewebpartcontent -url http://xx/sectors/rooms/Pages/default.aspx -scope page -searchstring “(?i:www.acc.xxx.net)” -replacestring “yyy” -publish -logfile “c:\replace.log” -test

    I get just the help screen of the stsadm tool.

    Is this the correct command to replace the links in a custom link summary web part?

  8. Hi Gary,

    Can you please share the complete code for this tool?

    We have not been able to use Settings parameters mentioned.

    Also if you can share some help document on how to use this utility.

  9. you are right. I got the utility working..great piece of work!!!
    Just a small problem.. i am not able to update the links in summary link webpart. Any idea???

  10. I am developing my own custom code to update the summary link webpart through code. I am able to successfully check out the page but when I do webPartmanager.SaveChanges(wp); it throws me an exception “The page is not checked out”. It will be great if you can help me out in this scenario. Looking forward to your help.

  11. hi,

    First of all, this is a great tool.

    I’m planning on using it after a big re-arrangement of sites on my portal.(backup and restore them to a different site structure)

    I want to know if it’s possible to import a file of string and replacement similar to what you update on: gl-replacefildvalues.

    because I got allot of changes I want to go over the webparts only one time and update all of the sites.

    Thanks In Advance

  12. Hey Gary. Thank you for always being so responsive to comments on your blog posts. These tools you’ve developed are invaluable beyond words.

    I’m having a problem with the command gl-replacewebpartcontent. I’m really trying to create a batch command that will change specific links in a web page, replacing spaces in those known links with hyphens.

    I have a CEWP on the page specified in the -url parameter and whenever I execute the command, it says the operation completed successfully but nothing is changed. I’ve made sure all the hyphens were the correct character but to no avail.

    Here’s the command that I enter:

    stsadm -o gl-replacewebpartcontent -url “http://hostname/media-resources/Pages/news-archive.aspx” -scope page -searchstring “(?i:Media%
    20Library/07-1205-Evans Joins Company.pdf)” -replacestring “Media%20Library/07-1
    205-Evans-Joins-Company.pdf” -publish -logfile c:\replace.log

    Any help would be greatly appreciated.

  13. Hi, Gary
    I have added a content editor web part on to the document libary page (you know the one when you navigate to a document library and you have the new, upload, actions and settings buttons). This command doesnt seem to be able to update this CEWP. Is this because the web part is not on a “web” and therefore never gets found. I tried looking for the web part using sharepoint manager but couldnt find it in the site with the document library.

  14. Hi Gary,
    I try to replace a part of a link in a webpart containing a question mark and an ampersand (200&?DocumentKey) with a string containing only an ampersand (200?DocumentKey). But the string cannot be found. Is there a problem with specific signs like & and ? ?
    How can I search for it?

    Thank you in advance

  15. Stefan – I use a regular expression to do the searching which means you’ll need to escape any special characters such as the question mark (can’t remember if the & is one or not) – try adding a backslash in front of each character: 200\&\?DocumentKey.

  16. Hi Gary,

    I need to do a mass change to replace the “Site Image” web part values for the name of the image to be displayed and the Alternative Text . I was wondering if this command would be able to do this. The data looks like it is stored in an xml format. If this command can not do it then do you have one that will.

    Also, replaceing the image content and keeping the established image name is not an option. To be honest this would be fast and easy. But does not change the Alternative Text .

    Thanks in advanced for the help.

  17. I just went to download your script and got a listing of usernames and email addresses instead. BTW – I did migrate the publish items command to a 2010 cmdlet if you’d rather use that.

Leave a Reply

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

*