Reputation: 8214
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!
Upvotes: 1
Views: 4781
Reputation: 9
There are several ways to accomplish what you are describing in your question.
Since you have already managed to get a reference to a SPFolder
object, let's go ahead and start from there.
You have used the Files
property of the SPFolder
object to get a reference to a SPFileCollection
. As you iterate over this collection, you are working with SPFile
objects, one at a time. As you have already discovered, the SPFile
object contains several interesting properties regarding the file itself. In order to access the additional metadata that you are looking for, however, you need to use the Item
property of the SPFile
object. With a reference to the underlying SPListItem
object of the file, you can reference the list fields you are trying to enumerate.
List fields (columns) can be accessed using an indexer as you have previously attempted. To build on the code snippets you provided:
SPFolder folder = web.GetFolderByServerRelativeUrl("path")
foreach (SPFile file in folder.Files)
{
SPListItem item = file.Item
String contentType = item["Content_x0020_Type"]
}
As always, consider including the appropriate exception handling and choosing the appropriate variable types that make sense for your particular need.
As you begin working with different field types, I keep the following reference handy to guide me in working with all of the different types of fields that SharePoint makes available: http://social.technet.microsoft.com/wiki/contents/articles/20831.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-powershell.aspx
The examples here are for PowerShell, but the function names are the same that you would use in other languages such as, C#.
Upvotes: 0
Reputation: 8214
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;
Upvotes: 1