Retarget Grouped Listings Web Parts

Posted on Posted in SharePoint 2007, STSADM Commands

When we did our test upgrade I found that "almost" every single page that had a Grouped Listings Web Part (which is basically just a content query web part) had its source changed from showing items from the "Listings" list to instead show all items from the site. Unfortunately there were too many of them to change manually and the gl-retargetcontentquerywebpart command that I had didn’t have sufficient options to restrict the changes to do what I needed (change the web part to use the "Listings" list as the source). So I decided to just create a new command using the gl-retargetcontentquerywebpart code as the basis: gl-retargetgroupedlistingswebpart. The code that actually does the retargetting of the web part is the same code used by the gl-retargetcontentquerywebpart command – I’ve just wrapped the core method with the appropriate looping and filtering constructs:

   1: private static void RetargetGroupedListings(SPWeb web, SPList list, SPFile file, string webPartTitle)
   2: {
   3:  bool wasCheckedOut = true;
   5:  if (!Utilities.EnsureAspx(file.Url, true, false))
   6:   return; // We can only handle aspx and master pages.
   8:  if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
   9:  {
  10:   file.CheckOut();
  11:   wasCheckedOut = false;
  12:   // If it's checked out by another user then this will throw an informative exception so let it do so.
  13:  }
  14:  else if (!Utilities.IsCheckedOutByCurrentUser(file.Item))
  15:  {
  16:   // We don't want to mess with files that are checked out to a different user so skip to the next.
  17:   return;
  18:  }
  19:  bool modified = false;
  21:  SPLimitedWebPartManager manager = null;
  22:  try
  23:  {
  24:   manager = web.GetLimitedWebPartManager(file.ServerRelativeUrl, PersonalizationScope.Shared);
  25:   foreach (WebPart webPart in manager.WebParts)
  26:   {
  27: #pragma warning disable 612
  28:    ListingSummary cqwp = webPart as ListingSummary;
  29: #pragma warning restore 612
  31:    if (cqwp == null)
  32:     continue;
  34:    if (cqwp.DisplayTitle.ToLowerInvariant() == webPartTitle.ToLowerInvariant())
  35:    {
  36:     Guid existingGuid = Guid.Empty;
  37:     if (!string.IsNullOrEmpty(cqwp.ListGuid))
  38:      existingGuid = new Guid(cqwp.ListGuid);
  40:     if (!existingGuid.Equals(list.ID))
  41:     {
  42:      RetargetContentQueryWebPart.AdjustWebPart(cqwp, list, web);
  44:      manager.SaveChanges(cqwp);
  45:      modified = true;
  46:     }
  47:    }
  48:    webPart.Dispose();
  49:   }
  51:  }
  52:  finally
  53:  {
  54:   if (manager != null)
  55:   {
  56:    manager.Web.Dispose();
  57:    manager.Dispose();
  58:   }
  60:   if (modified)
  61:    file.CheckIn("Checking in changes to page layout due to retargeting of grouped listings web part.");
  62:   else if (!wasCheckedOut)
  63:    file.UndoCheckOut();
  65:   if (modified && !wasCheckedOut)
  66:   {
  67:    file.Publish("Publishing changes to page layout due to retargeting of grouped listings web part.");
  68:    if (file.Item.ModerationInformation != null)
  69:     file.Approve("Auto-approving changes to page layout due to retargeting of grouped listings web part.");
  70:   }
  71:  }
  72: }

The syntax of the command can be seen below:

C:\>stsadm -help gl-retargetgroupedlistingswebpart

stsadm -o gl-retargetgroupedlistingswebpart

Retargets grouped listings web parts upgraded from V2 (points web parts with the specified title to the "Listings" list)

        -url <url to search>
        -scope <Farm | WebApplication | Site | Web | Page>
        [-title <title of web parts to search for (default is "Grouped Listings")>]

Here’s an example of how to retarget all the grouped listings web parts with a title of "Grouped Listings" to the appropriate "Listings" list in a web application:

stsadm -o gl-retargetgroupedlistingswebpart -url "http://intranet" -scope webapplication -publish

5 thoughts on “Retarget Grouped Listings Web Parts

  1. The command is already created – just download the code using the link on the top right and install the dll in the GAC and the xml file in the 12 hives config folder.

  2. Hi Gary!
    We are upgrading from V2 to V3. Listings web part data is going into Content query web part. The problem is that it adds big blurred images along with the links. To resolve it, if i try to change the style in the presentation in modify web part, then it looks fine but on clicking the links, it goes to some copyutil.aspx page which is just incorrect. Do you have any solution for this? i would really appreciate it.

  3. There’s a property on the web part called UseCopyUtil – you need to set this to false. You can use my gl-setwebpartstate command to do this or simply export the web part, change the xml and then import.

  4. Hello! I am seeing a behavior similar to what you describe above – however, it is appearing on SP 2010 from working SP 2007 grouped listings web parts.

    Have you encountered something similar? Is there an easy solution for SP 2010?

Comments are closed.