Question

I have been struggling with this for a while now and I cannot find any helpful information on the interweb or forums etc.

Basically I have been asked to output the contents of my SharePoint 2010 document library onto a standard MVC web page.

Here is my code:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    using (ClientContext site = new ClientContext("Http://MySPSite"))
    {
      List list = site.Web.Lists.GetByTitle("MyList");    
      site.Load(list);
      site.ExecuteQuery();
    }

    return View();
}
Was it helpful?

Solution

I managed to pull listitems that I wanted to using the following

public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            List<string> documentTitles = new List<string>();
            using (ClientContext context = new ClientContext("http://siteurl"))
            {
                List list = context.Web.Lists.GetByTitle("doctest");
                context.Load(list);
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View/>";
                ListItemCollection listItems  = list.GetItems(query);
                context.Load(list);
                context.Load(listItems);
                //context.Load(listItems ,items => items.Include(
                //    item=>item["FileLeafRef"] 
                //    ));
                context.ExecuteQuery();
                foreach (ListItem item in listItems )
                {                    
                    documentTitles.Add(item["FileLeafRef"].ToString());
                }
            }
            return View(documentTitles);
        }

FileLeafRef is the internal name for the "Name" column.

I commented out the include FileLeafRef since it is included by default but you may need it for other columns.

Also this needs to be done in .NET 3.5 since you are using the client libraries.

You will need to populate an object with the data you want and pass that to the view (If you are not aware of this you should look at some mvc examples. From your example I can't tell if you know mvc or not or if you are just posting code to demonstrate the SharePoint issue)

You may also have to set the context crentials

context.Credentials = new NetworkCredentials("Username", "Password", "Domain");

before calling execute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top