kamagra how much to take

Remember DateTime Casting With SP.ListItem Modified Property

I had this come up with a client this morning. To get down with it, consider the following client object model code:

  1. ClientContext clientContext = new ClientContext("http://test");
  2. List list = clientContext.Web.Lists.GetByTitle("TestList");
  3. ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
  4. ListItem item = listItems[0];
  6. // pay attention to this!
  7. DateTime modifiedDate = (DateTime)item["Modified"];
  9. // Should use include to only use the properties that are required
  10. clientContext.Load(item);
  11. clientContext.ExecuteQuery();

In the above we have a couple things going on. Firstly, a ClientContext object is instantiated to represent the context for SharePoint objects and operations. Next, we are getting a SP.List object by using the ListCollection.GetByTitle method passing in the list name. Next, we are getting all the items from the list by using the SP.List.GetItems method then passing in the CamlQuery.CreateAllItemsQuery method. Indexing the collection, we are getting a test SP.ListItem. Now this is where the context of this post comes into play. If you are getting the Modified property of the list item, you have to remember to explicit cast to a DateTime type (you could safely cast as well). If you don’t, the value **may** display empty (it’s not consistent across list types)!


One Comment

  1. Mark Short says:

    great post as usual!

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>