2009-11-04 65 views
1

創建新的SharePoint網站,我得到了著名的錯誤「此頁面的安全驗證無效。單擊後退......」,而使用對象模型創建Web站點......錯誤而使用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. 

請任何幫助真正的讚賞。

+0

我得到同樣的錯誤,如果我評論這一行 //CurrentWebApp.FormDigestSettings.Enabled = FALSE; – iJK 2009-11-04 21:58:47

+0

您是否可以使用標準「創建網站」頁面創建具有相同網站定義模板的網站? – axk 2009-11-05 15:09:21

+1

並非所有用戶都有權使用SharePoint UI創建網站。因此他們無法執行網站操作 - >創建網站。我確定使用系統帳戶,我可以使用相同的網站模板創建網站。 我很驚訝它現在正在工作......有些時髦的事情正在發生,我們只是不知道該怎麼做。 – iJK 2009-11-05 15:22:39

回答

1

你在做什麼,阻止了「GET參數有副作用」的異常是SharePoint的對象模型。

但是,「該頁面的安全驗證無效」異常實際上來自Form Digest組件,該組件位於Web應用程序的每個表單頁面中。這個想法是爲每個表單生成一個唯一的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/

+0

我會在頁面加載中調用「SPContext.ValidateFormDigest()」方法嗎? – iJK 2010-01-12 21:52:21

+0

閱讀文章,得到它感謝...讓我們看看它是如何工作 – iJK 2010-01-12 22:28:31

+0

調用此「SPUtility。點擊按鈕頂部的「ValidateFormDigest()」方法解決了問題 – iJK 2010-01-18 22:24:15

0

請試試這個:

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這裏)

+0

我的一個錯誤是我將新網站添加到site.AllWebs集合中,我應該將它添加到objWeb.Webs集合中。 – iJK 2010-01-18 22:23:18

0

驗證下一行

web.AllowUnsafeUpdates = true; 

這一行之前:

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

**web.AllowUnsafeUpdates = true;** 
相關問題