1

Short and sweet : I'm parsing an XML document and adding new job openings into a sharepoint list as well as deleting them if the job id's and deleting them if the particular job id is not present in the list item collection.

Here's the question:

If i try to query a field value from the item collection that dosent exist, will that cause an error?

 foreach (ListItem listItem in items) 
 { 

     console.WriteLine(listItem["Title"]=="XYZ") // say, there is no item with a title "XYZ" 
  } 
  • Are you asking what will happen if the listItem object does not have a value for Title or what will happen if the loop completes without finding a listItem who's Title equals "XYZ"? – Rob Windsor May 7 '14 at 13:36
  • yes , I want to know what will happen if the listItem object does not have a value for Title – Aman Gupta May 8 '14 at 9:53
2

If the target list does not have a field (column) named Title you will get a PropertyOrFieldNotInitialized exception. To avoid this exception you can do a check on the FieldValues property which is a Dictionary of all the field names and their associated values.

if (item.FieldValues.ContainsKey("Title"))
{
    var value = item["Title"];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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