1

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?

1 Answer 1

4

As part of using AddFieldAsXml you need to provide the field's internal and staticname

announcementy.Fields.AddFieldAsXml("<Field Type='User' DisplayName='Created By' StaticName='CreatedBy' Name='CreatedBy'/>", true, AddFieldOptions.AddToAllContentTypes);
announcementy.Fields.AddFieldAsXml("<Field Type='DateTime' DisplayName='Date Created' StaticName='DateCreated' Name='DateCreated'/>", true, AddFieldOptions.AddToAllContentTypes);

An interesting article on this topic http://blog.martinhey.de/en/post/2012/01/22/AddFieldAsXml-bug-or-feature-that-is-the-question.aspx

6
  • I'm afraid I'm still getting "Column 'CreatiedBy' does not exist. It may have been deleted by another user." I changed the name into Creatied so as to make sure it doesn't clash with the default column. It's not an issue of provisioning either - if I try to access the column after it appears in GUI,I get the same error. I'm not getting these issues in a generic list where I create fields with DisplayName only. Any other suggestions?
    – grisha
    Commented Oct 22, 2014 at 22:15
  • Are you suggesting that the list got created and now you are unable to add/edit data? Commented Oct 22, 2014 at 22:23
  • Yes, the columns are added, and the displayNames are showing correctly. I just cannot access them later via internal/static names.
    – grisha
    Commented Oct 22, 2014 at 22:24
  • 1
    Open list settings page and click on the column name. Check the URL and make sure the field name in query string is correct. Commented Oct 22, 2014 at 22:33
  • It works with oListItem1["Created_x0020_By"] and oListItem1["Date_x0020_Created"]. So despite declaring internal/static names, SPO seems to take a displayname and parse the space :) Not perfect, but solves my issue.Thanks.
    – grisha
    Commented Oct 23, 2014 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.