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.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI 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);
}
}
}
}