1

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?

2

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.

  • @ÉlodiePetit I have little update. – Vedran Rasol Sep 22 '11 at 11:44
0

try item.Title in your source code

  • Title property is always read-only. – Élodie Petit Sep 22 '11 at 11:07
  • item["Title"]="foldertitle" then item.Update(). It is possible to set title. Title is not read only – Strider Sep 22 '11 at 11:12
  • As I wrote in my post, I already tried item["Title"] but that doesn't change anything. – Élodie Petit Sep 22 '11 at 11:14
0

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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.