Partager via


CatalogConnectionManager - Classe

Gère les connexions du catalogue qui sont utilisées dans une collection de sites.

Hiérarchie d’héritage

System.Object
  Microsoft.SharePoint.Publishing.CatalogConnectionManager

Espace de noms :  Microsoft.SharePoint.Publishing
Assembly :  Microsoft.SharePoint.Publishing (dans Microsoft.SharePoint.Publishing.dll)

Syntaxe

'Déclaration
Public NotInheritable Class CatalogConnectionManager
'Utilisation
Dim instance As CatalogConnectionManager
public sealed class CatalogConnectionManager

Remarques

Connexions de catalogue peuvent être utilisées pour définir les sources de résultats qui peuvent être utilisés dans le composant WebPart de recherche de contenu et d'autres WebParts pilotée par recherche. Les connexions du catalogue également définissent comment les URL des éléments dans un catalogue sont réécrites sur un site.

Exemples

L'exemple suivant contient plusieurs méthodes qui illustrent l'utilisation de la classe CatalogConnectionManager pour créer et connecter les abonnements à un catalogue à partir d'un site de publication.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace CatalogConnectionSample
{
    public class CatalogSubscriber
    {
        string portalSiteUrl;
        string publishingCatalogUrl;
        string subscribingWebServerRelativeUrl;
        /// <summary>
        /// creates a subscription to a catalog from a publishing site.
        /// </summary>
        /// <param name="portalSiteUrl">Absolute Url of the site where a catalog content is rendered</param>
        /// <param name="publishingCatalogUrl">Url of the Library published as a catalog</param>
        /// <param name="subscribingWebServerRelativeUrl">(Optional)Server relative url of a subweb where catalog content is rendered</param>
        public CatalogSubscriber(string publishingCatalogUrl, string portalSiteUrl, string subscribingWebServerRelativeUrl = "/")
        {
            if (string.IsNullOrWhiteSpace(portalSiteUrl) ||
                string.IsNullOrWhiteSpace(publishingCatalogUrl))
            {
                throw new ArgumentNullException("A valid url must be provided for publishingCatalogUrl and portalSiteUrl");
            }
            this.portalSiteUrl = portalSiteUrl;
            this.publishingCatalogUrl = publishingCatalogUrl;
            this.subscribingWebServerRelativeUrl = subscribingWebServerRelativeUrl;
        }

        /// <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();
            }
        }

        public void ConnectToCatalog(string[] orderedPropertiesForURLRewrite, string taxonomyFieldManagedProperty)
        {

            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.
                    orderedPropertiesForURLRewrite,
                    this.subscribingWebServerRelativeUrl,
                    // The managed property which contains the category for the catalog item.
                    // Typically starts with owstaxid, when created by the system.
                    taxonomyFieldManagedProperty);
                // Perform any other adds/Updates
                // ...
                // Finally commit connection changes to store.
                manager.Update();
            }
        }

        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>
        /// Update the url rewrite template for a catalog connection.
        /// The url rewrite template provides the managed properties from the search schema used to rewrite urls to items from the catalog.
        /// </summary>
        /// <param name="newUrlTemplate"></param>
        public void UpdateCatalogUrlTemplate(string newUrlTemplate)
        {
            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);
                // Get the current settings
                CatalogConnectionSettings settings = manager.GetCatalogConnectionSettings(this.publishingCatalogUrl);
                if (settings == null)
                {
                    throw new ArgumentException(string.Format("This site is not connected to catalog {0}", this.publishingCatalogUrl));
                }
                settings.CatalogItemUrlRewriteTemplate = newUrlTemplate;
                // Update in memory
                manager.UpdateCatalogConnection(settings);
                // Perform any other adds/Updates

                // Finally Commit the update(s) to store.
                manager.Update();
            }
        }

        public void DeleteCatalog()
        {
            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))
                {
                    // Get the current settings
                    CatalogConnectionSettings settings = manager.GetCatalogConnectionSettings(this.publishingCatalogUrl);

                    manager.DeleteCatalogConnection(this.publishingCatalogUrl);
                    // Perform any other adds/Updates

                    // Finally Commit the update(s) to store.
                    manager.Update();
                }
            }
        }
    }
}

Cohérence de thread

Tous les membres statique (Partagé dans Visual Basic)s publics de ce type sont thread-safe. Cela n’est pas garanti pour les membres d’instance.

Voir aussi

Référence

CatalogConnectionManager - Membres

Microsoft.SharePoint.Publishing - Espace de noms