none
event receiver when an item added inside a folder within a document library RRS feed

  • Question

  • How to trigger an event receiver when there is a item added inside a folder within a document library?

    i know how to add the same in a document library, but when it comes to a folder within a  document library , how to proceed ?

    below is my  current code, i am stuck with , how to fetch item from the subfolder , that the end user //uploaded .

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

                 SPUser currUser = properties.Web.CurrentUser;
                /*SPListItem item = properties.ListItem;
    SPFile file = item.File;
    //To get folder name
    string fldrname = file.ParentFolder.ToString();
    SPList list = properties.List;
    SPQuery query = new SPQuery();

    //Hard coded the folder name, but you can get the value(substring Of file variable)from file variable.
    query.Folder = list.RootFolder.SubFolders["Testing1"];

    //Recursive Scope....
    query.ViewAttributes = "Scope=\"Recursive\"";
    SPListItemCollection allitems = list.GetItems(query);
    int filecount = allitems.Count;
                 * 
                 * 
                 * */
                try
                {

                    if (properties.ListTitle.ToLower() == "common")
                    {




                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {

                        //string strBIFRootURL = WebConfigurationManager.AppSettings["BIFRoot"].ToString();


                        SPSite CurrentSite = new SPSite(properties.SiteId);
                        SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);
                        SPList CurrentList = CurrentWeb.Lists[properties.ListId];
                        SPListItem CurrentListItem;

                        SPWeb myRootWeb = CurrentSite.RootWeb;


                        CurrentListItem = CurrentWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID); 

                        SPFieldUserValue flduserValue = new SPFieldUserValue(CurrentWeb, currUser.ID, currUser.Name);

                       bool isFolder = (properties.AfterProperties["vti_filesize"] == null);
                if (isFolder)
                { 

    // how to perform the permissions inside this folder ? 

                                                                                                                                  
    • Edited by SaMolPP Thursday, December 22, 2016 3:45 PM
    Thursday, December 22, 2016 12:30 PM

All replies

  • Hi,

    The following code snippet for your reference:

    SPList list = properties.List;
    SPListItem item=properties.ListItem;
    //run the code if upload the file in "folder" folder
    if (item.File != null && item.File.ParentFolder.Name.Equals("folder"))
    {
    	SPQuery query = new SPQuery();
    	query.Folder = item.File.ParentFolder;
    
    	//Recursive Scope....
    	query.ViewAttributes = "Scope=\"Recursive\"";
    	SPListItemCollection allitems = list.GetItems(query);
    	int filecount = allitems.Count;
    
    	//fetch all items from the subfolder 
    	foreach(SPListItem oItem in allitems)
    	{
    		SPListItem currentItem = oItem;
    
    	}
    
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, December 26, 2016 3:00 AM
    Moderator