我知道这是 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 封装上述内容:) 希望这会有所帮助:)