Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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