Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Good Morning Friends.

I am trying to download files from share point URL. but i am unable to download.
i found code but all code for downloading only one file.
but i want to download all the files present in SharePoint portal link.
on that link i have an column like,

File Name posted Date


Here may be 10 to 15 file can be posted i want to download all files in my local folder when i run the program.

And names format will be same only dates can change in posted date column.

i want to do looping through all files so we can download all the files one by one.

Please help me its urgent.

Thanks In advance.
Posted

If you know the document library name, you can do it. I did this sometimes back. Hope the code still works :) If not just try along these lines.

using SPC = Microsoft.SharePoint.Client; 

using (SPC.ClientContext context = new SPC.ClientContext(siteUrl))
{
    context.Credentials = new NetworkCredential("username", "password");
    SPC.Web myWeb = context.Web;
    context.Load(myWeb, website => website.Title);
    context.ExecuteQuery();
                    
    //Load Sample Documents document library
    SPC.List documentsList = myWeb.Lists.GetByTitle("Sample Documents");
    context.Load(documentsList);
    context.ExecuteQuery();

    //Load documents in the document library
    SPC.ListItemCollection listItemCollection = documentsList.GetItems(new SPC.CamlQuery());
    context.Load(listItemCollection);
    context.ExecuteQuery();

    foreach (SPC.ListItem item in listItemCollection)
    {
        SPC.FileInformation fileInfo = SPC.File.OpenBinaryDirect(context, item["FileRef"].ToString());

        using (MemoryStream memoryStream = new MemoryStream())
        {
            CopyStream(fileInfo.Stream, memoryStream);
            //Retrieved file is saved as byte array
            Byte[] retrievedFile= memoryStream.ToArray();
            
            string outFileName = @"C:\temp\test\" + item["FileLeafRef"].ToString();

            //Write the file to disk
            File.WriteAllBytes(outFileName, retrievedFile);
        }
    }
}
 
Share this answer
 
Comments
bunty swapnil 18-Jul-14 6:08am    
Could you please tell me the steps followed by you for completing this job.
like where we have to write this code.
i mean steps because i am very new to ssis.
bunty swapnil 21-Jul-14 7:50am    
Hi Vathsala,

From where will get thi sharepoint.client dll?

and the function copystream?
The files/dlls are located on any SharePoint 2013 server at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI or on any Sharepoint 2010 server at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI. You can copy the dlls from the servers or else you can download them from http://www.microsoft.com/en-us/download/details.aspx?id=35585[^]

Regarding the CopyStream: You don't really need to use that unless you are using .NET3.5 or before. Because .NET3.5 didn't have any in-built method to copy the stream; so I had to use a method to manually copy. As I mentioned in my previous reply; It was an old piece of code. But .NET 4.0 has
Stream.CopyTo
And .NET 4.5 has
Stream.CopyToAsync
So you can use either one of them to copy the stream to another.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900