开发者

How to Create a Folder in the Current Document Library if it's not already present?

I modified the code. I can now upload to current document library (not more hardcoding the document library or the acutal url). All i need to do now is to make sure folder exists or not. create folder if it does not exists in the current document library. I will continue to update the code if I came across the solution.

Thanks

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 doclib开发者_如何学CList = 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();
        //}

    }
} 


Assuming that "doclibList" is the Document Library which you want to create the folder in, you could just iterate through the folders in there and check if you find the necessary name. Put the following after your check for the if the doclibList is not null.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜