kamagra how much to take

Getting Safe Value From ListItem In Office 365 And SharePoint Online

Reading a ListItem in SharePoint online is nothing fantastic and is pretty similar to reading list items in the throwback server OM models. Consider the below example that starts with a ListItemCollection. We are reading the value of SomeCrap. The problem here is, what if that returns null? That’s an issue.

  1. ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
  2. list.Context.Load(items);
  3. list.Context.ExecuteQuery();
  4. foreach (ListItem item in items)
  5. {
  6. list.Context.Load(item, x => x["SomeCrap"]);
  7. list.Context.ExecuteQuery();
  8. string someCrap = item["SomeCrap"].ToString();
  9. }

There is an easy extension means to handle this though, where instead of null due to the ToString() it will just give the default string.Empty. Here is the extension and an example of its use.

  1. public static T GetSafeValue<T>(this ListItem item, string fieldName) where T : class
  2. {
  3. object o = item[fieldName];
  4. if (o == null || !(o is T))
  5. {
  6. return null;
  7. }
  8. return (T)o;
  9. }
  11. then its use.
  13. [csharp]
  15. foreach (ListItem listItem in items)
  16. {
  17. string someCrap = listItem.GetSafeValue<string>("SomeCrap");
  18. }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>