0

Hi I want to display a list of documents in an MVC view and be able to download them on click. I have attempted a solution myself as shown below. I have been struggling with this for a long time so any suggestions will be appreciated.

namespace SharePointMVC.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
            DefaultModel Lists = new DefaultModel();
            string siteURL = "http://MYSPSSITE";
            string documentListName = "MY DOCUMENT LIBRARY";
            ListItemCollection listItems = null;
            using (ClientContext clientContext = new ClientContext(siteURL))
            {
                List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);

                CamlQuery camlQuery = new CamlQuery(); ;
                camlQuery.ViewXml =
                    @"<View>

                     <Query>
                     <Where>
                     <Eq>
                     <FieldRef Name ='" + name + @"'/>

                     <Value Type ='" + type + "'>" + value + @"</Value>
                     </Eq>

                     </Where>
                     <RowLimit>" + rowLimit.ToString() + @"</RowLimit>

                     </Query>
                     </View>";

                listItems = documentsList.GetItems(camlQuery);

                clientContext.Load(documentsList);
                clientContext.Load(listItems);

                clientContext.ExecuteQuery();

            }

            return listItems;
        }

        private static ListItem GetDocumentFromSP(String documentName)
        {
            ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1);
            return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
        }

        public Stream DownloadDocument(string SiteURL, string documentName)
        {
            ListItem item = GetDocumentFromSP(documentName);
            if (item != null)
            {
                using (ClientContext clientContext = new ClientContext(SiteUrl))
                {
                    FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["Outline.docx"].ToString());
                    return fileInformation.Stream;
                }
            }

            return null;
        }
Josh Price
  • 259
  • 3
  • 9
  • 18
  • Did you already check this site out? -http://social.msdn.microsoft.com/Forums/eu/sharepointdevelopmentlegacy/thread/88a07e77-61ef-42d8-802e-0079c27b3bf7 – Ben_Coding Dec 18 '12 at 16:04
  • Yeah I have been through that site although I didn't find it to be very clear. Thanks anyway! – Josh Price Dec 18 '12 at 17:13

1 Answers1

0

If you don't have too many requirements (e.g. must be able to override the magical 5000 documents mark), you could use SPMetal and just run a LINQ query. Do note that it has limitations and isn't as performant as CAML Queries, but it works all the same.

E Ralph
  • 16
  • 1