4

Is there some helper method for example in CSOM API to check if Microsoft.SharePoint.Client.ListTemplateType is a list or a library, something like

IsLibrary(ListTemplateType type)

2 Answers 2

5

Can you use List.BaseType? Something like:

var list = ctx.Web.Lists.GetByTitle(listTitle);
ctx.Load(list);
ctx.ExecuteQuery();

bool isDocLib = list.BaseType == BaseType.DocumentLibrary;

Sorry if that's not valid C#, here's what I tested in JavaScript:

ctx=SP.ClientContext.get_current()
list=ctx.get_web().get_lists().getByTitle("Pages")
ctx.load(list)
ctx.executeQueryAsync(
    function() {
        console.log(list.get_baseType() === SP.BaseType.documentLibrary)
    },
    function() {
        console.log(arguments)
    }
)
2
  • this seems to be it - an easy way to check if a list is a library of any type (document, picture etc)
    – thomius
    Commented Feb 14, 2014 at 21:19
  • In SPO, and possibly 2013 On-Prem onwards, you should also check for ListTemplateType.MySiteDocumentLibrary // MySiteDocumentLibrary.Value = 700 Commented Mar 23, 2017 at 19:19
2

To determine ListTemplateType the following method could be used:

/// <summary>
/// Get List Template Type
/// </summary>
/// <param name="list">List client object</param>
/// <returns>returns List template type </returns>
private static ListTemplateType GetListTemplateType(List list)
{
      try
      {
          return (ListTemplateType)Enum.Parse(typeof(ListTemplateType), list.BaseTemplate.ToString());
      }
      catch
      {
          throw new InvalidEnumArgumentException("ListTemplateType", list.BaseTemplate, typeof(ListTemplateType));
      }
}

List.BaseTemplate property is used for resolving ListTemplateType

Usage

using (var ctx = new ClientContext(url))
{

     var list = ctx.Web.Lists.GetByTitle(listTitle);
     ctx.Load(list, l => l.BaseTemplate);
     ctx.ExecuteQuery();

     var listType = GetListTemplateType(list);
     if (listType == ListTemplateType.DocumentLibrary)
     {
         //...
     }

}
1
  • In SPO, and possibly 2013 On-Prem onwards, you should also check for ListTemplateType.MySiteDocumentLibrary // MySiteDocumentLibrary.Value = 700 Commented Mar 23, 2017 at 19:20

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.