private NavigationNode EnsureNavigationNode(NavigationNodeModelHost navigationNodeModelHost,
                                                    NavigationNodeDefinitionBase quickLaunchNode)
        {
            var navigationNode = navigationNodeModelHost.HostNavigationNode;
            var quickLaunch    = navigationNode.Children;

            var context = navigationNodeModelHost.HostWeb.Context;

            context.Load(quickLaunch);
            context.ExecuteQueryWithTrace();

            var existingNode = LookupNavigationNode(quickLaunch, quickLaunchNode);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingNode,
                ObjectType       = typeof(NavigationNode),
                ObjectDefinition = quickLaunchNode,
                ModelHost        = navigationNodeModelHost
            });

            if (existingNode == null)
            {
                existingNode = quickLaunch.Add(new NavigationNodeCreationInformation
                {
                    Title      = quickLaunchNode.Title,
                    IsExternal = quickLaunchNode.IsExternal,
                    Url        = ResolveTokenizedUrl(navigationNodeModelHost.HostClientContext, quickLaunchNode),
                    AsLastNode = true
                });

                context.ExecuteQueryWithTrace();
            }

            existingNode.Title     = quickLaunchNode.Title;
            existingNode.Url       = ResolveTokenizedUrl(navigationNodeModelHost.HostClientContext, quickLaunchNode);
            existingNode.IsVisible = quickLaunchNode.IsVisible;

            ProcessLocalization(existingNode, quickLaunchNode);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingNode,
                ObjectType       = typeof(NavigationNode),
                ObjectDefinition = quickLaunchNode,
                ModelHost        = navigationNodeModelHost
            });

            existingNode.Update();

            context.ExecuteQueryWithTrace();

            return(existingNode);
        }
        private NavigationNode EnsureQuickLaunchNavigationNode(NavigationNodeModelHost navigationNodeModelHost,
                                                               QuickLaunchNavigationNodeDefinition quickLaunchNode)
        {
            var navigationNode = navigationNodeModelHost.HostNavigationNode;
            var quickLaunch    = navigationNode.Children;

            var context = navigationNodeModelHost.HostWeb.Context;

            context.Load(quickLaunch);
            context.ExecuteQuery();

            var existingNode = GetNavigationNode(navigationNodeModelHost, quickLaunchNode);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingNode,
                ObjectType       = typeof(NavigationNode),
                ObjectDefinition = quickLaunchNode,
                ModelHost        = navigationNodeModelHost
            });

            if (existingNode == null)
            {
                existingNode = quickLaunch.Add(new NavigationNodeCreationInformation
                {
                    Title      = quickLaunchNode.Title,
                    IsExternal = quickLaunchNode.IsExternal,
                    Url        = quickLaunchNode.Url
                });

                context.ExecuteQuery();
            }

            existingNode.Title     = quickLaunchNode.Title;
            existingNode.Url       = quickLaunchNode.Url;
            existingNode.IsVisible = quickLaunchNode.IsVisible;

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingNode,
                ObjectType       = typeof(NavigationNode),
                ObjectDefinition = quickLaunchNode,
                ModelHost        = navigationNodeModelHost
            });

            existingNode.Update();

            context.ExecuteQuery();

            return(existingNode);
        }
        protected NavigationNode GetNavigationNode(
            NavigationNodeModelHost navigationNodeModelHost,
            NavigationNodeDefinitionBase quickLaunchNode)
        {
            var navigationNode = navigationNodeModelHost.HostNavigationNode;
            var quickLaunch    = navigationNode.Children;

            var context = navigationNodeModelHost.HostWeb.Context;

            context.Load(quickLaunch);
            context.ExecuteQueryWithTrace();

            var existingNode = LookupNavigationNode(quickLaunch, quickLaunchNode);

            return(existingNode);
        }
        protected NavigationNode GetNavigationNode(
            NavigationNodeModelHost navigationNodeModelHost,
            QuickLaunchNavigationNodeDefinition quickLaunchNode)
        {
            var navigationNode = navigationNodeModelHost.HostNavigationNode;
            var quickLaunch    = navigationNode.Children;

            var context = navigationNodeModelHost.HostWeb.Context;

            context.Load(quickLaunch);
            context.ExecuteQuery();

            var existingNode = quickLaunch.OfType <NavigationNode>()
                               .FirstOrDefault(n => n.Url == quickLaunchNode.Url);

            return(existingNode);
        }
        private void ValidateQuickLaunchNavigationNode(NavigationNodeModelHost navigationNodeModelHost, QuickLaunchNavigationNodeDefinition quickLaunchModel)
        {
            var qlNode = GetNavigationNode(navigationNodeModelHost, quickLaunchModel);

            ValidateNode(qlNode, quickLaunchModel);
        }