How to set the name of a new SPListItem programmatically?

I have my own list, which can contain CustomContentType. This is how I create a new item:

//Create root folder SPListItem rootItem = navigation.Items.Add(); SPContentType folderType = navigation.ContentTypes["ListLevel"]; rootItem[SPBuiltInFieldId.Title] = "root"; rootItem["ContentTypeId"] = folderType.Id; rootItem.Update(); 

The problem is that when I look at my list, I see the following:

enter image description here

When I go to the list through a web browser and create the content type manually, everything is fine. (This means that the header is "root", not an ID).

+6
source share
3 answers

Thank you for the answers!

The solution was a mixture of both answers. In addition, you need to reload the list:

  //Create root folder SPListItem rootItem = navigation.Items.Add(); SPContentType contentType = navigation.ContentTypes["ListLevel"]; rootItem["ContentTypeId"] = contentType.Id; rootItem["Title"] = "root"; rootItem.Update(); navigation.Update(); rootItem = navigation.GetItemById(rootItem.ID); rootItem["Name"] = "root"; rootItem.Update(); 
+5
source

The name field corresponds to the file name. Despite what you see in the column heading, 1125_.000 is the name of the list item that is automatically generated if you did not add it:

 rootItem["Name"] = "myname"; 

"Name" is an embedded field.

+1
source

First try setting the content type identifier, and then enter rootItem.update (). After that, configure the contents of the field.

 //Create root folder SPListItem rootItem = navigation.Items.Add(); SPContentType folderType = navigation.ContentTypes["ListLevel"]; rootItem["ContentTypeId"] = folderType.Id; rootItem.Update(); rootItem[SPBuiltInFieldId.Title] = "root"; rootItem.Update(); 
0
source

Source: https://habr.com/ru/post/901301/


All Articles