Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, June 12, 2019 10:43 AM
Hello,
I want to get specific folder from document library having more than 10k folders. I want to achieve it using CSOM code. I have found below code but i am getting conversion error:
var web = clientContext.Web;
var list = web.Lists.GetByTitle("listName");
clientContext.Load(list);
clientContext.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>";
List<ListItem> items = new List<ListItem>();
do
{
ListItemCollection listItemCollection = list.GetItems(camlQuery);
clientContext.Load(listItemCollection);
clientContext.ExecuteQuery();
//Adding the current set of ListItems in our single buffer
items.AddRange(listItemCollection);
//Reset the current pagination info
camlQuery.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;
} while (camlQuery.ListItemCollectionPosition != null);
var filteritems = items.Where(tt => tt.FieldValues["FileRef"].ToString().Equals(Request.QueryString["foldername"], StringComparison.CurrentCultureIgnoreCase));
All replies (3)
Thursday, June 13, 2019 8:30 AM âś…Answered
Hi,
Please check the code below.
string siteURL = "http://sp2013/sites/team";
string folderName = Request.QueryString["foldername"];
ClientContext ctx = new ClientContext(siteURL);
ctx.Credentials = new NetworkCredential("username", "password", "domain");
Web web = ctx.Web;
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>";
do
{
List list = ctx.Web.Lists.GetByTitle("TestList");
ListItemCollection listItems = list.GetItems(camlQuery);
ctx.Load(listItems);
ctx.ExecuteQuery();
camlQuery.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
var folders = listItems.Where(l => l["FileLeafRef"].Equals(folderName)).ToList();
foreach (var folder in folders)
{
Console.WriteLine("FolderUrl: " + folder["FileRef"]);
}
}while (camlQuery.ListItemCollectionPosition != null);
Best Regards,
Dennis
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
SharePoint Server 2019 has been released, you can click
here to download it.
Click
here to learn new features. Visit the dedicated
forum to share, explore and talk to experts about SharePoint Server 2019.
Thursday, June 13, 2019 2:32 AM
Hi,
If you want to get files from a specific folder, we can use Web.GetFolderByServerRelativeUrl method to achieve it.
The following example code for your reference.
string siteURL = "http://sp2013/sites/team";
string folderRelativeUrl = "DocumentLibrary/Folder1";
ClientContext clientContext = new ClientContext(siteURL);
clientContext.Credentials = new NetworkCredential("username", "password", "domain");
Web web = clientContext.Web;
Folder folder = web.GetFolderByServerRelativeUrl(folderRelativeUrl);
clientContext.Load(folder.Files);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File file in folder.Files)
{
Console.WriteLine(file.Name);
}
More information:
Best Regards,
Dennis
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
SharePoint Server 2019 has been released, you can click
here to download it.
Click
here to learn new features. Visit the dedicated
forum to share, explore and talk to experts about SharePoint Server 2019.
Thursday, June 13, 2019 6:33 AM
Thanks Dennis for the reply. I wanted to get a specific folder from thousands of folder. This is what i came up with.
Let me know if it can still be optimized.
bool oFlag = false;
oWeb = clientContext.Web;
oList = clientContext.Web.Lists.GetByTitle("TestList");
clientContext.Load(oWeb);
clientContext.Load(oList);
clientContext.ExecuteQueryRetry();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>";
if (oList != null)
{
List<ListItem> items = new List<ListItem>();
do
{
ListItemCollection listItemCollection = oList.GetItems(camlQuery);
clientContext.Load(listItemCollection, collection => collection.Include(item => item.DisplayName), collection => collection.Include(item => item.FileSystemObjectType), collections => collections.ListItemCollectionPosition);
clientContext.ExecuteQuery();
//Adding the current set of ListItems in our single buffer
items.AddRange(listItemCollection);
//Reset the current pagination info
camlQuery.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;
foreach (var item in items)
{
if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
if (item.DisplayName.Equals(Request.QueryString["foldername"], StringComparison.CurrentCultureIgnoreCase))
{
Folder rootFolder = item.Folder;
......................
// break the for
oFlag = true;
break;
}
}
}
// break the while
if (oFlag==true)
{
break;
}
items.Clear();
} while (camlQuery.ListItemCollectionPosition != null);
}