Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a sharepoint document library containing documents and folders (the folders are newly introduced as a new content type derived from folder) and I'm having an event reciever on document added only to my folders. So to recover the parent folder of my newly added document I tried to write this way but it doesn't seem to work well :

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);

    //getting the item (document) newly added to my content type folder
    SPListItem myItem = properties.ListItem;
    //getting the folder containing my doc
    SPFolder myFolder = myItem.File.ParentFolder;
    //other code on my SPFolder
}
share|improve this question
    
While adding the document, have you identified and select the folder? Added the document inside your desired folder? –  Arsalan Adam Khatri Oct 15 '12 at 9:21
    
see I'm making an event handler, so I'm not concerned with the adding ,I'm just dectecting my added document and I'm trying to retrieve its parent folder's name –  Glolita Oct 15 '12 at 9:25

2 Answers 2

You're missing a check if myItem.File is null, so your code will crash when a folder is added, but besides from that it looks ok.

share|improve this answer

Try this:

SPFolder parentFolder = null;
if (spListItem.Folder != null)
{
    parentFolder = spListItem.Folder.ParentFolder;
}
else if (spListItem.File != null)
{
    parentFolder = spListItem.File.ParentFolder;
}
else
{
    SPFile spFile = spListItem.Web.GetFile(spListItem.Url);
    if (spFile != null)
        parentFolder = spFile.ParentFolder;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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