Quick and Easy: Get the SPFolder of an SPListItem in an Event Receiver

I hate to admit it, but I struggled with this one all day.  My event receiver needs to update a field of its parent folder.  This little bit shows how to do it:

        private void UpdateParentFolder(SPItemEventProperties properties)
        {

            SPFolder thisItemFolder = properties.ListItem.File.ParentFolder;
            thisItemFolder.Item["ZZ Approval Status"] = "Good news, everyone!";
            thisItemFolder.Item.Update();
           
           
        } // UpdateParentFolder

In this case, I’m working with a document library and the properties are coming from an ItemAdded event.

The trick is that you can’t get the SPFolder of the item directly from the item itself (i.e. properties.ListItem.Folder is null).  Instead, go to the list item’s associated File and get the File’s folder.

</end>

 Subscribe to my blog!

Technorati Tags:

One thought on “Quick and Easy: Get the SPFolder of an SPListItem in an Event Receiver

Leave a Reply

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