C# – Getting error “Updates are currently disallowed on GET requests” in custom web part

csharepointweb-parts

I have a custom SharePoint web part that I have deployed as farm solution and am having issues writing to a SharePoint list. Despite setting AllowUnsafeUpdates to True (in many different places) and updating the SPWeb object prior to calling the SPListItem update, the following is the error message I experience when programatically writing to a list:

Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.

I have also tried using RunWithElevatedPrivileges, but no luck.

Here is my code for your reference:

        public void WriteError(string errorTask, string errorMessage)
        {
            string task = "Error Log Entry";
            string fileName = "";
            string fileTitle = "";
            string fileType = "";

                using (SPSite oSiteCollection = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb oWebsite = oSiteCollection.RootWeb)
                    {
                        oSiteCollection.AllowUnsafeUpdates = true;
                        oWebsite.AllowUnsafeUpdates = true;
                        oWebsite.Update();

                        SPSite errorLogSite = new SPSite(oWebsite.ServerRelativeUrl);
                        SPListItemCollection oList = errorLogSite.RootWeb.Lists["ErrorLog"].Items;

                        SPListItem oItem = oList.Add();
                        oItem["ErrorTask"] = task + ": " + errorTask;
                        oItem["ErrorMessage"] = errorMessage;
                        oItem["UserName"] = String.IsNullOrEmpty(UserName) ? "Not Available" : UserName;
                        oItem["FileName"] = String.IsNullOrEmpty(fileName) ? "Not Available" : fileName;
                        oItem["Title"] = String.IsNullOrEmpty(fileTitle) ? "Not Available" : fileTitle;
                        oItem["FileType"] = String.IsNullOrEmpty(fileType) ? "Not Available" : fileType;

                        oItem.Update();
                    }
                }

        }

Best Answer

It seems that you do not fully understand the goal of AllowUnsafeUpdates property of SPWeb. You should set it to true on that SPWeb instance, which you are using for SPList instance retrieving. Also there is no reason to create so much instances of SPSite and SPWeb as it may degrade performance. For your case try next code

public void WriteError(string errorTask, string errorMessage)
        {
            string task = "Error Log Entry";
            string fileName = "";
            string fileTitle = "";
            string fileType = "";

            var errorLogWeb = SPContext.Current.Site.RootWeb;
            errorLogWeb.AllowUnsafeUpdates = true;
            var errorLogList = errorLogWeb.Lists["ErrorLog"];
            SPListItem oItem = errorLogList.Items.Add();
            oItem["ErrorTask"] = task + ": " + errorTask;
            oItem["ErrorMessage"] = errorMessage;
            oItem["UserName"] = String.IsNullOrEmpty(UserName) ? "Not Available" : UserName;
            oItem["FileName"] = String.IsNullOrEmpty(fileName) ? "Not Available" : fileName;
            oItem["Title"] = String.IsNullOrEmpty(fileTitle) ? "Not Available" : fileTitle;
            oItem["FileType"] = String.IsNullOrEmpty(fileType) ? "Not Available" : fileType;

            oItem.Update();
        }
Related Topic