public void ConnectToCatalog(string newUrlTemplate, string taxonomyFieldManagedProperty, bool isCustomCatalogItemUrlRewriteTemplate) { using (SPSite publishingPortalSite = new SPSite(this.portalSiteUrl)) { // Get the connection manager for the site where the catalog content will be rendered. CatalogConnectionManager manager = new CatalogConnectionManager(publishingPortalSite); // Use "Contains" to check whether a connection exists. if (manager.Contains(this.publishingCatalogUrl)) { throw new ArgumentException(string.Format("A connection to {0} already exists", this.publishingCatalogUrl)); } // Get the catalog to connect to. CatalogConnectionSettings settings = PublishingCatalogUtility.GetPublishingCatalog(publishingPortalSite, this.publishingCatalogUrl); manager.AddCatalogConnection(settings, // If the items in the catalog should have rewritten urls, specify what properties should be used in rewriting the url. newUrlTemplate, this.subscribingWebServerRelativeUrl, // The managed property which contains the category for the catalog item. // Typically starts with owstaxid, when created by the system. taxonomyFieldManagedProperty, isCustomCatalogItemUrlRewriteTemplate); // Perform any other adds/Updates // ... // Finally commit connection changes to store. manager.Update(); } }
/// <summary> /// Method to get a CatalogConnectionSettings from the site /// </summary> /// <param name="site">The SPSite to get the connection from</param> /// <param name="webAbsoluteUrl">The full absolute url of the catalog</param> /// <param name="catalogWebRelativeUrl">The root url of the catalog.</param> /// <returns>A catalogConnectionSettings object</returns> public CatalogConnectionSettings GetCatalogConnectionSettings(SPSite site, Uri webAbsoluteUrl, Uri catalogWebRelativeUrl) { var tokens = catalogWebRelativeUrl.ToString().Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList(); CatalogConnectionSettings catalogConnectionSettings = null; try { catalogConnectionSettings = PublishingCatalogUtility.GetPublishingCatalog(site, SPUtility.ConcatUrls(webAbsoluteUrl.AbsoluteUri, string.Join("/", tokens))); } catch (InternalQueryErrorException exception) { this.logger.Error("Publishing Catalog with tokens {0} was not found on site {1}", string.Join(", ", tokens.ToArray()), site.Url); this.logger.Exception(exception); } return(catalogConnectionSettings); }
/// <summary> /// Connect without any settings /// A result source will be created and the catalog will be available for content search webparts /// but item urls will have the original path to the source catalog library. /// </summary> public void ConnectToCatalog() { using (SPSite publishingPortalSite = new SPSite(this.portalSiteUrl)) { // Get the connection manager for the site where the catalog content will be rendered. CatalogConnectionManager manager = new CatalogConnectionManager(publishingPortalSite, createResultSource: true); // Use "Contains" to check whether a connection exists. if (manager.Contains(this.publishingCatalogUrl)) { throw new ArgumentException(string.Format("A connection to {0} already exists", this.publishingCatalogUrl)); } // Get the catalog to connect to. CatalogConnectionSettings settings = PublishingCatalogUtility.GetPublishingCatalog(publishingPortalSite, this.publishingCatalogUrl); settings.ConnectedWebServerRelativeUrl = this.subscribingWebServerRelativeUrl; manager.AddCatalogConnection(settings); // Perform any other adds/Updates // ... // Finally commit connection changes to store. manager.Update(); } }