Monday, April 7, 2014

Problem with cross site publishing catalog connections when use SPExport/SPImport for copying sites

In my blog I already wrote about various problems on sites after export/import (see e.g. Using SPExport/SPImport for copying content with managed metadata or Problem with SPContextPageInfo.IsWebWelcomePage on imported Sharepoint sites). There was also article about problems with connecting to catalogs on imported sites: Problem with connecting to catalog on site collections imported with SPExport/SPImport API in Sharepoint. In this post I will describe one more problem with publishing catalog connections on imported sites.

Suppose that we have site collection which uses cross-site publishing, i.e. has one or more connected catalogs. After that we export site collection via SPExport and import it to another location via SPImport. Now if we will check available catalog connections on the imported site in Site settings > Manage catalog connections there won’t be any connections. However if we will click Connect to catalog on the same page we will see all connections from the source site. The problem is that they still will point to the source site and it won’t be possible to disconnect them from UI (in the last column where normally there is Connect link, there will be server-relative URL of the source site collection which won’t be clickable).

In order to solve this problem we discussed the following approach with colleagues: if it is not possible to disconnect copied connections from UI, we will try to do it programmatically, and after that will check will it be possible to reconnect them from the target site. My colleague Mikko Niemi created console utility which enumerates all available connections and allows to disconnect them one by one. With his approval I post the code of this utility here:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         var siteUrl = args[0];
   6:  
   7:         Console.WriteLine("Trying to connect site '" + siteUrl + "'...");
   8:  
   9:         using (var site = new SPSite(siteUrl))
  10:         {
  11:             Console.ForegroundColor = ConsoleColor.Green;
  12:             Console.WriteLine("Connected to site '" + site.Url + "'");
  13:             Console.WriteLine();
  14:             Console.ForegroundColor = ConsoleColor.Gray;
  15:  
  16:             while (true)
  17:             {
  18:                 ShowAndDeleteConnections(site);
  19:  
  20:                 Console.WriteLine("To delete more press enter or " +
  21:                     "type q to quit:");
  22:                 var line = Console.ReadLine().ToLower();
  23:                 if (line == "q" || line == "quit")
  24:                 {
  25:                     break;
  26:                 }
  27:             }
  28:  
  29:         }
  30:  
  31:         Console.WriteLine();
  32:         Console.WriteLine("Finished, press key to close!");
  33:         Console.ReadKey();
  34:     }
  35:  
  36:     private static void ShowAndDeleteConnections(SPSite site)
  37:     {
  38:         Console.WriteLine("Fetching catalog connections... ");
  39:  
  40:         var catalogConnectionManager = new CatalogConnectionManager(site);
  41:         var catalogs = catalogConnectionManager.ConnectedPublishingCatalogs;
  42:  
  43:         if (catalogs == null || !catalogs.Any())
  44:         {
  45:             Console.ForegroundColor = ConsoleColor.Yellow;
  46:             Console.WriteLine("No connected catalog connections found!");
  47:             Console.ForegroundColor = ConsoleColor.Gray;
  48:         }
  49:         else
  50:         {
  51:             Console.WriteLine("Connected catalogs:");
  52:             var i = 1;
  53:             foreach (var c in catalogs)
  54:             {
  55:                 Console.ForegroundColor = ConsoleColor.White;
  56:                 Console.WriteLine("{0}. {1} ({2})", i, c.CatalogName,
  57:                     c.CatalogUrl);
  58:                 Console.WriteLine();
  59:                 Console.ForegroundColor = ConsoleColor.Gray;
  60:  
  61:                 i++;
  62:             }
  63:  
  64:             Console.WriteLine("Input the catalog number " +
  65:                 "that you want to delete (empty = no delete):");
  66:             var catalogIndex = Console.ReadLine();
  67:             if (!string.IsNullOrEmpty(catalogIndex))
  68:             {
  69:                 var catalogTodelete = catalogs[int.Parse(catalogIndex) - 1];
  70:                 Console.ForegroundColor = ConsoleColor.Yellow;
  71:                 Console.WriteLine("Deleting connection '" +
  72:                     catalogTodelete.CatalogName + "'...");
  73:                 Console.ForegroundColor = ConsoleColor.Gray;
  74:  
  75:                 catalogConnectionManager.
  76:                     DeleteCatalogConnection(catalogTodelete.CatalogUrl);
  77:                 catalogConnectionManager.Update();
  78:             }
  79:         }
  80:     }
  81: }
   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         var siteUrl = args[0];
   6:  
   7:         Console.WriteLine("Trying to connect site '" + siteUrl + "'...");
   8:  
   9:         using (var site = new SPSite(siteUrl))
  10:         {
  11:             Console.ForegroundColor = ConsoleColor.Green;
  12:             Console.WriteLine("Connected to site '" + site.Url + "'");
  13:             Console.WriteLine();
  14:             Console.ForegroundColor = ConsoleColor.Gray;
  15:  
  16:             while (true)
  17:             {
  18:                 ShowAndDeleteConnections(site);
  19:  
  20:                 Console.WriteLine("To delete more press enter or " +
  21:                     "type q to quit:");
  22:                 var line = Console.ReadLine().ToLower();
  23:                 if (line == "q" || line == "quit")
  24:                 {
  25:                     break;
  26:                 }
  27:             }
  28:  
  29:         }
  30:  
  31:         Console.WriteLine();
  32:         Console.WriteLine("Finished, press key to close!");
  33:         Console.ReadKey();
  34:     }
  35:  
  36:     private static void ShowAndDeleteConnections(SPSite site)
  37:     {
  38:         Console.WriteLine("Fetching catalog connections... ");
  39:  
  40:         var catalogConnectionManager = new CatalogConnectionManager(site);
  41:         var catalogs = catalogConnectionManager.ConnectedPublishingCatalogs;
  42:  
  43:         if (catalogs == null || !catalogs.Any())
  44:         {
  45:             Console.ForegroundColor = ConsoleColor.Yellow;
  46:             Console.WriteLine("No connected catalog connections found!");
  47:             Console.ForegroundColor = ConsoleColor.Gray;
  48:         }
  49:         else
  50:         {
  51:             Console.WriteLine("Connected catalogs:");
  52:             var i = 1;
  53:             foreach (var c in catalogs)
  54:             {
  55:                 Console.ForegroundColor = ConsoleColor.White;
  56:                 Console.WriteLine("{0}. {1} ({2})", i, c.CatalogName,
  57:                     c.CatalogUrl);
  58:                 Console.WriteLine();
  59:                 Console.ForegroundColor = ConsoleColor.Gray;
  60:  
  61:                 i++;
  62:             }
  63:  
  64:             Console.WriteLine("Input the catalog number " +
  65:                 "that you want to delete (empty = no delete):");
  66:             var catalogIndex = Console.ReadLine();
  67:             if (!string.IsNullOrEmpty(catalogIndex))
  68:             {
  69:                 var catalogTodelete = catalogs[int.Parse(catalogIndex) - 1];
  70:                 Console.ForegroundColor = ConsoleColor.Yellow;
  71:                 Console.WriteLine("Deleting connection '" +
  72:                     catalogTodelete.CatalogName + "'...");
  73:                 Console.ForegroundColor = ConsoleColor.Gray;
  74:  
  75:                 catalogConnectionManager.DeleteCatalogConnection(
  76:                     catalogTodelete.CatalogUrl);
  77:                 catalogConnectionManager.Update();
  78:             }
  79:         }
  80:     }
  81: }

With this utility you may disconnect copied “broken” connections and reconnect them again e.g. from UI (after disconnecting near appropriate connection you will see Connect link). Hope that it will help you if you will face with similar problem.

No comments:

Post a Comment