I am trying to add fields to an announcement list.
I have successfully added various fields with single-word names:
announcementy.Fields.AddFieldAsXml("<Field Type='User' DisplayName='CreatedBy'/>", true, AddFieldOptions.AddToAllContentTypes);
announcementy.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='DateCreated'/>", true, AddFieldOptions.AddToAllContentTypes);
announcementy.Update();
context.ExecuteQuery();
ListItemCreationInformation itemCreateInfo1 = new ListItemCreationInformation();
ListItem oListItem1 = announcementy.AddItem(itemCreateInfo1);
oListItem1["Title"] = "Item no " + i.ToString() + " with body";
oListItem1["CreatedBy"] = context.Web.CurrentUser;
oListItem1["DateCreated"] = DateTime.Now;
oListItem1.Update();
context.ExecuteQuery();
However when I change DateCreated into Date Created it gives me the following errors, which is weird considering that it is a DisplayName/Title:
One or more field types are not installed properly. Go to the list settings page to delete these fields.
and
Column 'Date Created' does not exist. It may have been deleted by another user. /sites/TeamsiteWithLists/Announcements list
I found that it may be a bug and tried to use a workaround with first creating single-word names and then renaming them, but Sharepoint didn't react/execute/like it and users still see "CreatedBy" instead of "Created By":
announcementy.Fields.AddFieldAsXml("<Field Type='User' DisplayName='CreatedBy'/>", true, AddFieldOptions.AddToAllContentTypes);
announcementy.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='DateCreated'/>", true, AddFieldOptions.AddToAllContentTypes);
announcementy.Fields.GetByInternalNameOrTitle("CreatedBy").Title = "Created By";
announcementy.Fields.GetByInternalNameOrTitle("DateCreated").Title = "Date Created";
announcementy.Update();
context.ExecuteQuery();
Running .Title="" under separate ExecuteQuery or after Sleep() didn't change anything. Manual change is not an option. How can I set it correctly?