I have a custom Document Library that has a custom content type which is an extension of a Folder (just adding some more fields). I'm trying to, in code, add a new item of this content type to the library. But I get the error:

To add an item to a document library, use SPFileCollection.Add()

But what I am adding is not a file, and all the overloads for that method require some kind of file stream or object. I've tried:

SPListItem newCustomFolder = docLibrary.Folders.Add();
SPListItem newCustomFolder = docLibrary.AddItem();
SPListItem newCustomFolder = docLibrary.Items.Add();

And trying to declare it as SPFolder doesn't match the return of the above functions nor does accept setting fields.

How do I do this?

1 Answer 1


Okay, I had an idea, and creating it as a folder, then getting the item representation of that folder, and then changing that items content type and setting all the custom fields seems to have worked.

SPFolderCollection folders = docLibrary.RootFolder.SubFolders;
SPFolder newFolder = folders.Add(SALAE_docLibrary.RootFolder.Url + "/foo");
SPListItem newCustomFolder = newFolder.Item;
//set content type, set fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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