以编程方式更新 moss 中导航节点的 url

发布于 2024-08-05 05:52:04 字数 517 浏览 2 评论 0原文

任何人都可以明白为什么这不应该工作:

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 数据库。

Can anyone see why this should not work:

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();

I can see debugging that the url get's set to "" but when the page renders, the navigation still shows the url as /about/default.aspx

I'm running this in page_load and expected it to update the moss database with the new url value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-08-12 05:52:04

我知道这已经3岁了!但因为网上没有关于更新当前网址的地方!

我必须进行一些调试,这就是 iv 得出的结果!顺便说一下,我得到了提示

topNavigationWeb.Update();

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

一点背景知识!我想在列表中添加、更新和删除项目时更新列表中的快速链接!在我的列表中,有两列“标题”和“URL”!

然后我在 VS 2010 中创建了一个项目,它是一个事件接收器,仅连接到

.cs 文件中的该列表(在 elements.xml 文件中完成),我添加了项目添加、项目删除(未删除;))和项目更新

public override void ItemAdded(SPItemEventPropertiesproperties)

public override void ItemUpdated(SPItemEventPropertiesproperties)

public override void ItemDeleting(SPItemEventPropertiesproperties)

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

添加/更新

   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();
节点头.update();

这将删除该项目!

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

    web.AllowUnsafeUpdates = true;

    nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).First() ;
    nodeHeader.Url = url;

    web.AllowUnsafeUpdates = false;

    nodeHeader.Update();
    web.Update();

正如你所看到的,我的提示是什么意思!节点标题.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 封装上述内容:)希望这会有所帮助:)

I know this is 3years old! but as there is no where online about updating a current url!

I had to do some debugging and this is what iv come up with! By the way I got the hint from

topNavigationWeb.Update();

indicating that its updating a list! hint hint!

a bit of background! I wanted to update the quick links from a list when they add, update and delete an item from the list! On my list I have two columns Title and URL!

I then created a project in VS 2010, its an event receiver that is connected only to that list (done in the elements.xml file)

within the .cs file I added item added, item deleting(not deleted ;) ) and item updated

public override void ItemAdded(SPItemEventProperties properties)

public override void ItemUpdated(SPItemEventProperties properties)

public override void ItemDeleting(SPItemEventProperties properties)

now within each method you can simply call this method!

add/update

   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();
   }

the first part is checking if the node exists using the title (header)! I'm comparing between what headers there are and from the list item:

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

this part is the comparison:

n.Title == header

I'm getting these values (header and url / spweb) like so:

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

the method that is calling the above would look like this:

   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);
       }
   }

and the above method is called within the itemadded and itemupdated ;) passing the values like so:

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

similar things can be done for the delete ;)

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

that will get the node!

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

that will delete the item!

so in your case what you need, I don't know if you noticed it was this part:

    web.AllowUnsafeUpdates = true;

    nodeHeader = quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).First() ;
    nodeHeader.Url = url;

    web.AllowUnsafeUpdates = false;

    nodeHeader.Update();
    web.Update();

as you can see what i mean by the hint! nodeHeader.Update(); didnt update the url, when debugging it does change but when i go to the update part it doesnt work :( and it clicked in i need web.AllowUnsafeUpdates = true; loool only becuse I experienced a similar situation before!

for you it would be:

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;
  }
}

If the user doesnt have the sufficent rights than you need to encapsulate the above with runwithelevatedprivalages :) hope this helps :)

睫毛上残留的泪 2024-08-12 05:52:04

您是否尝试过:

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

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

Have you tried:

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

It doesn't look like you are updating the SPNavigationNode object. (Note: you may not need the second Update call.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文