sharepoint - Retrieve All Documents from all Subfolders in a Document Library - CSOM -
i using client side object model approach c# in order retrieve list items document library containing sub folders. checked out msdn documentation , stuck why cannot field property, or if doing right.
networkcredential credentials = system.net.credentialcache.defaultnetworkcredentials; clientcontext clientcontext = new clientcontext(resources.defaultspsite); clientcontext.credentials = credentials; //load libraries sharepoint //web site = clientcontext.web; clientcontext.load(clientcontext.web.lists); clientcontext.executequery(); //list shareddocumentslist = clientcontext.web.lists.getbytitle("testldocs"); //camlquery camlquery = new camlquery(); //camlquery.viewxml = @"<view scope='recursive'><query></query></view>"; foreach (list list in clientcontext.web.lists) { clientcontext.load(list); clientcontext.executequery(); //list.templatefeatureid.tostring().equals("") && string basetype = list.basetype.tostring(); string listtitle = list.title.tostring(); if (list.basetype.tostring().equals("documentlibrary", stringcomparison.invariantcultureignorecase) && list.title.tostring().equals("testldocs", stringcomparison.invariantcultureignorecase)) { foreach (folder subfolder in list.rootfolder.folders) { foreach (file f in subfolder.files) { console.writeline((string) f.title); } } } } }
the error receiving "foreach(file f in subfolder.files)" collection may not initialized error. there anyway field values of documents in every subfolder within document library using csom?
i know can type field values list item ie (listitem["fieldname"]). should go route instead?
some recommendations:
1) prefer clientruntimecontext.loadquery method load specific lists, example:
var lists = ctx.loadquery(ctx.web.lists.where(l => l.basetype == basetype.documentlibrary)); ctx.executequery();
2) since sharepoint scom supports request batching recommended minimize number of requests server. following example demonstrates how perform single request server in order load files document libraries:
foreach (var list in lists) { var items = list.getitems(createallfilesquery()); ctx.load(items, icol => icol.include(i => i.file)); results[list.title] = items.select( i=>i.file); } ctx.executequery();
3) prefer load files via caml query demonstrated below:
public static camlquery createallfilesquery() { var qry = new camlquery(); qry.viewxml ="<view scope=\"recursiveall\"><query><where><eq><fieldref name=\"fsobjtype\" /><value type=\"integer\">0</value></eq></where></query></view>"; return qry; }
then following example return files in library:
var items = list.getitems(createallfilesquery()); ctx.load(items, icol => icol.include(i => i.file)); ctx.executequery(); var files = items.select( i=>i.file).tolist();
it more optimized way of loading specific lists performance perspective
complete example
how load files document libraries using sharepoint csom:
using (var ctx = new clientcontext(weburi)) { var results = new dictionary<string, ienumerable<file>>(); var lists = ctx.loadquery(ctx.web.lists.where(l => l.basetype == basetype.documentlibrary)); ctx.executequery(); foreach (var list in lists) { var items = list.getitems(createallfilesquery()); ctx.load(items, icol => icol.include(i => i.file)); results[list.title] = items.select( i=>i.file); } ctx.executequery(); //print results foreach (var result in results) { console.writeline("list: {0}",result.key); foreach (var file in result.value) { console.writeline("file: {0}", file.name); } } }
Comments
Post a Comment