65.9K
CodeProject 正在变化。 阅读更多。
Home

使用客户端对象模型 (CSOM) 在 SharePoint 2013 Online 中添加/更新/删除快速启动 (当前导航)/全局导航中的节点

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2015年4月30日

CPOL
viewsIcon

21705

本文档介绍了如何使用 .Net Managed CSOM 在 SharePoint 2013 Online 站点中添加/更新/删除站点导航节点。

修改站点导航中的节点是一项非常常见的任务,通常使用 UI 完成。SharePoint 客户端对象模型也允许添加/更新/删除站点导航中的节点。

在使用站点导航中的节点时,我们需要首先获取节点集合。可以通过请求 QuickLaunch 属性(左侧导航)或 TopNavigationBar 属性(顶部导航)的 Navigation 类来实现。

 

NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

NavigationNodeCollection topNavNodeColl = context.Web.Navigation.TopNavigationBar;

在给定的示例中,节点仅被修改在左侧导航中。要修改顶部导航中的节点,必须使用 TopNavigationBar 属性,而不是 QuickLaunch 属性。

添加节点

private static void AddNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Create a new node

            NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();

            newNavNode.Title = "Google";

            newNavNode.Url = "https://www.google.com"; //URL must always start with http/https

            qlNavNodeColl.Add(newNavNode);

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }
        }

这样,节点始终作为集合中的第一个节点添加。要将节点作为最后一个节点添加,应将 NavigationNodeCreationInformationAsLastNode 属性设置为 true

newNavNode.AsLastNode = true;

如果需要将节点添加到特定节点之后,则需要将 PreviousNode 属性设置为要添加新创建节点之后的节点。

private static void AddNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;


            IEnumerable<NavigationNode> twitterNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Twitter"));

            try

            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            //Create a new node

            NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();

            newNavNode.Title = "Google";

            newNavNode.Url = "https://www.google.com"; //URL must always start with http/https

            newNavNode.PreviousNode = twitterNode.FirstOrDefault();

            qlNavNodeColl.Add(newNavNode);

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }
        }

修改现有节点

private static void UpdateNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Fetching node which needs to be updated

            IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            if (googleNode.Count() == 1)
            {
                NavigationNode gNode = googleNode.FirstOrDefault();

                gNode.Url = "https://www.google.co.in";

                gNode.Update();

                try
                {
                    context.ExecuteQuery();
                }
                catch (Exception)
                {
                   //Handle exception
                }
            }
        }

删除节点

private static void DeleteNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Fetching node which needs to be deleted

            IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            if (googleNode.Count() == 1)
            {
                googleNode.FirstOrDefault().DeleteObject();

                try
                {
                    context.ExecuteQuery();
                }
                catch (Exception)
                {
                    //Handle exception
                }
            }
        }
使用客户端对象模型 (CSOM) 在 SharePoint 2013 Online 中添加/更新/删除快速启动 (当前导航)/全局导航中的节点 - CodeProject - 代码之家
© . All rights reserved.