7

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();
listItemInfo.set_leafName('Test');
var listItem = myList.addItem(listItemInfo);
listItem.set_item('Title', 'MyTest');
listItem.set_item('ContentTypeId', myContentType.get_id());
listItem.update();
context.load(myList);
context.executeQueryAsync();

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.

2

Philippe,

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";
ctx.ExecuteQuery();

.b

  • 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 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.