在提升的安全上下文下进行 SharePoint PublishingWeb 更改失败,为什么?
我在更新 RunWithElevatedPrivileges 下的 SharePoint PublicationWeb 属性时遇到问题。它失败,并出现异常“此页面的安全验证无效”,在这一行:“pubWeb.IncludeInCurrentNavigation = false;”。下面是我尝试运行的代码。通常您可以设置AllowUnsafeUpdates = true,但publishingWeb 没有这个特殊属性。
我的问题是在提升的上下文中更新发布Web 属性的正确方法是什么?
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(parentSiteUrl))
{
//siteCollection.AllowUnsafeUpdates = true;
using (SPWeb web = siteCollection.OpenWeb(subSiteUrl))
{
//web.AllowUnsafeUpdates = true;
if (PublishingWeb.IsPublishingWeb(web))
{
// hide new sub-site from navigation elements.
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
pubWeb.IncludeInCurrentNavigation = false;
pubWeb.IncludeInGlobalNavigation = false;
pubWeb.Update();
}
}
}
});
I'm having trouble updating a SharePoint publishingWeb attribute under RunWithElevatedPrivileges. it fails with the exception "The security validation for this page is invalid" at this line : "pubWeb.IncludeInCurrentNavigation = false;". Below is the code i'm trying to run. Normally you can set AllowUnsafeUpdates = true, but publishingWeb's don't have this special property.
My question is what is the proper way to update publishingWeb attributes in an elevated context?
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(parentSiteUrl))
{
//siteCollection.AllowUnsafeUpdates = true;
using (SPWeb web = siteCollection.OpenWeb(subSiteUrl))
{
//web.AllowUnsafeUpdates = true;
if (PublishingWeb.IsPublishingWeb(web))
{
// hide new sub-site from navigation elements.
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
pubWeb.IncludeInCurrentNavigation = false;
pubWeb.IncludeInGlobalNavigation = false;
pubWeb.Update();
}
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果此更改发生在回发 (POST) 上,则应在进行更改之前调用
SPSecurity.ValidateFormDigest()
。 AllowUnsafeUpdates 仅用于 http GET 请求。如果它是一个 GET 请求,我预计注释掉的行会起作用,但既然它被注释了,我认为它不会起作用。我建议您使用:
因为
PublishingWeb
是SPWeb
实例的包装器,可以通过Web
属性访问它。但这很奇怪,我本来希望提供的SPWeb
是同一个实例(因此您的注释行应该有效。)If this change occurs on a postback (a POST), you should be calling
SPSecurity.ValidateFormDigest()
before you make the change. AllowUnsafeUpdates is only used for http GET requests.If it is a GET request, I would have expected the commented-out line to have worked, but since it's commented I presume it didn't. I would suggest you to use:
as a
PublishingWeb
is a wrapper for anSPWeb
instance, which is accessible via theWeb
property. It's strange though, I would have expected the suppliedSPWeb
to have been the same instance (and as such your commented line should have worked.)正在阅读一些有关使用此属性
pubWeb.Navigation.ExcludeFromNavigation(true, web.ID);
而不是
pubWeb.IncludeInCurrentNavigation = false;
pubWeb.IncludeInGlobalNavigation = false;
不确定这是否与您想要实现的目标相关。
Was reading a bit about using this property
pubWeb.Navigation.ExcludeFromNavigation(true, web.ID);
instead of
pubWeb.IncludeInCurrentNavigation = false;
pubWeb.IncludeInGlobalNavigation = false;
Not sure if that is relevant to what your trying to accomplish.