1

我有一个已部署为场解决方案的自定义 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();
                    }
                }

        }
4

2 回答 2

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();
        }
于 2012-12-11T08:18:10.150 回答
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;
                        }
                    }

            }
于 2012-12-15T12:23:26.863 回答