我对 Sharepoint 比较陌生,使用的是 2010 版。我在尝试构建的应用程序页面中遇到以下代码问题:
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Utilities;
namespace SharePointProject5.Layouts.SharePointProject5
{
public partial class ApplicationPage1 : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
SPContext context = SPContext.Current;
StringBuilder output = new StringBuilder();
using(SPSite site = context.Site)
using (SPWeb web = site.AllWebs["BDC_SQL"])
{
UserProfileManager upmanager = new UserProfileManager(ServerContext.GetContext(site));
string ListMgr = "";
string ADMgr = "";
bool allowUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
SPListCollection listcollection = web.Lists;
SPList list = listcollection["BDC_SQL"];
foreach (SPListItem item in list.Items)
{
output.AppendFormat("<br>From List - Name & manager: {0} , {1}", item["ADName"], item["Manager_ADName"]);
UserProfile uProfile = upmanager.GetUserProfile(item["ADName"].ToString());
output.AppendFormat("<br>From Prof - Name & manager: {0} , {1}", uProfile[PropertyConstants.DistinguishedName], uProfile[PropertyConstants.Manager]);
ListMgr = item["Manager_ADName"].ToString();
ADMgr = Convert.ToString(uProfile[PropertyConstants.Manager]);
if (ListMgr != ADMgr)
{
output.AppendFormat("<br>This record requires updating from {0} to {1}", uProfile[PropertyConstants.Manager], item["Manager_ADName"]);
uProfile[PropertyConstants.Manager].Value = ListMgr;
uProfile.Commit();
output.AppendFormat("<br>This record has had its manager updated");
}
else
{
output.AppendFormat("<br>This record does not need to be updated");
}
}
web.AllowUnsafeUpdates = allowUpdates;
web.Update();
}
Label1.Text = output.ToString();
}
}
}
在我添加“uProfile.Commit();”之前一切正常线。现在我收到以下错误消息:
Microsoft.SharePoint.SPException was unhandled by user code
Message=Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.
Source=Microsoft.SharePoint
ErrorCode=-2130243945
NativeErrorMessage=FAILED hr detected (hr = 0x80004005)
NativeStackTrace=""
StackTrace:
at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
at Microsoft.SharePoint.Library.SPRequest.ValidateFormDigest(String bstrUrl, String bstrListName)
at Microsoft.SharePoint.SPWeb.ValidateFormDigest()
at Microsoft.Office.Server.UserProfiles.UserProfile.UpdateBlobProfile()
at Microsoft.Office.Server.UserProfiles.UserProfile.Commit()
at SharePointProject5.Layouts.SharePointProject5.ApplicationPage1.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase.OnLoad(EventArgs e)
at Microsoft.SharePoint.WebControls.LayoutsPageBase.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: System.Runtime.InteropServices.COMException
Message=<nativehr>0x80004005</nativehr><nativestack></nativestack>Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.
Source=""
ErrorCode=-2130243945
StackTrace:
at Microsoft.SharePoint.Library.SPRequestInternalClass.ValidateFormDigest(String bstrUrl, String bstrListName)
at Microsoft.SharePoint.Library.SPRequest.ValidateFormDigest(String bstrUrl, String bstrListName)
InnerException:
我已尝试通过添加代码来纠正此问题以允许不安全的更新,但我仍然收到此错误。
有人给我指导吗?将不胜感激。
提前致谢
马特。
最佳答案
Best Pattern for AllowUnsafeUpdates
web.AllowUnsafeUpdates = true;
web.Update(); //remove this line
或者检查你的表单摘要
http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/
关于c# - Sharepoint 2010 应用程序页面问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352347/