使用 sharepoint 对象模型创建新的 sharepoint 站点时出错

发布于 2024-08-09 18:47:27 字数 5031 浏览 6 评论 0原文

使用对象模型创建 SharePoint 网站时,我收到著名的错误“此页面的安全验证无效。单击“返回”...”...

这是我在 Web 部件中创建共享点网站的代码。

using (SPSite objSite = new SPSite(SPContext.Current.Site.ID))
{
    objSite.AllowUnsafeUpdates = true;
    using (SPWeb objWeb = objSite.OpenWeb(SPContext.Current.Web.ID))
    {
        objWeb.AllowUnsafeUpdates = true;
        SPWeb NewWeb3 = null;
        try
        {
            NewWeb3 = objSite.AllWebs.Add(RelativeITTURL, projectCode, SiteDescription, LocaleID, ITTSiteDefinitionTemplate, false, false);
            NewWeb3.AllowUnsafeUpdates = true;

            NewWeb3.Navigation.UseShared = true;
            NewWeb3.BreakRoleInheritance(true);
            NewWeb3.AllowUnsafeUpdates = true;
            ITTSiteID = new Guid(NewWeb3.ID.ToString());
            NewWeb3.Update();

            NewWeb3.AllowUnsafeUpdates = false;
        }
        catch (Exception ex3)
        {
            StringBuilder Message = new StringBuilder();
            Message.Append(string.Format("Error while creating the site for the 3rd time. '{0}/{1}'\n", _siteCollection, projectCode));
            Message.Append(string.Format("Project Code: {0}\n", projectCode));
            Message.Append(string.Format("ITT Site URL: {0}\n", FullITTSiteURL));
            Message.Append(string.Format("Web Template: {0}\n", _webTemplate));
            Message.Append(string.Format("Web Template To String: {0}\n", ITTSiteDefinitionTemplate.ToString()));
            Message.Append(string.Format("Locale ID: {0}\n", LocaleID.ToString()));
            //Message.Append(string.Format("Form digest validated: {0}\n", ValidateFormDigect.ToString()));

            NFER.ErrorManagement.ErrorLog.LogErrorToEventLog(ex3, Message.ToString());

            lblProvisionStatus.Text = "An error occurred while creating the site. Please close your browser and try again.";
            return;
        }
        finally
        {
            //Since an error can occur
            if (NewWeb3 != null)
                NewWeb3.Dispose();
        }
    }
}

我在这一行收到错误,

CurrentSite.AllWebs.Add...

我可以使用系统帐户成功创建站点,但使用有限的测试帐户会失败。有趣的是,它在某个时间点是有效的,但现在却不起作用了。

这是调用堆栈,

Error Message: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. 
Error Source: Microsoft.SharePoint 
Stack Trace:    at Microsoft.SharePoint.Library.SPRequest.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs)
   at Microsoft.SharePoint.SPSite.CreateWeb(String strUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere, Guid webId, Guid rootFolderId, Boolean createSystemCatalogs)
   at Microsoft.SharePoint.SPSite.SPWebCollectionProvider.CreateWeb(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere)
   at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)
   at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, SPWebTemplate WebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)
   at ProjectManWebParts.ITTStage2DataCapture.<>c__DisplayClass3.<BtnProvisionITT_Click>b__0() 
Target Site: Void CreateWeb(System.String, System.String, System.String, UInt32, System.String, Boolean, Boolean, System.Guid ByRef, System.Guid ByRef, Boolean) 
Data Begin Information 

Data End Information 

Error Message: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. 
Error Source:  
Stack Trace:    at Microsoft.SharePoint.Library.SPRequestInternalClass.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs)
   at Microsoft.SharePoint.Library.SPRequest.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs) 
Target Site: Void CreateWeb(System.String, System.String, System.String, UInt32, System.String, Boolean, Boolean, System.Guid ByRef, System.Guid ByRef, Boolean) 
Data Begin Information 

Data End Information 


 Custom Error Message: Error while creating the site for the 3rd time. 




For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

非常感谢任何帮助。

I get the famous error "The security validation for this page is invalid. Click Back..." while creating SharePoint sites using the object model...

This is my code in the web part to create share point sites.

using (SPSite objSite = new SPSite(SPContext.Current.Site.ID))
{
    objSite.AllowUnsafeUpdates = true;
    using (SPWeb objWeb = objSite.OpenWeb(SPContext.Current.Web.ID))
    {
        objWeb.AllowUnsafeUpdates = true;
        SPWeb NewWeb3 = null;
        try
        {
            NewWeb3 = objSite.AllWebs.Add(RelativeITTURL, projectCode, SiteDescription, LocaleID, ITTSiteDefinitionTemplate, false, false);
            NewWeb3.AllowUnsafeUpdates = true;

            NewWeb3.Navigation.UseShared = true;
            NewWeb3.BreakRoleInheritance(true);
            NewWeb3.AllowUnsafeUpdates = true;
            ITTSiteID = new Guid(NewWeb3.ID.ToString());
            NewWeb3.Update();

            NewWeb3.AllowUnsafeUpdates = false;
        }
        catch (Exception ex3)
        {
            StringBuilder Message = new StringBuilder();
            Message.Append(string.Format("Error while creating the site for the 3rd time. '{0}/{1}'\n", _siteCollection, projectCode));
            Message.Append(string.Format("Project Code: {0}\n", projectCode));
            Message.Append(string.Format("ITT Site URL: {0}\n", FullITTSiteURL));
            Message.Append(string.Format("Web Template: {0}\n", _webTemplate));
            Message.Append(string.Format("Web Template To String: {0}\n", ITTSiteDefinitionTemplate.ToString()));
            Message.Append(string.Format("Locale ID: {0}\n", LocaleID.ToString()));
            //Message.Append(string.Format("Form digest validated: {0}\n", ValidateFormDigect.ToString()));

            NFER.ErrorManagement.ErrorLog.LogErrorToEventLog(ex3, Message.ToString());

            lblProvisionStatus.Text = "An error occurred while creating the site. Please close your browser and try again.";
            return;
        }
        finally
        {
            //Since an error can occur
            if (NewWeb3 != null)
                NewWeb3.Dispose();
        }
    }
}

I get an error at this line

CurrentSite.AllWebs.Add...

I can create sites successfully with the system account but with a limited test account it fails. The funny thing is it was working at one point in time but now it is not.

This is the call stack

Error Message: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. 
Error Source: Microsoft.SharePoint 
Stack Trace:    at Microsoft.SharePoint.Library.SPRequest.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs)
   at Microsoft.SharePoint.SPSite.CreateWeb(String strUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere, Guid webId, Guid rootFolderId, Boolean createSystemCatalogs)
   at Microsoft.SharePoint.SPSite.SPWebCollectionProvider.CreateWeb(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere)
   at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)
   at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, SPWebTemplate WebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)
   at ProjectManWebParts.ITTStage2DataCapture.<>c__DisplayClass3.<BtnProvisionITT_Click>b__0() 
Target Site: Void CreateWeb(System.String, System.String, System.String, UInt32, System.String, Boolean, Boolean, System.Guid ByRef, System.Guid ByRef, Boolean) 
Data Begin Information 

Data End Information 

Error Message: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. 
Error Source:  
Stack Trace:    at Microsoft.SharePoint.Library.SPRequestInternalClass.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs)
   at Microsoft.SharePoint.Library.SPRequest.CreateWeb(String bstrUrl, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, Boolean bCreateUniqueWeb, Boolean bConvertIfThere, Guid& pgWebId, Guid& pgRootFolderId, Boolean bCreateSystemCatalogs) 
Target Site: Void CreateWeb(System.String, System.String, System.String, UInt32, System.String, Boolean, Boolean, System.Guid ByRef, System.Guid ByRef, Boolean) 
Data Begin Information 

Data End Information 


 Custom Error Message: Error while creating the site for the 3rd time. 




For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

Please any help is truly appreciated.

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

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

发布评论

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

评论(3

转身泪倾城 2024-08-16 18:47:27

您正在做的是防止“GET 参数有副作用”异常成为 SharePoint 的对象模型。

然而,异常“此页面的安全验证无效”实际上来自于 Web 应用程序中每个表单页面中的 Form Digest 组件。这个想法是为每个表单生成一个唯一的 ID,并在提交表单时在服务器上验证它。

有时,要解决此问题,值得侵入 HTTPContext.Current 并将“FormDigestValidated”字段设置为 true。像这样: HttpContext.Current.Items["FormDigestValidated"]=true;

编辑
有人说调用 SPUtility.ValidateFormDigest() 会做同样的事情(请参阅http://stephenkaye.blogspot.com/2008/03/form-digest-and-spsecurityrunwithelevat.html)。这听起来比在 HttpContext 中设置字段要少一些“hackish”,

我还发现了一篇有关安全验证等的解释性文章: http://hristopavlov.wordpress.com/2008/05/21/what-you-need-to-know-about- allowunsafeupdates-part-2/

What you are doing, is preventing the "GET parameters are having side effects" exceptions from being SharePoint's object model.

However the exception "The security validation for this page is invalid" actually comes from Form Digest component that lives in every form page in your web app. The idea is to generate a unique ID for each form and to VALIDATE it at the server when the form gets submitted.

Sometimes, to work around this problem, it's worth hacking into HTTPContext.Current and setting the "FormDigestValidated" field to true. Like this: HttpContext.Current.Items["FormDigestValidated"]=true;

EDIT
Some people say that calling SPUtility.ValidateFormDigest() does the same thing (see http://stephenkaye.blogspot.com/2008/03/form-digest-and-spsecurityrunwithelevat.html). This sounds a bit less "hackish" than setting the field in HttpContext

I found also an explanatory article about security validation etc: http://hristopavlov.wordpress.com/2008/05/21/what-you-need-to-know-about-allowunsafeupdates-part-2/

相对绾红妆 2024-08-16 18:47:27

请尝试这个:

Guid webId = Guid.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate() // it's required?
{
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
        try
        {
            site.AllowUnsafeUpdates = true;
            using(SPWeb web = site.AllWebs.Add(RelativeITTURL, projectCode, 
                SiteDescription, LocaleID, siteDefinitionTemplate, false, false))
            {
                web.AllowUnsafeUpdates = true;
                web.Navigation.UseShared = true;
                web.BreakRoleInheritance(true);
                web.AllowUnsafeUpdates = true;
                web.Update();
                webId = new Guid(NewWeb3.ID.ToString());
            }
        }
        catch (Exception ex3)
        {
            // error handler
        }
    }
 });

如果这不起作用,请告诉我(我这里没有 WSS)

Please, try this:

Guid webId = Guid.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate() // it's required?
{
    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
        try
        {
            site.AllowUnsafeUpdates = true;
            using(SPWeb web = site.AllWebs.Add(RelativeITTURL, projectCode, 
                SiteDescription, LocaleID, siteDefinitionTemplate, false, false))
            {
                web.AllowUnsafeUpdates = true;
                web.Navigation.UseShared = true;
                web.BreakRoleInheritance(true);
                web.AllowUnsafeUpdates = true;
                web.Update();
                webId = new Guid(NewWeb3.ID.ToString());
            }
        }
        catch (Exception ex3)
        {
            // error handler
        }
    }
 });

Please let me know if this doesn't works (I'm without WSS here)

云朵有点甜 2024-08-16 18:47:27

下一行:

web.AllowUnsafeUpdates = true;

验证该行之前的

SPSite siteCol = new SPSite(YourSiteURL);
SPWeb web = siteCol.OpenWeb();
SPList list = web.Lists[YourListName];

**web.AllowUnsafeUpdates = true;**

Verify the next line

web.AllowUnsafeUpdates = true;

this line is before:

SPSite siteCol = new SPSite(YourSiteURL);
SPWeb web = siteCol.OpenWeb();
SPList list = web.Lists[YourListName];

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