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've bound and ItemUpdating event to a document library of a MOSS publishing site, I’m then trying to compare the Before and After properties.

When I amend the field in the UI I expect the two values to be different, however they are always the same - by that I mean the original value. AfterProperties does not hold the new value.

This is the code I am using…

string internalName = properties.ListItem.Fields["SchemeProvider"].InternalName;

string BeforeValue = properties.BeforeProperties[internalName]);
string AfterValue = properties.AfterProperties[internalName]);

if (BeforeValue != AfterValue)
{
    properties.cancel = true;
    properties.ErrorMessage = “you cannot change this field value”;
}

What I don’t understand is that the ItemUpdated Before and After Properties are correct, in that the values are different.

share|improve this question

2 Answers 2

Have you tried:

string BeforeValue = properties.ListItem[internalName];

instead?

BeforeProperties is a weird beast, I try to avoid using it.

share|improve this answer
    
I did indeed (forgot to include that in the sample) but still no luck. Its enough to drive a person mad. –  Rob Jan 7 '11 at 14:24

First of all, I would recommend having this page open at all times while developing an event receiver. The important part for you, is the Document library table:

Library         BeforeProperties    AfterProperties    properties.ListItem
ItemAdding      No value            No value           Null
ItemAdded       No value            No value           New value
ItemUpdating    Original value      Changed value      Original value
ItemUpdated     Original value      Changed value      Changed value
ItemDeleting    No value            No value           Original value
ItemDeleted     No value            No value           Null

Also, if you are going to try to compare Before/After values, watch out for these types: People fields, rich text fields (Note), and DateTime fields. I'm sure this is probably a subset so make sure you test.

share|improve this answer
    
That's what I found confusing, I was using that page when developing and the table states the AfterProperties value for ItemUpdating is the 'Changed Value' yet my code always has the Original Value, have I missed something? –  Rob Jan 7 '11 at 15:09
    
You know the AfterProperties subscript operator returns an object right? Shouldn't you be casting to a string or calling ToString()? Ex: (string)properties.AfterProperties[internalName] or properties.AfterProperties[internalName].ToString() –  Kit Menke Jan 7 '11 at 19:24

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.