0

I want to create a new Document Library using server side code and I try to use SPListTemplateType.DocumentLibrary inside SPWeb.Lists.Add() method but it won't allow me. I have to used

 SPListTemplate listTemplate = web.ListTemplates["Document Library"] 

instead, anyone know the reason for this?

1
  • var listTemplate = web.ListTemplates["Document Library"]; web.Lists.Add("Title", "Description", listTemplate); Works like a charm. Commented Sep 13, 2016 at 8:04

1 Answer 1

2

If you take a look at the SPListTemplateType construct, it's an enum

SPListTemplateType enumeration

so when you call SPListTemplateType.DocumentLibrary, what you actually get back is the list type ID, in this example 101.

Whereas in second instance, you get actual SPListTemplate object that you can work with.

You can use SPListTemplateType to create library in the following way:

spWeb.Lists.Add("List Name", "List Discription",SPListTemplateType.PictureLibrary);

I know that you mentioned that it didn't work but could you provide any code samples or exception you are getting, if you are getting any? Until we get some more information, it's all speculation.

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.