Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the name of a new SPListItem programmatically?

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

like image 652
LMW-HH Avatar asked Nov 11 '11 14:11

LMW-HH


2 Answers

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();
like image 114
LMW-HH Avatar answered Oct 05 '22 12:10

LMW-HH


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.

like image 29
Rex M Avatar answered Oct 05 '22 12:10

Rex M