Issue with Sharepoint 2010 application page
- by Matt Moriarty
I am relatively new to Sharepoint and am using version 2010. I am having a problem with the following code in an application page I am trying to build:
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();
}
}
}
Everything worked fine up until I added in the 'uProfile.Commit();' line. Now I am getting the following error message:
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:
I have tried to rectify this by adding in code to allow the unsafe updates but I still get this error.
Does anyone have any guidance for me? It would be much appreciated.
Thanks in advance,
Matt.