Sharepoint 2010应用程序页面出现问题

时间:2011-03-18 13:03:06

标签: c# sharepoint-2010

我对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: 

我试图通过添加代码来解决此问题以允许不安全的更新,但我仍然会收到此错误。

有人对我有任何指导吗?非常感谢。

提前致谢,

3 个答案:

答案 0 :(得分:1)

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/

答案 1 :(得分:1)

尝试在网站对象上设置allowUnsafeUpdates,它看起来像这样 -

site.AllowUnsafeUpdates = true;
UserProfileManager upmanager = new UserProfileManager(ServerContext.GetContext(site)); 

我的猜测是UserProfileManager在网站上创建一个Web对象,并在其使用的Web上继承该设置。
我希望你已经弄清楚了......

答案 2 :(得分:0)

尝试使用digest验证针对SharePoint的页面,而不是打开不安全的更新(不是一个好的做法)

    protected override void OnInit(EventArgs e)
    {
        SPUtility.ValidateFormDigest();
        base.OnInit(e);
    }

还尝试对POST请求进行更新,对GET请求进行更新也不是一个好的做法和安全风险