我有一个包含文档和文件夹的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
}
答案 0 :(得分:0)
我使用自定义扩展方法:
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;
}
答案 1 :(得分:0)
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