我有一个包含文档和文件夹的 sharepoint 文档库(作为新内容类型引入),并且我有一个仅添加到我的文件夹中的文档的事件接收器所以为了检索我新添加的文档的父文件夹,我尝试以这种方式编写,但它似乎效果不佳:
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
}
来自sharepoint.stackexchange的这个片段帮助了我:
SPFile file = properties.ListItem.Web.GetFile(properties.ListItem.Url);
SPFolder fileFolder = file.ParentFolder;
https://sharepoint.stackexchange.com/questions/7090/get-containing-folder-list-for-spfile-item
我使用自定义扩展方法:
SPFolder myFolder = myItem.GetParentFolder();
// TODO Test myFolder for null, because it could happen in edge cases
其中实现如下:
public static SPFolder GetParentFolder(this SPListItem spListItem)
{
SPFolder parentFolder = null;
// Try to get it from folder
if (spListItem.Folder != null)
{
parentFolder = spListItem.Folder.ParentFolder;
}
// Try to get it from file
else if (spListItem.File != null)
{
parentFolder = spListItem.File.ParentFolder;
}
// At last get file - it is more time/resource consuming
else
{
SPFile spFile = spListItem.Web.GetFile(spListItem.Url);
if (spFile != null)
parentFolder = spFile.ParentFolder;
}
// Can be null
return parentFolder;
}