I want to download all the files in .pdf format from the document library, it doesn't concern with format which we have uploaded like .xls, .docx etc.
Thanks in advance.
I want to download all the files in .pdf format from the document library, it doesn't concern with format which we have uploaded like .xls, .docx etc.
Thanks in advance.
Could check the file extension in the code logic like this to download only pdf files:
using (var clientContext = new ClientContext("http://sp/sites/devtest"))
{
var list = clientContext.Web.Lists.GetByTitle("Documents");
ListItemCollection ListItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(list);
clientContext.Load(ListItems);
clientContext.ExecuteQuery();
foreach (ListItem item in ListItems)
{
clientContext.Load(item, i => i.File);
clientContext.ExecuteQuery();
if (item.File.Name.Split('.')[1].ToLower()=="pdf")
{
var fileRef = item.File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
var fileName = Path.Combine(@"C:\", (string)item.File.Name);
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
}
}