【问题标题】:Updating the url of a navigation node in moss programatically以编程方式更新 moss 中导航节点的 url
【发布时间】:2016-05-11 22:39:48
【问题描述】:

谁能明白为什么这不起作用:

SPSite topNavigationSite = new SPSite("http://moss");
SPWeb topNavigationWeb = topNavigationSite.OpenWeb();
SPNavigationNodeCollection topNavigationBarNodes = topNavigationWeb.Navigation.TopNavigationBar;
SPNavigationNode updateNode = topNavigationBarNodes.Navigation.GetNodeByUrl("/about");
updateNode.Url = "";
topNavigationWeb.Update();

我可以看到调试 url get 设置为 "" 但是当页面呈现时,导航仍然将 url 显示为 /about/default.aspx

我在 page_load 中运行它,并希望它使用新的 url 值更新 moss 数据库。

【问题讨论】:

    标签: sharepoint navigation


    【解决方案1】:

    我知道这是 3 岁!但由于没有在线更新当前网址的地方!

    我不得不做一些调试,这就是 iv 想出的!顺便说一下,我从

    那里得到了提示
    topNavigationWeb.Update();
    

    表示它正在更新列表!提示提示!

    一点背景!当他们从列表中添加、更新和删除项目时,我想更新列表中的快速链接!在我的列表中,我有两列 Title 和 URL!

    然后我在 VS 2010 中创建了一个项目,它是一个仅连接到该列表的事件接收器(在 elements.xml 文件中完成)

    在 .cs 文件中,我添加了添加项目、删除项目(未删除;))和更新项目

    public override void ItemAdded(SPItemEventProperties 属性)

    public override void ItemUpdated(SPItemEventProperties 属性)

    public override void ItemDeleting(SPItemEventProperties 属性)

    现在您可以在每个方法中简单地调用该方法!

    添加/更新

       public static void AddQuickLaunchItem(string header, string url, SPWeb web)
       {
           SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;
    
           // try to get quick launch header
           SPNavigationNode nodeHeader =
           quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();
    
           //if header not found create it
           if (nodeHeader == null)
           {
               nodeHeader = quickLaunch.AddAsFirst(new SPNavigationNode(header, url,true));
           }
           else
           {
    
               web.AllowUnsafeUpdates = true;
    
    
               nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).First() ;
               nodeHeader.Url = url;
    
               web.AllowUnsafeUpdates = false;
           }
           nodeHeader.Update();
           web.Update();
       }
    

    第一部分是使用标题(标题)检查节点是否存在!我正在比较有哪些标题和列表项:

    SPNavigationNode nodeHeader =
               quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();
    

    这部分是对比:

    n.Title == header
    

    我得到这些值(标题和 url / spweb),如下所示:

    public static void AddQuickLaunchItem(string header, string url, SPWeb web)

    调用上述方法的方法如下所示:

       private void addloopweblinks(SPSite siteCollection, SPItemEventProperties properties)
       {
           // Enumerate through each site and apply branding.
           foreach (SPWeb web in siteCollection.AllWebs)
           {
               AddQuickLaunchItem(properties.ListItem["Title"].ToString(), properties.ListItem["URL"].ToString(), web);
           }
       }
    

    上面的方法在 itemadded 和 itemupdated 中被调用;) 像这样传递值:

       public override void ItemAdded(SPItemEventProperties properties)
       {
               using (SPSite siteCollection = new SPSite(properties.WebUrl))
               {
                   if (siteCollection != null)
                   {
                           addloopweblinks(siteCollection, properties);
                   }
               }
       }
    

    可以为删除做类似的事情;)

    SPNavigationNode nodeHeader = quickLaunch.Cast().Where(n => n.Title == header).FirstOrDefault();

    这将获得节点!

    nodeHeader.delete(); nodeHeader.update();

    这将删除该项目!

    所以在你的情况下你需要什么,我不知道你是否注意到这是这部分:

        web.AllowUnsafeUpdates = true;
    
        nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).First() ;
        nodeHeader.Url = url;
    
        web.AllowUnsafeUpdates = false;
    
        nodeHeader.Update();
        web.Update();
    

    如您所见,我所说的提示是什么意思! nodeHeader.Update();没有更新网址,调试时它确实发生了变化,但是当我进入更新部分时它不起作用:(它点击了我需要web.AllowUnsafeUpdates = true; loool,只是因为我之前遇到过类似的情况!

    对你来说是:

    using(SPSite topNavigationSite = new SPSite("http://moss"))
    {
      using(SPWeb topNavigationWeb = topNavigationSite.OpenWeb())
      {
         web.AllowUnsafeUpdates = true;
         SPNavigationNodeCollection topNavigationBarNodes =topNavigationWeb.Navigation.TopNavigationBar;
         SPNavigationNode updateNode = topNavigationBarNodes.Navigation.GetNodeByUrl("/about");
         updateNode.Url = "";
         updateNode.Update();
         web.Update();
         web.AllowUnsafeUpdates = false;
      }
    }
    

    如果用户没有足够的权限,那么您需要使用 runwithelevatedprivalages 封装上述内容:) 希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      你试过了吗:

      updateNode.Update();
      topNavigationWeb.Update();
      

      您似乎没有更新 SPNavigationNode 对象。 (注意:您可能不需要第二次 Update 调用。)

      【讨论】:

      • Alex,我可以循环查看所有 spnavigationnodes,因为我认为现在它可能设置为“”,因此我不能使用 GetNodeByUrl :)
      • @orl78:是的,您应该能够在 SPNavigationNodeCollection 上执行 foreach 并检查节点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多