开发者

Getting field names of a listItem - (Strongly typed?) in SharePoint 2010 Client API

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: frank.sinatra@ratpack.com
  • 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开发者_JS百科));

        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.


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]);
}


You can access the other properties via the indexed property on the ListItem.

You have to add the properties as ViewFields to the ViewXml:

var camlQuery = new CamlQuery 
{
  ViewXml = "<View><ViewFields><FieldRef Name='Last_x0020_Name'/></ViewFields><RowLimit>100</RowLimit></View>"
};

Note the _x0020_ encoding for spaces.


If you go to your list settings you can find the column name by clicking on it. This will send you to a link like: http://servername/_layouts/FldEdit.aspx?List=%7BEACD90E2%2DED64%2D42EE%2DB1FB%2D2631687C545F%7D&Field=Title
The name of the field is the RHS of Field=Title. Given that you know the column names you can use the following code:

cxt.Load(items,
   c => c.Include(
      item => item["ID"],
      item => item["Title"],
      item => item["AssignedTo"],
      item => item["Status"]));
   cxt.ExecuteQuery();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜