user1477388
user1477388

Reputation: 21420

Field Not Recognized on AddAsync of SharePoint List Item

When I try to create a new list item with the basic calendar/list fields everything works perfectly. However, when I try to do so with a "non-standard" field i.e. a field I added, I am getting a "field not recognized" error.

The field is clearly there! Is there some special way I need to populate these custom fields?

// get a specific list
ISiteListsCollectionPage list = await graphClient.Sites["root"].Lists.Request()
    .Filter($"DisplayName eq 'Outlook Integration'").GetAsync();

// create a dictionary of [calendar] list properties
Dictionary<string, object> props = new Dictionary<string, object>();

// populate properties, all of these work just fine
props.Add("Title", evt.Subject);
props.Add("Location", evt.Location?.DisplayName);
props.Add("EventDate", utcStart.ToString("yyyy-MM-ddTHH:mm:ssK"));
props.Add("EndDate", utcEnd.ToString("yyyy-MM-ddTHH:mm:ssK"));
props.Add("Description", Regex.Replace(evt.Body.Content, "<.*?>", String.Empty)); // remove HTML content

// populate custom properties
props.Add("ResourceID", evt.Id); // throws error: Field 'ResourceID' is not recognized

// create list item with our properties dictionary
var newItem = new ListItem
{
    Name = "My New Event",
    Fields = new FieldValueSet()
    {
        AdditionalData = props
    }
};

// call the service and get the result
var newListItem = await graphClient.Sites["root"].Lists[list[0].Id].Items.Request().AddAsync(newItem);

This is the complete list of fields on my list:

enter image description here

Here you can see the display name is "ResourceID" whereas the API name is "O365EventId." However, both result in the same error, "Field not recognized."

enter image description here

Note: ResourceID is one of the fields that I added. How can I set the value of this field via the Graph API?

Upvotes: 2

Views: 1679

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

Marc is right by saying in comment regarding column name, the provided screenshot displays Column.displayName which is

The user-facing name of the column.

but what actually FieldValueSet.AdditionalData expects as a key is Column.name which is:

The API-facing name of the column as it appears in the fields on a listItem. For the user-facing name, see displayName.

In your case most likely displayName and name properties are different, you could verify it via following endpoint: https://graph.microsoft.com/v1.0/sites/root/lists/Outlook Integration/columns

and that's the reason why this error occurs.

Via the Graph API client (C#), you can see a list of all columns for any given list like so:

// get specific list by name
ISiteListsCollectionPage list = await graphClient.Sites["root"].Lists.Request()
    .Filter($"DisplayName eq 'YOUR_LIST_NAME_HERE'").GetAsync();

// get columns and output them to the log as a serialized object
var listColumns = await graphClient.Sites["root"].Lists[list[0].Id].Columns.Request().GetAsync();
logger.LogInformation($"List Columns Object: {JsonConvert.SerializeObject(listColumns).ToString()}");

Upvotes: 2

Related Questions