0

我修改了代码。我现在可以上传到当前文档库(不再对文档库或实际 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();
        //}

    }
} 
4

1 回答 1

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(); 
}
于 2010-04-07T12:44:07.830 回答