0

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
}
2
  • While adding the document, have you identified and select the folder? Added the document inside your desired folder? Commented Oct 15, 2012 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
    Commented Oct 15, 2012 at 9:25

2 Answers 2

1

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.

0

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;
}

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.