1

here is the problem. I have a form library that uses Infopath as form. It has an event receiver. This was done by someone else. I was trying to get the beforeProperties, afterProperties or ListItem of the item to check the values, but it is not working.

BeforeProperties is working fine in ItemUpdating, but Afterproperties and ListItem do not. they are throwing this error:"Object not set to an instance of an object".

I tried my code in another Form Library that I created myself, everything works OK. it is the same code, but in two different form libraries. they are both using infopath.

So the Form library that is built for the client is showing the same error as above when I use HttpContext.Current.Session. But it is working fine in my Form Librar.

I have no idea what should I do or change in the form library to make my code work as in my test form library.

the code is simple:

public ovverride void ItemUpdating(SPItemEventProperties properties)
{
string beforeProp = properties.BeforeProperties["CurrentOccupant"].ToString(); (this is OK)
string afterProp = properties.AfterProperties["CurrentOccupant"].ToString(); (Error above)
string a = properties.ListItem["CurrentOccupant"].ToString(); (error above)

HttpContext.Current.Session["SomeValue"] = "hello there"; (error above)

//the same code with the same field name works fine in a form library that I created myself.
}

The field name is correct because it is working correctly in beforeProperties.

Please don't come around to tell me that I have to instantiate my object.

Edit:

I saw that fields used in the Form library where I get my error are in gray color, whereas in my test Form library are blue and clickable. Could this be the cause of this error? Does it mean that the data is saved first in the xml and then is saved in the readonly field?

How can I get the data directly from xml file and compare the old and new value?

Thank you.

1
  • Fields showing as links usually mean the fields are implemented as site collection level fields. If they're not links, they are created as list level fields.
    – Jussi Palo
    Apr 2, 2013 at 10:03

2 Answers 2

2

You can't use SPItemEventProperties.AfterProperties in ItemUpdating event receiver as item is being added, e.g. Not Added yet.

What SPItemEventProperties.AfterProperties property gets ?

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

3
  • this link and in other places, like my test with a different form library, state otherwise: synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122 AfterProperties properties Changed value
    – Imir Hoxha
    Apr 2, 2013 at 13:39
  • are you using field's internal name ? , check this link out how this guy solved same problem, go4answers.webhost4life.com/Example/… Apr 2, 2013 at 14:25
  • Muhammad, both properties are available in the ItemUpdated and ItemUdating. The only thing that people should keep in mind is that they should try with InternalName and DisplayName to see which one works for them. In my case it was Display Name.
    – Imir Hoxha
    Apr 4, 2013 at 7:24
1

Problem solved, FINALLY!!!!!! This works for me, but it might not work for others. I had to use the Display Name of the field in both properties. In some posts was stated that I had to use the InternalName for the afterproperties, but for me it was DisplayName in both cases.

public ovverride void ItemUpdating(SPItemEventProperties properties)
{
string beforeProp = properties.BeforeProperties["Current Occupant"].ToString();
string afterProp = properties.AfterProperties["Current Occupant"].ToString();
}

the same thing works for ItemUpdated

public ovverride void ItemUpdating(SPItemEventProperties properties)
{
string beforeProp = properties.BeforeProperties["Current Occupant"].ToString();
string afterProp = properties.AfterProperties["Current Occupant"].ToString();
}

the last post in this link helped me find my own problem with afterproperties. So I decided to use Display name instead of InternalName since I was getting an error all the time.

http://social.technet.microsoft.com/forums/en-US/sharepointdevelopmentprevious/thread/7ae6aaec-c94a-45da-8cb2-c8fedc87c965

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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