将文件夹添加到当前文档库

时间:2010-04-02 22:06:37

标签: sharepoint document-library

如何将文件夹添加到当前文档库(如果当前文档库中不存在该文件夹)?

(当前是最终用户的所在地) (我将把这段代码添加到itemAdded事件处理程序中)

2 个答案:

答案 0 :(得分:0)

curSPList.Items.Add("My Folder Name", SPFileSystemObjectType.Folder);

将在任何SharePoint列表中创建一个新文件夹,包括文档库。如果您计划在事件处理程序中实现它,则可以从SPItemEventProperties参数的“List”属性获取对SPList的引用。

答案 1 :(得分:0)

这是最终的代码。如果“uHippo”不存在,则在当前文档库中创建文件夹“uHippo”。

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


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



    }
}