SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a new item in my Entreprise Wiki Pages Document Library. It's associated with a News Content type. I was able to retrieve the SP.List object (myList), the SP.ContentType object (myContentType) but now I just need to match both. Here is the code that I've tryed.

var listItemInfo = new SP.ListItemCreationInformation();
var listItem = myList.addItem(listItemInfo);
listItem.set_item('Title', 'MyTest');
listItem.set_item('ContentTypeId', myContentType.get_id());

And I have this error :

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

Can someone give me some clue ? I don't have an actual file to upload. On the UI, I juste click the new document for my content type.

share|improve this question


As the error message says, you need to use SPFileCollection.Add on document libraries and you need to add the file content from somewhere.

Depending on which context you have, you may want to try something like this:

byte[] content = Encoding.Unicode.GetBytes("file_content");
ClientContext ctx = new ClientContext(new Uri("http://your_site/"));
List l = ctx.Web.Lists.GetByTitle("Shared Documents");
File f = l.RootFolder.Files.Add(
    new FileCreationInformation() { Url = "File.aspx", Overwrite = true, Content = content }
ListItem item = f.ListItemAllFields;
item["ContentType"] = "Basic Page";


share|improve this answer
Are you using Javascript here ? – Philippe Lavoie Feb 27 '12 at 14:16
Nope, but the principles are exactly the same. Use SPFileCollection.Add instead of adding items to a list. – Bjørn Furuknap Feb 29 '12 at 21:28

Your Answer


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.