在自定义Web部件中收到错误“GET请求当前不允许更新”

时间:2012-12-10 22:44:23

标签: c# sharepoint web-parts

我有一个自定义的SharePoint Web部件,我已将其部署为服务器场解决方案,并且在写入SharePoint列表时遇到问题。尽管将AllowUnsafeUpdates设置为True(在许多不同的地方)并在调用SPListItem更新之前更新SPWeb对象,但以下是我以编程方式写入列表时遇到的错误消息:

GET请求目前不允许更新。要允许GET上的更新,请在SPWeb上设置“AllowUnsafeUpdates”属性。

我也尝试过使用RunWithElevatedPrivileges,但没有运气。

以下是我的代码供您参考:

        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();
                    }
                }

        }

2 个答案:

答案 0 :(得分:3)

您似乎并未完全理解SPWeb的AllowUnsafeUpdates属性的目标。 您应该在用于SPList实例检索的SPWeb实例上将其设置为true。此外,没有理由创建如此多的SPSite和SPWeb实例,因为它可能会降低性能。对于您的情况,请尝试下一个代码

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();
        }

答案 1 :(得分:0)

你做的朋友

 oSiteCollection.AllowUnsafeUpdates = true;
 oWebsite.AllowUnsafeUpdates = true;

但你永远不会把它们设为,

  oSiteCollection.AllowUnsafeUpdates = false;
  oWebsite.AllowUnsafeUpdates = false;

还有另一件你不应该做的事情是oSiteCollection.AllowUnsafeUpdates = false;

试试这段代码,

    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)
                        {                           
                            oWebsite.AllowUnsafeUpdates = true;               

                            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();
                            oWebsite.Update();
                            oWebsite.AllowUnsafeUpdates = false;
                        }
                    }

            }