views:

149

answers:

2

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

    }
} 
A: 

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

PERFECTO. THAT'S EXACTLY WHAT I WAS LOOKING FOR. THANKS COMET..

Ross Malai
read how StackOverflow works... this should be a comment on ccomet Answer, and, don't write in CAPS!
balexandre
Okay, Thanks a lot
Ross Malai