6

I am able to add new list item in sharepoint list using the CSOM using the below code :

 ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem oListItem; oListItem = currentList.AddItem(itemCreateInfo); oListItem["fieldName"] = fieldvalue; oListItem.Update(); 

oListItem["fieldName"] This line takes the internal name of the fields. Is there any way in which we can access the listitem field by its display names.

Any idea to achieve this.

Thanks

3
  • 1
    It is not a good practice to get fields by its display name (since they tend to change a lot, can exist multiple times in the same view etc) unless it is absolutely necessaryCommentedJan 17, 2014 at 7:12
  • @Robert Yes you are absolutely right. But I have some scenario where I have the long field name so the internal name of field trims the field name to a specific character and omits other. So I need to access the field by character name.
    – Sushri
    CommentedJan 17, 2014 at 7:16
  • 2
    @Sushri as a best practice when creating a list, always give your fields a short name without spaces and no special characters (stick to numbers and letters only), then edit the field and change its title to whatever you want. This makes your life as a developer easier (you don't have to deal with XML encoded characters)CommentedJan 17, 2014 at 9:39

2 Answers 2

2

ListItem.Item property has the following signature

public Object this[ string fieldName ] { get; set; } 

where fieldName accepts InternalName or StaticName

The solution would be to retrieve list fields first and then resolve field InternalName by Title as demonstrated below:

//Retrieve list columns first var list = clientContext.Web.Lists.GetByTitle(listTitle); var listFields = list.Fields; clientContext.Load(listFields, fields => fields.Include(field => field.Title, field => field.InternalName)); clientContext.ExecuteQuery(); var itemCreateInfo = new ListItemCreationInformation(); var listItem = list.AddItem(itemCreateInfo); var titleField = listFields.FirstOrDefault(f => f.Title == "Title"); //resolve Field Internal Name by Title listItem[titleField.InternalName] = "SharePoint"; listItem.Update(); clientContext.ExecuteQuery(); 
    0

    lvResult is a list witin my client application that i created: here im populating the client list from a list witin sharepoint using the field names using a loop as i wanted to return all fields.

     private void getListDetails(string zoneName) { lvResult.Items.Clear(); try { ClientContext context = new ClientContext(txtURL.Text); List list = context.Web.Lists.GetByTitle(zoneName); CamlQuery query = new CamlQuery(); query.ViewXml = "<View/>"; ListItemCollection items = list.GetItems(query); context.Load(list); context.Load(items); context.ExecuteQuery(); foreach (ListItem item in items) { ListViewItem fitems = new ListViewItem((String)item["Title"]); fitems.SubItems.Add((String)item["ImageUrl"]); fitems.SubItems.Add((String)item["NavigationUrl"]); fitems.SubItems.Add(item["OpenInNewWindow"].ToString()); //add the items to the list lvResult.Items.Add(fitems); } } catch (Exception a) { MessageBox.Show(a.ToString()); } } 

    for you, you can do the same above or somthing on the lines of:

     foreach (ListItem item in items) { string fieldValue = item.FieldValuesAsText["field name"]; } 

    if you know the index order of each row say you only want to return the first item row than there is no need for a foreach:

    string fieldValue = items[0].FieldValuesAsText["field name"];

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.