我修改了代码。我现在可以上传到当前文档库(不再硬编码文档库或实际URL)。我现在需要做的就是确保文件夹是否存在。如果当前文档库中不存在,则创建该文件夹。如果我遇到解决方案,我会继续更新代码。
由于
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPSite currentSite = new SPSite(properties.WebUrl))
using (SPWeb currentWeb = currentSite.OpenWeb())
{ SPListItem oItem = properties.ListItem;
string doclibname = "Not a doclib";
//Gets the name of the document library
SPList doclibList = oItem.ParentList;
if (null != doclibList)
{
doclibname = doclibList.Title;
}
// this section also not working.
// getting Object reference not set to an instance of an object or something like that.
//if (currentWeb.GetFolder("uHippo").Exists == false)
//{
SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "uHippo");
folder.Update();
//}
}
}
答案 0 :(得分:5)
假设“doclibList”是您要在其中创建文件夹的文档库,您可以遍历其中的文件夹并检查是否找到了必要的名称。在检查doclibList是否为空之后,请执行以下操作。
bool foundFolder = false; //Assume it isn't there by default
if (doclibList.Folders.Count > 0) //If the folder list is empty, then the folder definitely doesn't exist.
{
foreach (SPListItem fItem in doclibList.Folders)
{
if (fItem.Title.Equals("uHippo"))
{
foundFolder = true; //Folder does exist, break loop.
break;
}
}
}
if (foundFolder == false)
{
SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "uHippo");
folder.Update();
}