3

I'm having problems creating an Alert for the current user programmatically for a GET request. No matter how I try to do it, it says updates are not allowed for GET requests.

var spContext = SPContext.Current;
var web = spContext.Web;
web.AllowUnsafeUpdates = true;
var user = web.CurrentUser;
var alert = user.Alerts.Add();
alert.Title = spContext.ListItem.Title;
alert.AlertType = SPAlertType.Item;
alert.Item = spContext.ListItem;
alert.EventType = SPEventType.All;
alert.Update(false); // error here

I've tried SPSecurity.RunWithElevatedPrivileges as well, after fetching the SPUser, but the result is the same. I've had great success with web.AllowUnsafeUpdates = true in other places.

Am I perhaps allowing unsafe updates on the wrong SPWeb?

1

You cannot do it on Get request, It has to be Post.

If you want to do it on Page load, you can do a callback as soon as page loads and get the job done.

0

I usually create a new SPWeb object inside a using, by use of the ID from the SPContext, that way it's being properly disposed and you know for sure you have the correct web object.

As for your problem; there are some actions which trigger the AllowUnsafeUpdates property to revert back to false again. Although I'm not 100% sure that adding a new alert does this, it might trigger some sort of update on the web object and thereby reset the bit. Just move the line directly above the update statement just to be sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.