0

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.

1

1 Answer 1

1

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);
                    }
                }

            }


        }

Your Answer

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

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