private void ValidateNode(NavigationNode qlNode, QuickLaunchNavigationNodeDefinition quickLaunchModel)
        {
            TraceUtils.WithScope(traceScope =>
            {
                var pair = new ComparePair<QuickLaunchNavigationNodeDefinition, NavigationNode>(quickLaunchModel, qlNode);

                traceScope.WriteLine(string.Format("Validating model:[{0}] node:[{1}]", quickLaunchModel, qlNode));

                traceScope.WithTraceIndent(trace => pair
                    .ShouldBeEqual(trace, m => m.Title, o => o.Title)
                    .ShouldBeEqual(trace, m => m.Url, o => o.Url)
                    .ShouldBeEqual(trace, m => m.IsVisible, o => o.IsVisible)
                    .ShouldBeEqual(trace, m => m.IsExternal, o => o.IsExternal));
            });
        }
        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 SPNavigationNode EnsureQuickLaunchNavigationNode(
            SPNavigationNode navigationNode,
            QuickLaunchNavigationNodeDefinition quickLaunchNode)
        {
            var quickLaunch = navigationNode.Children;

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

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

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

            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(SPNavigationNode),
                ObjectDefinition = quickLaunchNode,
                ModelHost = navigationNode
            });

            existingNode.Update();

            return existingNode;
        }
        protected NavigationNode GetRootNavigationNode(
            WebModelHost webModelHost,
            QuickLaunchNavigationNodeDefinition quickLaunchModel)
        {
            NavigationNodeCollection quickLaunch = null;
            var result = GetRootNavigationNode(webModelHost, quickLaunchModel, out quickLaunch);

            return result;
        }
        private NavigationNode EnsureRootQuickLaunchNavigationNode(
            WebModelHost webModelHost,
            QuickLaunchNavigationNodeDefinition quickLaunchModel)
        {
            NavigationNodeCollection quickLaunch = null;

            var context = webModelHost.HostWeb.Context;

            var existingNode = GetRootNavigationNode(webModelHost, quickLaunchModel, out quickLaunch);
            var previousNode = quickLaunch.Count > 0 ? quickLaunch.Last() : null;

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

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

                context.ExecuteQuery();
            }

            existingNode.Title = quickLaunchModel.Title;
            existingNode.Url = quickLaunchModel.Url;
            existingNode.IsVisible = quickLaunchModel.IsVisible;

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

            existingNode.Update();

            context.ExecuteQuery();

            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 GetRootNavigationNode(
            WebModelHost webModelHost,
            QuickLaunchNavigationNodeDefinition quickLaunchModel, out NavigationNodeCollection quickLaunch)
        {
            var web = webModelHost.HostWeb;
            var context = webModelHost.HostWeb.Context;

            quickLaunch = web.Navigation.QuickLaunch;

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

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

            return existingNode;
        }
 public static ModelNode AddQuickLaunchNavigationNode(this ModelNode model, QuickLaunchNavigationNodeDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddQuickLaunchNavigationNode(this ModelNode model, QuickLaunchNavigationNodeDefinition definition)
 {
     return AddQuickLaunchNavigationNode(model, definition, null);
 }
        public void Deploy_QuickNavigation()
        {
            // Step 1, define top navigation nodes
            var home = new QuickLaunchNavigationNodeDefinition
            {
                Title = "Home link",
                Url = "/",
                IsExternal = false
            };

            var google = new QuickLaunchNavigationNodeDefinition
            {
                Title = "Google link",
                Url = "http://google.com",
                IsExternal = true
            };

            // Step 2, define web model and artifact relationships - add feature to the web 
            var model = SPMeta2Model
                             .NewWebModel(web =>
                             {
                                 web
                                   .AddQuickLaunchNavigationNode(home)
                                   .AddQuickLaunchNavigationNode(google);
                             });

            // Step 3, deploy model
            DeployWebModel(model);
        }
        private void ValidateRootQuickLaunchNavigationNode(WebModelHost webModelHost, QuickLaunchNavigationNodeDefinition quickLaunchModel)
        {
            var qlNode = GetRootNavigationNode(webModelHost, quickLaunchModel);

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

            ValidateNode(qlNode, quickLaunchModel);
        }