Company      |       Articles & Research      |      Services      |      Software      |      Contact

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:

C#:
  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];
  5.  
  6. // pay attention to this!
  7. DateTime modifiedDate = (DateTime)item["Modified"];
  8.  
  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)!

share save 171 16 Remember DateTime Casting With SP.ListItem Modified Property

Related posts:

  1. Remember When Using SPUtility.FormatDate To UTC (Extension Method)
  2. Using FileSystemObjectType.Folder and UnderlyingObjectType in ECMAScript (Client Object Model)
  3. Fix Unhandled Error in Silverlight 2 Application Invalid cross-thread access error
  4. SharePoint ECMAScript (Client Object Model) Empty List Helper
  5. Building Master SPFile WebForm Collections, Remember SPListItems!

1 Comment »

  1. great post as usual!

    Comment by Mark Short — May 3, 2010 @ 6:16 am

RSS feed for comments on this post. TrackBack URL

Leave a comment