2 years ago
#18937
Cherylaksh
Get List of files from folders and subfolders from sharepoint using REST API C#
I want to populate a list in my code, with the fileNames present in a folder and subfolders in Sharepoint Onpremises path. Here is my code so far:
private void GetListOfFiles()
{
Uri repoUri = new Uri(repositoryPath);
WebResponse endpointResponse = (System.Net.HttpWebResponse)(GenerateRequest(repoUri, credentials)).GetResponse();
Console.WriteLine("Response received for Request ...");
Stream webStream = endpointResponse.GetResponseStream();
// Dont Know how to get the filenames from the stream and put it in List<string>
}
public HttpWebRequest GenerateRequest(Uri fullUri, ICredentials credentials)
{
var siteURL = "https://domain.sharepoint.net/sites/MyTestsite";
var relativeURL = fullUri.PathAndQuery;
var commandString = string.Format("/_api/web/GetFolderByServerRelativeUrl('{0}')?$expand=Folders,Files", relativeURL);
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteURL + commandString);
endpointRequest.Credentials = credentials;
endpointRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
endpointRequest.Method = "GET";
endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
Console.WriteLine("Request created for getting list of files ...");
return endpointRequest;
}
My response does not give me the list of files in all folders and subfolders, and even if it does, can someone help me on parsing the same to get the List populated. Is /_api/web/GetFolderByServerRelativeUrl('{0}')?$expand=Folders,Files even the right way to get all the files from folders and subfolders?
c#
rest
sharepoint
httpwebrequest
office365api
0 Answers
Your Answer