Tuesday, July 18, 2006

To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb

UPDATE 20/7/2007 : From Gary comes this solution :

There is a way to accomplish this on a GET method. What you need to be sure to do is after executing the web.AllowUnsafeUpdates = true, you need to execute web.Update() or it doesn't get committed. I used that with no problems. Also, be sure to set the property back to false when you're done with it.
Yeah that's right.. I spend hours and hours today figuring that one out. Let me begin by describing the context what I was trying to achieve. I'm currently busy with creating a webpart that creates a site beneath a portal (/sites/teamsite) by using information coming from a list. So the first thing to achieve was adding a site programmatically using the object model. In the SDK I found two solutions to achieve this: 1. Using the SPWebCollection.Add method 2. Using the SPSiteCollection.Add method. Now, to my believes a site beneath a portal (thus : portal/sites/teamsite), is a sitecollection. So I went for solution 1 ;) (SPWebCollection.Add method can be used to create subsites within a sitecollection) So my code looked like this :
protected override void RenderWebPart(HtmlTextWriter output) { try { SPGlobalAdmin globalAdmin = new SPGlobalAdmin(); SPSiteCollection siteCollections = globalAdmin.VirtualServers[0].Sites; SPSite newSiteCollection = siteCollections.Add("sites/requestsite", "RequestSite", "RequestSite", 1033, "MPS#5", "myusername", "Robin", "myemailaddress"); } catch (Exception error) {output.write(error.Message.ToString();} }
This didn't work because I got the following error: "Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb" So I added the following line:
globalAdmin.AllowUnsafeUpdates = true;
And yet I was still receiving the error so I turned to my favorite friend Google! There I found a lot of users dealing with the same problem as me. But as my search was progressing I stumbled upon : Eric Lim's Things to note: SharePoint AllowUnsafeUpdates Eric had the same problem but had a smart colleague who pointed out that the code must be executed in a postback. Using this knowledge, I created a simple button and moved the code from the RenderWebpart method to the Button.Click method, compiled it, ran stsadm -addwppack for the millionth time, opened the page where the webpart was located.. and .. yes it worked! :) Thanks Eric!

16 comments:

dwarf nebula said...

Hi,

There is a way to accomplish this on a GET method. What you need to be sure to do is after executing the web.AllowUnsafeUpdates = true, you need to execute web.Update() or it doesn't get committed. I used that with no problems. Also, be sure to set the property back to false when you're done with it.

Thanks,
Gary

Robin Meuré said...

Hi Gary,

thanks for sharing that info!
I will mention it in my post!

Michael Creamer said...

Thanks for the information however I'm still getting the same error message.

I've even tried placing my DLL into the GAC.

Is there anything else I'm missing?

thanks!
Michael

Anonymous said...

Please help. I am getting the "Updates are currently disallowed on GET requests.... To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb" error when simply trying to check-out a custom-built WebPart page. Is there a setting in Web.config that can be set for AllowUnsafeUpdates? Please advise. Thank you. -- Mark K.

Robin Meuré said...

And you only get the error if you want to checkout a custom webppart page? Tried to check-out an empty webpart page or a word document?

Unfortunately there is no option in the web.config you can set. Have you read my next post on the problem http://glorix.blogspot.com/2007/08/picture-library-and-unsafe-updates.html ?

What is the configuration you are using?

Anonymous said...

Hi,

I am trying to update a column value of a document library through custom webpart,but getting an error of unsafe updates in the code pasted below. It throws an error while registering an event.

SPWeb Web = SPControl.GetContextWeb(Context);
Web.AllowUnsafeUpdates = true;
Web.Update();

SPList docLib = Web.SPList["XYZ"]; SPDocumentLibrary docLibrary = (SPDocumentLibrary)docLib;

String assemblyName = "CountEvent, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b2a66d62d59ba69e";
String ClassName = "CountEvent.EventCount";
docLibrary.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, ClassName);

Robin Meuré said...

Is your event in the load of the webpart or in the execution of a button?

Anonymous said...

Its in load of the webpart

Anonymous said...

The event is working fine in the execution of the button but want to implement it in the load of the webpart

Robin Meuré said...

Did you try the following code :

SPWeb Web = SPControl.GetContextWeb(Context);
Web.AllowUnsafeUpdates = true;


SPList docLib = Web.SPList["XYZ"]; SPDocumentLibrary docLibrary = (SPDocumentLibrary)docLib;

String assemblyName = "CountEvent, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b2a66d62d59ba69e";
String ClassName = "CountEvent.EventCount";
docLibrary.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, ClassName);

Web.Update();

Anonymous said...

yes

Anonymous said...

It throws an exception while registering an event.

Robin Meuré said...

You got IM? Or an email address? Maybe we can work it out behind the scenes and then post the solution in the comment :)

Anonymous said...
This comment has been removed by a blog administrator.
Aman said...

I am trying to load a web part programatically into a web zone at page_load event of teh page.
I even tried
siteColl.AllowUnsafeUpdates=true; web.AllowUnsafeUpdates = true;
web.Update();

But all in vain :(
Please guide me.

electronic signature said...

A special thanks for this informative post. I definitely learned new stuff here I wasn't aware of !