I'm new to the Sharepoint client object model and I'm having difficulty getting all of the items from a particular List
along with their column data. I think it's confusing me because it's actually a Folder
set to display as a List
which is then filtered by a View
!
The setup is like this:
List: Managed Collateral
View: Proposal Collateral
Folder: Proposal Collateral
Folder relative URL: /sites/collateral/Managed Collateral/Proposal Collateral
Which looks like this:
I can use methods like Lists.GetByTitle("title");
and GetFolderByServerRelativeUrl("path");
to get the List
and Folder
objects respectively. I get the correct Count
returned from these so I think they're returned correctly.
I've tried iterating through these but I can only ever seem to access static properties like file.Name
and file.TimeLastModified
, but my list shows columns like Content_x0020_Type
and Proposal_x0020_Type
.
I've tried things like file["Content_x0020_Type"]
but that didn't work for me either.
How do I get a Folder, then loop through each item and get the data from the columns from the associated List/View?
I can post my code if needed, but I felt it would just confuse matters.
Thanks!
You know when you've had an issue for two days and you've read countless bits of documentation and forum posts before finally posting a question somewhere only to work it out shortly after? Yeah, that.
Anyway, I think I have something that fetches the data I want in the most efficient way. I'll post it here rather than delete my question in case it helps someone else.
If anyone has any improvements or suggestions then I might change the accepted answer.
// Fields
private static string _siteUrl;
private static string _userName;
private static string _passWord;
private static string _domain;
private static DataTable dataTable;
private ClientContext _clientContext;
private Web _spWebsite;
// We'll store the data in a table for ease
dataTable = new DataTable();
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Path", typeof(string));
dataTable.Columns.Add("Proposal Grouping", typeof(string));
dataTable.Columns.Add("Modified", typeof(DateTime));
_clientContext = new ClientContext(_siteUrl);
_clientContext.Credentials = new NetworkCredential(_userName, _passWord, _domain);
_spWebsite = _clientContext.Web;
List list = _spWebsite.Lists.GetByTitle(@"Managed Collateral");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View><Query></Query></View>";
camlQuery.FolderServerRelativeUrl = @"/sites/collateral/Managed Collateral/Proposal Collateral";
ListItemCollection listItems = list.GetItems(camlQuery);
_clientContext.Load(listItems, items => items.Include(
item => item.File.Name,
item => item.File.ServerRelativeUrl,
item => item["Proposal_x0020_Navigation_x0020_Grouping"],
item => item["Modified"]));
_clientContext.ExecuteQuery();
if (listItems != null)
{
foreach (ListItem item in listItems)
{
DataRow dr = dataTable.NewRow();
dr["Name"] = item.File.Name;
dr["Path"] = item.File.ServerRelativeUrl;
dr["Proposal Grouping"] = item["Proposal_x0020_Navigation_x0020_Grouping"];
dr["Modified"] = item["Modified"];
dataTable.Rows.Add(dr);
}
}
dataGridView.DataSource = dataTable;