How i can check if the (SPItemEventProperties properties) contain a specific ListItem or not
-
12-12-2020 - |
Question
I am building an event receiver inside my sharepoint 2013, which get fired when items are updated and the ListTemplateId="100"
.
as follow:-
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
if (
( properties.ListItem["ProjectStatus"] != null &&
properties.ListItem["ProjectStatus"] != string.Empty &&
properties.ListItem["ProjectStatus"].ToString().ToLower() == "approved" )
)
{
Now the problem is that inside the sites which have this event receiver enabled,there are multiple custom lists which the event receiver should not fire on.
For that i am checking if the list contain a column named "ProjectStatus
" before modifying the fields inside the event receiver. but the problem I am facing is that let say a custom list item (which does not contain a field named "ProjectStatus"
) got updated, then my event receiver will raise the following exception :-
Column 'ProjectStatus' does not exist. It may have been deleted by another user.
now i thought when i add the following check inside my If statement :-
properties.ListItem["ProjectStatus"] != null
this will prevent the exception from being raised.. but seems this is not true. so my question is how i can check if the properties.ListItem contain a specific column or not ?
Solution
One option is to use ContainsField(string fieldName)
method
properties.ListItem.Fields.ContainsField("displayname of the field")
There is also a Contains
and ContainsFieldWithStaticName
method.
MSDN documentation is here:
https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcollection_members.aspx