Getting field names of a listItem - (Strongly typed?) in SharePoint 2010 Client API
The thing to be aware of is that each SPListItem has a different set of field depending on the underlying content type. This is also modified depending on which list the item is sitting in. So this is why there is no strongly typed indexing into the fields...
However, adding the following code should help you get a feel for what is inside each listitem
foreach(SPField field in oListItem.Fields)
{
Console.Write(field.Title + " (" + field.InternalName + "): ");
Console.WriteLine(oListItem[field.Id].ToString());
}
The property SPField.SchemaXml shows the full detail on the configuration of each field. Having a copy of SharePointManager will give you a good idea of how any given site hangs together.
If you can only use the client object model to discover this
foreach(string fieldName in oListItem.FieldValues.Keys)
{
Console.Write(fieldName);
Console.WriteLine(oListItem.FieldValues[fieldName]);
}
David C
Updated on July 06, 2022Comments
-
David C almost 2 years
I'm very new to SharePoint, using 2010. Trying to see what we can do with it, in particular with Lists. And I have a feeling I'm missing something pretty obvious, but can't find it.....
In a Sharepoint site I created a list called Famous People: Added a few people in, Frank Sinatra etc.
- Last Name: Sinatra
- First Name: Frank
- E-mail Address: [email protected]
- Job Title: Singer
- etc
So then I've been trying to get that information into a simple C# console application, referring and trying many examples I have found. But I get stuck around working out what the actual fieldNames(?) are that I need to refer to, as it doesn't appear to be strongly typed. But also using the likes of "E-mail Address" doesn't seem to work either
Here's one example I've been trying. (From: How to: Retrieve List Items)
string siteUrl = "http://servername/site/"; var clientCtx = new ClientContext(siteUrl); Microsoft.SharePoint.Client.List oList = clientCtx.Web.Lists.GetByTitle("Famous People"); var camlQuery = new CamlQuery {ViewXml = "<View><RowLimit>100</RowLimit></View>"}; ListItemCollection collListItem = oList.GetItems(camlQuery); clientCtx.Load(collListItem, items => items.Include( item => item.Id, item => item.DisplayName, item => item.HasUniqueRoleAssignments)); clientCtx.ExecuteQuery(); foreach (ListItem oListItem in collListItem) { Console.WriteLine("ID: {0} \nDisplay name: {1} \nUnique role assignments: {2}", oListItem.Id, oListItem.DisplayName, oListItem.HasUniqueRoleAssignments); } Console.ReadLine(); clientCtx.Dispose();
Which brings back:
ID: 1 Display name: Clough Unique role assignments: False ID: 2 Display name: Sinatra Unique role assignments: False ID: 3 Display name: Simpson Unique role assignments: False ID: 4 Display name: Skywalker Unique role assignments: False
I have tried each of the methods on the examples link I provided above. But from there I have no idea how to get to the other properties such as "Job", or "E-mail address".
Or for example if I'm dealing with a List which I don't know, how do I find out the valid field names from my application via web service?Thanks.
-
Nat over 12 yearsGet a copy of SharePointManager (spm.codeplex.com)
-
David C over 12 yearsoListItem doesn't seem to give me a oListItem.Fields. Isn't SPsomething; Eg. SPFields; for working on the server, not client/api?
-
David C over 12 yearsYou are right, looking at those url's does give me the field names, but not dynamically. It feels tremendously scruffy; I was after something to return the field names in my code from the API/Client Object Model.
-
David C over 12 yearsThis just didn't seem to work for me, unless there was more I needed to add?
-
Nat over 12 yearsSorry, for that you need to itereate of the filedvalues.keys