1

Am struggling to get a pdf file from a Sharepoint 2010 site, I have managed to get a list of entries and found the file details.

How do I now download the file? I got a root folder file downloaded but this file is deep in many folders(with spaces!) within Sharepoint and I could not get right context.

Can someone guide me how to get the file?

I get an error on this line at when I try to access the file and save it;

var fileRef = oListItem.File.ServerRelativeUrl;


private static void camlTest()
    {
        ClientContext clientContext = new ClientContext("http://SPsite/Departments/sales/salesshelf/");
        Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Target Folder");
        clientContext.Load(spList);
        clientContext.ExecuteQuery();

        if (spList != null && spList.ItemCount > 0)
        {
            Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='RecursiveAll'> <Query> <Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where> 
                                </Query> <ViewFields><FieldRef Name='_dlc_DocIdUrl' /><FieldRef Name='_dlc_DocId' /><FieldRef Name='FileLeafRef' /><FieldRef Name='LinkFilenameNoMenu' /><FieldRef Name='LinkFilename' /><FieldRef Name='ParentLeafName' /><FieldRef Name='Title' /><FieldRef Name='DocIcon' /><FieldRef Name='FileRef' /><FieldRef Name='ContentType' /></ViewFields> </View>";

            ListItemCollection listItems = spList.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            Console.WriteLine("List Count: {0} ", listItems.Count);

            foreach (ListItem oListItem in listItems)
            {
                string spFilename = (string)oListItem["FileLeafRef"];
                string spRelativePathFilename = (string)oListItem["FileRef"];

                //Ignore     /Archive/
                if (checkForArchivePath(spRelativePathFilename)==false)
                {
                    //Check for file name
                    if (spFilename == "TargetPDF.pdf")
                    {
                        Console.WriteLine("Passed:   ID: {0} \nFileLeafRef/Filename: {1} \nFileRef/Path: {2}", oListItem.Id, oListItem["FileLeafRef"], oListItem["FileRef"]);


                        var fileRef = oListItem.File.ServerRelativeUrl;
                        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                        var fileName = Path.Combine("C:\\LocalDir\\", (string)oListItem.File.Name);
                        using (var fileStream = System.IO.File.Create(fileName))
                        {
                            fileInfo.Stream.CopyTo(fileStream);
                        }
                    }
                }
            }
        }
    }
0

After a bit of coffee, hair pulling etc it turned out it was just lifting the File Ref value and the file came down

var fileRef = (string)oListItem["FileRef"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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