c# - Create folder on SharePoint document library using client object model -
i want create folder on sharepoint document library using client object model (c#). below code that.
contenttypecollection listcontenttypes = list.contenttypes; clientcontext.load(listcontenttypes, types => types.include (type => type.id, type => type.name, type => type.parent)); var result = clientcontext.loadquery(listcontenttypes.where(c => c.name == "folder")); clientcontext.executequery(); contenttype foldercontenttype = result.firstordefault(); listitemcreationinformation newiteminfo = new listitemcreationinformation(); newiteminfo.underlyingobjecttype = filesystemobjecttype.folder; newiteminfo.leafname = foldername; listitem newlistitem = list.additem(newiteminfo); newlistitem["contenttypeid"] = foldercontenttype.id.tostring(); newlistitem["title"] = foldername; ewlistitem.update(); clientcontext.load(list); clientcontext.executequery();
now have thousands of folders created on library. there other way can using bulk operation, client server connection called once.
problem is taking time create folder because each , every folder call sharepoint object.
it's easy. add of folders wish create list , loop through list , create list items before running executequery function:
list<string> foldernames = new list<string>(); foldernames.add("folder 1"); foldernames.add("folder 2"); foldernames.add("folder 3"); clientcontext context = new clientcontext("https://sharepoint.site/test"); list list = context.web.lists.getbytitle("documents"); var folder = list.rootfolder; context.load(folder); context.executequery(); foreach (string foldername in foldernames) { listitemcreationinformation newiteminfo = new listitemcreationinformation(); newiteminfo.underlyingobjecttype = filesystemobjecttype.folder; newiteminfo.leafname = foldername; listitem newlistitem = list.additem(newiteminfo); newlistitem["title"] = foldername; newlistitem.update(); } context.executequery();
works charm. need stage of folder paths/names before run csom code.
Comments
Post a Comment