1

So I'm trying to accomplish this kind of functionality on my SharePoint 2010 list:

I have a field of type choice in my list, which has 7 values and i want users not to be able to change the value of that field from values 2,3,4,5,6,7 to value 1.

I've written an event receiver for that list, here's my code:

public override void ItemUpdated(SPItemEventProperties properties)
   {
       base.ItemUpdated(properties);

       string beforeStatus = properties.BeforeProperties["Status"].ToString();
       string afterStatus = properties.AfterProperties["Status"].ToString();

       if (beforeStatus != "1stValue" && afterStatus == "1stValue")
       {
           properties.Cancel = true;
           properties.ErrorMessage = "This isn't allowed.";
       }
   }

I've tried using both ItemUpdated and ItemUpdating event receivers, when I was debugging I saw that the event receiver get's called as it should be, but beforeStatus and afterStatus is getting null from the item in both cases.

So, how can I get the values of the item's field before and after updating correctly? Thanks in advance!

Note: the field's internal and display names are both Status.

Gintas K
  • 1,438
  • 3
  • 18
  • 38

2 Answers2

4

Use ItemUpdating event and then afterproperties contains changed value and ListItem contains original value of a field.

Here you can find info what properties are avaialable in each events.

It is also important how do you edit the list item. If via SharePoint default edit form all columns are present in afterproperties collection, but if you edit an item from custom code (e.g. webpart, event receive) only updated columns are present in that collection.

Edit: For good looking errors you can redirect user to custom error page (which you have to create)

properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; 
properties.RedirectUrl = "/_layouts/MySolution/CustomErrorPage.aspx?Error=" + errorMessage;
Greg
  • 942
  • 10
  • 18
  • I've found the same article and solved this just a couple of minutes before (see my answer) :D Thank you anyway! – Gintas K Sep 13 '13 at 12:46
  • And is it any way to maybe alert some message to user and return to the edit form without saving? :) – Gintas K Sep 13 '13 at 12:49
  • Oh, and one more thing: if user edits some other field so it doesn't get updated either? So then i'll have to add `properties.AfterProperties["Status"] = beforeStatus;` to my code and don't cancel anything? – Gintas K Sep 13 '13 at 12:53
  • I don't think there is a way to go back to edit form and see changed values. But you can create custom edit from (and add some js validation) or create custom save button (which is probably more suitable in your case) – Greg Sep 13 '13 at 12:54
  • Yes you cancel saving operation for all properties. Like I said in previous comment custom save button would be the best solution for you. – Greg Sep 13 '13 at 12:58
1

I've found a solution to this by myself:

According to this article, I've found out that if I want to get both the old and new values, I have to use ItemUpdating event receiver and use properties.ListItem to get old values and properties.AfterProperties to get the new ones.

Although the error message looks awful to users: error

I will try to solve this now :)

Gintas K
  • 1,438
  • 3
  • 18
  • 38