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.

1 Answer 1

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

2
  • Are you using Javascript here ? Commented Feb 27, 2012 at 14:16
  • Nope, but the principles are exactly the same. Use SPFileCollection.Add instead of adding items to a list. Commented Feb 29, 2012 at 21:28

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.