Search code examples
c#sharepointcsomsharepoint-appssharepointdocumentlibrary

How to itetrate through files in a document library


I've been creating a provider hosted app and I'm stuck at retrieving Documents titles using CSOM C# my code is as follows.

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
    Web web = clientContext.Web;
    ClientOM.List list = clientContext.Web.Lists.GetByTitle("Documents");

    clientContext.Load(list);
    clientContext.ExecuteQuery();

    Response.Write(list.Title);

    Folder folder = list.RootFolder;
    clientContext.Load(folder);
    clientContext.ExecuteQuery();
    string count = folder.ItemCount.ToString();
    Response.Write("Folder variable count: "+count);

    ClientOM.FileCollection fcollection = folder.Files;
    clientContext.Load(folder.Files);
    clientContext.ExecuteQuery();
    Response.Write("\n Files count: " + folder.Files.Count);
    foreach (ClientOM.File f in folder.Files)
    {
        Response.Write(f.Title);


    }
}

Output : Folder variable count: 6 Files count: 0


Solution

  • Thanks for all the replies. As i found that my code was right I started looking for some administrative aspect of the apps. One of my colleagues pointed out that the Provider hosted apps need some specific permisssions set.

    I went to the Appmanifest.xml file and then I added this code.

    <AppPermissionRequests>
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />
      </AppPermissionRequests>