Question

I have a custom list which can contain a 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, when I'm looking at my list after this I see that:

enter image description here

When I go to the list via a webbrowser and create the content type manually, everthing is fine. (Which means that the Title is "root" and not the ID).

Was it helpful?

Solution

Thank you both for you answers!

The solution was a mixture of both answers. Additionally you have 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();

OTHER TIPS

The "name" field corresponds to the filename. Despite what you see in the column heading, the 1125_.000 is the filename of the list item, which is automatically generated if you don't supply one:

rootItem["Name"] = "myname";

"Name" is a built-in field.

Try to set the content type ID first then do a rootItem.update(). Set your field content after that.

//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(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top