/// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static Boolean IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            Boolean result             = false;
            var     navigationSettings = new WebNavigationSettings(web.Context, web);

            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            StandardNavigationSettings targeNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targeNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                (web.ParentWeb.ServerObjectIsNull.HasValue && !web.ParentWeb.ServerObjectIsNull.Value))
            {
                Uri currentWebUri = new Uri(web.Url);
                Uri parentWebUri  = new Uri(String.Format("{0}://{1}{2}", currentWebUri.Scheme, currentWebUri.Host, web.ParentWeb.ServerRelativeUrl));

                using (ClientContext parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targeNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static bool IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            var result             = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);

            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var targetNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targetNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                !web.ParentWeb.ServerObjectIsNull())
            {
                var currentWebUri = new Uri(web.Url);
                var parentWebUri  = new Uri($"{currentWebUri.Scheme}://{currentWebUri.Host}{web.ParentWeb.ServerRelativeUrl}");

                using (var parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targetNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return(result);
        }
        /// <summary>
        /// Returns an editable version of the Global Navigation TermSet for a web site
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">Declares whether to look for Current or Global Navigation</param>
        /// <returns>The editable Global Navigation TermSet</returns>
        public static NavigationTermSet GetEditableNavigationTermSet(this Web web, ManagedNavigationKind navigationKind)
        {
            if (!web.IsManagedNavigationEnabled(navigationKind))
            {
                throw new ApplicationException(String.Format("The current web is not using the Taxonomy provider for {0} Navigation.", navigationKind));
            }

            switch (navigationKind)
            {
            case ManagedNavigationKind.Global:
                return(GetEditableNavigationTermSetByProviderName(web, web.Context,
                                                                  "GlobalNavigationTaxonomyProvider"));

            case ManagedNavigationKind.Current:
                return(GetEditableNavigationTermSetByProviderName(web, web.Context,
                                                                  "CurrentNavigationTaxonomyProvider"));

            default:
                return(null);
            }
        }
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static Boolean IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            Boolean result = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);
            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            StandardNavigationSettings targeNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targeNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                (web.ParentWeb.ServerObjectIsNull.HasValue && !web.ParentWeb.ServerObjectIsNull.Value))
            {
                Uri currentWebUri = new Uri(web.Url);
                Uri parentWebUri = new Uri(String.Format("{0}://{1}{2}", currentWebUri.Scheme, currentWebUri.Host, web.ParentWeb.ServerRelativeUrl));

                using (ClientContext parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targeNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return (result);
        }
        /// <summary>
        /// Returns an editable version of the Global Navigation TermSet for a web site
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">Declares whether to look for Current or Global Navigation</param>
        /// <returns>The editable Global Navigation TermSet</returns>
        public static NavigationTermSet GetEditableNavigationTermSet(this Web web, ManagedNavigationKind navigationKind)
        {
            if (!web.IsManagedNavigationEnabled(navigationKind))
            {
                throw new ApplicationException(String.Format("The current web is not using the Taxonomy provider for {0} Navigation.", navigationKind));
            }

            switch (navigationKind)
            {
                case ManagedNavigationKind.Global:
                    return (GetEditableNavigationTermSetByProviderName(web, web.Context,
                        "GlobalNavigationTaxonomyProvider"));
                case ManagedNavigationKind.Current:
                    return (GetEditableNavigationTermSetByProviderName(web, web.Context,
                        "CurrentNavigationTaxonomyProvider"));
                default:
                    return (null);
            }
        }
        /// <summary>
        /// Determines whether the current Web has the managed navigation enabled
        /// </summary>
        /// <param name="web">The target web.</param>
        /// <param name="navigationKind">The kind of navigation (Current or Global).</param>
        /// <returns>A boolean result of the test.</returns>
        public static bool IsManagedNavigationEnabled(this Web web, ManagedNavigationKind navigationKind)
        {
            var result = false;
            var navigationSettings = new WebNavigationSettings(web.Context, web);
            web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation);
            web.Context.Load(web.ParentWeb, pw => pw.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            var targetNavigationSettings =
                navigationKind == ManagedNavigationKind.Current ?
                navigationSettings.CurrentNavigation : navigationSettings.GlobalNavigation;

            if (targetNavigationSettings.Source == StandardNavigationSource.InheritFromParentWeb &&
                !web.ParentWeb.ServerObjectIsNull())
            {
                var currentWebUri = new Uri(web.Url);
                var parentWebUri = new Uri($"{currentWebUri.Scheme}://{currentWebUri.Host}{web.ParentWeb.ServerRelativeUrl}");

                using (var parentContext = web.Context.Clone(parentWebUri))
                {
                    result = IsManagedNavigationEnabled(parentContext.Web, navigationKind);
                }
            }
            else
            {
                result = targetNavigationSettings.Source == StandardNavigationSource.TaxonomyProvider;
            }

            return (result);
        }