C# Recursive Function - Sharepoint Related

Discussion in 'OT Technology' started by Phil Blunts, Sep 15, 2006.

  1. Phil Blunts

    Phil Blunts Guest

    I'm having a brainfart.

    I need a recursive function that I can use to iterate through all folders and subfolders in a WSS 2.0 site. Basically I'm writing a web part that will loop through all files in a relatively small WSS implementation and compare the TimeCreated against value I'm pulling and display all documents uploaded since the users last login.

    Currently this is what I have, but it's only reaching top level folders and is not going into sub folders. I'm fairly certain I need a recursive function that I can pass an SPFolder to and will check if it that folder has a subfolder, if it does, it will pass back to itself and check again or iterate the files depending on whether or not it has subfolders.

    This is what I currently have, I need help writing the recursive function though.
    Code:
    SPWeb site = SPControl.GetContextWeb(Context);
    SPUser user = site.CurrentUser;
    
    SPFolderCollection folders = site.Folders;
    
    StringBuilder sb = new StringBuilder();
    
    if(Convert.ToDateTime(Session["lastLogin"]) > DateTime.MinValue)
    {
        foreach (SPFolder folder in folders)
        {
            SPFileCollection files = folder.Files;
            
            foreach(SPFile file in folder.Files)
            {
                string strFileName = file.Name;
                DateTime dtDateCreated = file.TimeCreated;
    
                if(dtDateCreated > Convert.ToDateTime(Session["lastLogin"]))
                {
                    sb.Append( "Folder: " + Microsoft.SharePoint.Utilities.SPEncode.HtmlEncode(folder.Name) + "File Name: " + strFileName + " Time Created: " + dtDateCreated + "<br/>");
                }
            }
        }
    }
    lblResult.Text = sb.ToString(); 
    
     
  2. GunboatDiplomat

    GunboatDiplomat New Member

    Joined:
    Jun 9, 2006
    Messages:
    214
    Likes Received:
    0
    What's a "brainfart?"

    Do you not know what a recursive function is?

    I don't know C# and have never seen a line of it in my life but I know how to program so let me take a look, here...

    Your first problem would be that you don't have a function. It's hard to make a recursive function without a function, so... try something like this:
    Code:
    SPWeb site = SPControl.GetContextWeb(Context);
    SPUser user = site.CurrentUser;
    
    SPFolderCollection folders = site.Folders;
    LBLResult lblResult;
    
    // I don't know how to declare a function
    void RecursiveFunction(LBLResult lblResult, SPFolderCollection folders)
    {
    	StringBuilder sb = new StringBuilder();
    
    	// I dont know what this is testing.  What is "Session?"
    	if(Convert.ToDateTime(Session["lastLogin"]) > DateTime.MinValue)
    	{
    		foreach (SPFolder folder in folders)
    		{
    			SPFileCollection files = folder.Files;
    	        
    			foreach(SPFile file in folder.Files)
    			{
    				string strFileName = file.Name;
    				DateTime dtDateCreated = file.TimeCreated;
    
    				if(dtDateCreated > Convert.ToDateTime(Session["lastLogin"]))
    				{
    					sb.Append( "Folder: " + Microsoft.SharePoint.Utilities.SPEncode.HtmlEncode(folder.Name) + "File Name: " + strFileName + " Time Created: " + dtDateCreated + "<br/>");
    				}
    			}
    			lblResult.Text = sb.ToString();
    
    			// I don't know how to check the size of a folder list...
    			if(folder.Folders.Size > 0)
    			{
    				// I don't know how to get a list of subfolders from a folder...
    				RecursiveFunction(lblResult, folder.Folders);
    			}
    		}
    	}
    }
    
    RecursiveFunction(lblResult, folders)
     
  3. Phil Blunts

    Phil Blunts Guest

    I'm aware that I didn't have a function. What I posted was the code I was currrently using, simply demonstrating the way I accomplished it before I realized I needed a recursive function.

    I'm home now so I don't have the code, but I got it figured out earlier.

    Thanks for the reply though.
     

Share This Page