Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
@ÉlodiePetit I have little update. –  Vedran Rasol Sep 22 '11 at 11:44
    
Thank you Vedran! –  Élodie Petit Sep 22 '11 at 12:41
add comment

try item.Title in your source code

share|improve this answer
    
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
add comment

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");
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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