Monday, July 14, 2014

Get a parent folder of SPListItem

There is a difference how to get parent folder for a list item when the list item is in custom list or in document library.
We can use File.ParentFolder property of SPListItem in document library :


SPFolder parentFolder = listItem.File.ParentFolder;


But in custom list the SPListItem.File property is NULL.
And instead of File.ParentFolder we can get a parent folder this way:


SPFolder folder = ((SPListItem)li.FirstUniqueAncestorSecurableObject).Folder;