Pregunta

I am trying to create a list item with a folder content type by setting it's content type id to a folder content type id. The folder is created successfully but the title is set as 1_.000, 2_.000 etc.

I tried setting item["Title"] and item["Name"] to no avail.

What should I do?

¿Fue útil?

Solución

This is not document library, righ?

If not this will do (not the best code I wrote but it works):

string folderName = "MyFolderName";

using (SPSite site = new SPSite("http://mycoolsite/"))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["MyList"];
        string rootUrl = list.RootFolder.Url;
        SPListItem item = list.Items.Add();
        item["ContentType"] = "Folder";
        item.Update();
        SPFolder folder = web.GetFolder(string.Format("{0}/{1}", list.RootFolder.Url, item.DisplayName));
        folder.MoveTo(string.Format("{0}/{1}", list.RootFolder.Url, folderName));
    }
}

As far as I know SPFolder.MoveTo() is only option for folder renaming .

Or simply use this Create Sub Folders in Lists Programmatically

string folderName = "MyFolderName";

using (SPSite site = new SPSite("http://mycoolsite/"))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["MyList"];
        SPListItem item = list.AddItem("", SPFileSystemObjectType.Folder, folderName);
        item.Update();
    }
}

And little explanation: First example is what question owner wants: Create item with Content type Folder and set/change its name. Second one is just adding folder to SPList.

Otros consejos

try item.Title in your source code

If you are trying to create a new subfolder in a doc lib, this is how you do it:

var web = SPContext.Current.Web;
web.Folders["ListName"].SubFolders.Add("FolderName");
Licenciado bajo: CC-BY-SA con atribución
scroll top