views:

263

answers:

2

Hello everyone,

I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I have deployed a publishing portal. I am developing a ASP.Net web application using VSTS 2008 + C# + .Net 3.5 + ASP.Net + SharePoint Server 2007 SDK.

Here is my code snippets and I got error -- "Updates are currently disallowed on GET requests". Any ideas how to fix?

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

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteCollections = webApp.Sites;
                foreach (SPSite item in siteCollections)
                {
                    SPWeb webItem = item.OpenWeb();
                    webItem.AllowUnsafeUpdates = true;
                }

                SPSite newSiteCollection = siteCollections.Add("sites/myblog",
                    "New Sample Site", "New Sample Site Description", 1033, "BLOG#0",
                    "foo\\foouser", "Owner name", "[email protected]");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString() + "\t" + ex.StackTrace);
            }
        }

thanks in advance, George

+1  A: 

Add a check to ensure that you are getting a POST instead of a GET before you attempt to allow updates. Make sure that whatever is making the change does it via a POST request rather than using URL parameters and a GET.

if (IsPostBack)
{
   ...
}
tvanfosson
Add this if check to where?
George2
Wrap everything inside the `try` block I would imagine, though if the request is being sent as a get, this will simply mean that it does nothing -- no exception, but no code executed either.
tvanfosson
Why get method can not be used? I think from the error message, it means get can be used when we enable AllowUnsafeUpdates. Any comments?
George2
+1  A: 

The problem why you are not allowed to read/write to database on GET request is because your code will be exploitable via a cross-site scripting. Read about AllowUnsafeUpdates consequences here.

Anyway, if you like, you can set this SPWeb.AllowUnsafeUpdates to true, but use it like this:

try {
  web.AllowUnsafeUpdates = true;
  ...
}
finally {
  web.AllowUnsafeUpdates = false;
}
Janis Veinbergs
DO you mean if I put your code into try/catch block in Page_Load of aspx?
George2
Yes, you wrap this code around where you call .Add method of SPSiteCollection object.And do not loop SPWeb objects setting AllowUnsafeUpdates to true (and you don't even call SPWeb.Dispose). See here: http://msdn.microsoft.com/en-us/library/aa973248.aspxSPSite iteself has AllowUnsafeUpdates property - you should probably use that.
Janis Veinbergs
Thanks, question answered!
George2