How to set the name of a new SPListItem programmatically?
Asked Answered
C

3

7

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).

Ceil answered 11/11, 2011 at 14:46 Comment(0)
C
6

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();
Ceil answered 11/11, 2011 at 15:52 Comment(0)
G
1

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.

Gavrila answered 11/11, 2011 at 15:15 Comment(0)
B
0

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(); 
Boltrope answered 11/11, 2011 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.