3

I am using the following code to create a folder inside my document library. The event get triggered and executed till the last line of my code without any issues. However the folder is not getting created or listed in my document library.

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);        

    string strDashListRoot = "http://win-hmpjltdbh5q:37642";
    using (SPSite site = new SPSite(strDashListRoot))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;                    

            SPList spl = web.Lists["client_documents"];
            spl.Items.Add("", SPFileSystemObjectType.Folder, "Helllworld");
            spl.Update();
            web.AllowUnsafeUpdates = false;
        }
    }           
}
shytikov
  • 9,155
  • 8
  • 56
  • 103
aarpey
  • 158
  • 1
  • 3
  • 15
  • You can probably use the current context, rather than new-ing up a site/web (likely not your issue though). Are you getting any exceptions, or is it just not being created? Are you sure that the account has sufficient permissions to create a folder? – Servy Oct 01 '12 at 15:45

2 Answers2

5

You need

var i = spl.Items.Add("", SPFileSystemObjectType.Folder, "Helllworld");
i.Update(); 

instead of

spl.Items.Add("", SPFileSystemObjectType.Folder, "Helllworld");
spl.Update();

(assuming your Add call is fine - it looks OK to me)

(Also, are you sure you need the AllowUnsafeUpdates handling? I wouldn't have expected it to be necessary when you're in an ItemAdded handler.)

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Seems strange.. all directories are getting created but not able to see in list view. I can access the directory by name from URL. i.e the folder "Helllworld" exists but not visible in my list. How ever I can see it when I execute http://win-hmpjltdbh5q:37642/client_documents/HelloWorld :( – aarpey Oct 01 '12 at 15:56
  • +1. On `AllowUnsafeUpdates` - it would not be needed if `SPContext.Current.Web` used, but since OP news up SPWeb it needs to be set (i.e. if original call was made during GET with setting `AllowUnsafeUpdates` in another handler). Lack of code that deals with using powerful enough account to update another web is suspicious, but unlikely an issue yet. – Alexei Levenkov Oct 01 '12 at 16:08
  • It would be better in terms of performance to use `AddItem` instead of `Items.Add`, see here: http://sharepoint.stackexchange.com/a/54989/17191 – Mariusz Ignatowicz Apr 05 '17 at 18:56
0

I developed the following code based on Rawling's answer:

private static void CreateFolder(SPWeb web, SPList spList, SPListItem currentItem, string folderName)
{
    if (currentItem.FileSystemObjectType != SPFileSystemObjectType.Folder)
    {
        string itemUrl = web.Url + "/" + currentItem.Url.Substring(0, currentItem.Url.LastIndexOf('/'));

        var folder = spList.Items.Add(itemUrl, SPFileSystemObjectType.Folder, folderName);
        string folderUrl = itemUrl + "/" + folder.Name;

        if (!FolderExists(folderUrl, web))
        {
            try
            {
                folder.Update();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}


public static bool FolderExists(string url, SPWeb web)
{
    if (url.Equals(string.Empty))
    {
        return false;
    }

    try
    {
        return web.GetFolder(url).Exists;
    }
    catch (ArgumentException)
    {
        throw;
    }
    catch (Exception)
    {
        throw;
    }
}
Bartho Bernsmann
  • 2,393
  • 1
  • 25
  • 34