簡體   English   中英

Sharepoint 2010應用程序頁面出現問題

[英]Issue with Sharepoint 2010 application page

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

我試圖通過添加代碼以允許不安全的更新來解決這個問題,但我仍然得到這個錯誤。

有沒有人對我有任何指導? 非常感謝。

提前致謝,

馬特。

AllowUnsafeUpdates的最佳模式

web.AllowUnsafeUpdates = true;
web.Update(); //remove this line

或檢查您的表格摘要

http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/

嘗試在站點對象上設置allowUnsafeUpdates,它看起來像這樣 -

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

我的猜測是UserProfileManager在網站上創建一個Web對象,並在其使用的Web上繼承該設置。
我希望你已經弄清楚了......

嘗試使用摘要驗證針對SharePoint的頁面,而不是打開不安全的更新(不是一個好的做法)

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

還嘗試對POST請求進行更新,對GET請求進行更新也不是一個好的做法和安全風險

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM