0

I am making a visual webpart in SP 2010, and I download the SPListItems from a document library. I need to get the creation date of all the documents and folder items. I can do it successfuly for items that are documents and not folders using:

DateTime now = System.DateTime.Today;

TimeSpan diff = now - item.File.TimeCreated;

But if I try to do this for a item that is a folder, I get an exception about a null object. I think the "File" variable is null value.

Does anyone know how to get the creation date of a folder?

Thanks.

3 Answers 3

2

Use following code to get the creation date of folder:

item.Folder.Item["Created"]
2
  • Isn't it equivalent to item["Created"] directly?
    – Steve B
    Nov 14, 2014 at 16:18
  • Check the answer by @dylan. Looks like that was throwing exception Nov 15, 2014 at 3:06
0

Yes, in your case, most likely the File property of the list item is null, because the list item refers to a folder and not a file.

I don't see anything similar to TimeCreated in the SPFolder properties, so you could get the creation date of a folder by getting the creation date of it's list item, for instance:

DateTime created = null;
if (item.File != null)
{
    created = item.File.TimeCreated;
}
else
{
    created = (DateTime)item["Created"];
}
2
  • when I try that I get the error (8/22/2013 1:49:58 PM) - Error Message: Value does not fall within the expected range. Stacktrace: at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName, Boolean bThrow) at Microsoft.SharePoint.SPListItemCollection.GetColumnNumber(String groupName, Boolean bThrowException) at Microsoft.SharePoint.SPListItemCollection.GetRawValue(String fieldname, Int32 iIndex, Boolean bThrow) at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException)
    – omega
    Aug 22, 2013 at 17:51
  • Looks like there is a created timestamp in hashtable of SPFolder.Properties msdn.microsoft.com/en-us/library/… Aug 22, 2013 at 18:10
0

I'm assuming you have item initiated as SPListItem.Item[some index]. For a folder you need to change that to SPListItem.Folder.Item["Created"]. Give this code a shot.

DateTime created = item.File.TimeCreated;

if (created == null)
{
    created = item.Folder.Item.File.TimeCreated;
}

//rest of your logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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