Question

I've an event receiver and in itemupating i check if i've a value for a lookup field but get an exception. If i look in powershell, the column exist so the first if is ok but he can't test if it's null. I've tried to replace the space by x0020 with no more success this line is working but doesn't get the value: properties.ListItem.Fields["Service box"]

if (properties.ListItem.Fields.ContainsField("Service box"))
        {
            //here is throw the exception
            if (properties.ListItem["Service box"] != null)
            {
                serviceOld = properties.ListItem["Service box"].ToString();
            }
        }

I follow this next post without success: http://blog.vanmeeuwen-online.nl/2012/07/value-does-not-fall-within-expected.html

thanks

Was it helpful?

Solution

you should use the itemupdating afterproperties for this:

if(properties.AfterProperties["Service box"] == null)
{
         //Cancel event
         properties.Cancel = true;
         properties.ErrorMessage = "value cannot be null";
}

EDIT

its todo with the current condition of the item. Your using the event itemUpdating what means that it still hasnt saved and so doesnt exist yet.

If you did itemUpdated than you would see the changes after its saved. Thats why we have AfterProperties for itemUpdating to know what the user has put it to verifiy before its saved.

AfterProperties exist for both updating and updated and contains what the user has put in but before its saved to sharepoint.

the difference is if you use properties.listitem, than in updating it has the old values as its not saved yet but in updated it would have the same value as AfterProperties :) as its saved now so both would be the same. Thats where you would user BeforeProperties.

Gets a hash table of properties consisting of string/value pairs that correspond to fields in the SPItem object after the event occurred.

it should also note before its saved ;) too.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties.afterproperties.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top