3

I've a document library and within this document libary I have sub-folders (no subfolders further).

There is 1 documentlibary called DocumentACC and this document libary contains Sub-folders as you see below. These subfolders contains FILES (Word, PPT, XLS etc.) no subfolders within this further.

So when I open the DocumentACC then the Folder it shows e.g. 15 files. now the idea is with my sandbox solution WEBpart which has only 1 button. The idea is when the user clicks on the button he should get all files names + location of that files....

!!!! But only files for that specific folder, where he is !!!!

So I looked and found the Folder is in the URL... But how can I get the files for that folder. So I think the URL with the querystring parameter "RootFolder" is fine, but how can I get the files.... I'm struggling and struggling, can someone bring me on right path... Below is the folder structure and the small code for webpart....

So HOw can I get all files from Folder ACC 2015-11-05??

DocumentACC

ACC 2015-12-14

ACC 2015-11-05

ACC 2015-10-15

http://SharePoint2013/AccountingSite/DocumentsACC/Forms/AllItems.aspx?RootFolder=%2FAccountingSite%2FDocumentsACC%2FACC%202015%2D12%2D14&FolderCTID=0x012000BA2FCD357903654890332D0D577C47E9&View=%7B2493FEB5%2DC1D4%2D401E%2D8358%2DCE0BC680FDAD%7D

libName = "DocumentsACC";

using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
    SPDocumentLibrary lstDocLib = (SPDocumentLibrary)web.Lists[libName];

    Label3.Text = lstDocLib.RootFolder.Name;
    foreach (SPListItem item in lstDocLib.Items)
    {
        //Label3.Text = item.Folder.Name;
        if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
        {
            //item is a folder
            Label1.Text = Label1.Text + "<BR />" + item.Name;
        }
        else if (item.FileSystemObjectType == SPFileSystemObjectType.File)
        {
            //item is afile
            Label2.Text = Label1.Text + "<BR />" + item.Name;

            //HERE I NEED TO GET THE FILE NAMES and LOCATION
        }
    }
}

1 Answer 1

0

If you have an SPListItem whose content type is Folder, you can get an SPFolder object using the Folder property:

item.Folder

With that object, you can get the Files property to retrieve a SPFileCollection object with all of the files inside the folder:

item.Folder.Files

If your SPListItem is a file, you can get its SPFile object with the File property:

item.File

Both SPFolder and SPFile have a Url property that will give you the web-relative URL for that object:

item.File.Url
item.Folder.Url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.