1
votes

in SharePoint Server 2010 i have a document library and i want to create sub-folders every time a folder created, i have a code snippet but it's not working, i tried to debug but also the event isn't firing, could any one help me please and here is my code:

public class EventReceiver1 : SPItemEventReceiver
{
    /// <summary>
    /// An item was added.
    /// </summary>
    private string[] subFolders = new string[] { "sub-folder1", "sub-folder2", "sub  folder3" };
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        SPWeb web = properties.OpenWeb();
        SPDocumentLibrary ProductsLibrary = (SPDocumentLibrary)web.Lists[properties.ListId];
        if (properties.ListItem.ContentType.Name.ToLower() == "new content type" && properties.ListItem.Folder.ParentFolder.ToString() == ProductsLibrary.RootFolder.ToString())
        {
            string Url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
            SPFolder libFolder = ProductsLibrary.RootFolder.SubFolders[properties.ListItem.Name];
            string newFolderUrl = (web.Url + "/" + libFolder.ToString());
            foreach (string subfolder in subFolders)
            {
                SPListItem newSubFolder = ProductsLibrary.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, subfolder);
                newSubFolder.Update();
            }
        }
    }
}

thank you

2

2 Answers

0
votes

the solution is to opent the elements.xml and replace the with the and the code will run perfectly.

0
votes

Make sure

  1. you have added this event receiver as part of a feature
  2. the feature containing this event receiver is activated

Event wont fire, if you have not followed the above steps