UWP Accessing Documents Library

November 11, 2016

Accessing the documents library from a UWP app is frowned upon by Microsoft; however, it is possible. Here is some code that will access the library:

var files = await KnownFolders.DocumentsLibrary.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);
foreach (var f in files)
    BasicProperties props = await f.GetBasicPropertiesAsync();

This will access the library and get the properties for each file. However, just running it will fail with this error:


So, you’ll probably get this error and, like me (and not for the first time), go looking for it here:


Of course, you won’t find it (because it’s not there), and then you’ll turn to Google. If that brought you here then you’re next step is to open the manifest file directly:

[code lang=“XML”] … <uap:Capability Name=“documentsLibrary” />

If there are already Capabilities then just add the line:

[code lang="XML"]
    <uap:Capability Name="documentsLibrary" />

Note: you need the uap prefix.

And, that’s not all. Next you need to tell it which documents it can access:

[code lang=“XML”] <uap:Extension Category=“windows.fileTypeAssociation”> <uap:FileTypeAssociation Name=“.txt”> uap:DisplayNameText</uap:DisplayName> uap:SupportedFileTypes uap:FileType.jpg</uap:FileType> uap:FileType.txt</uap:FileType> uap:FileType.gif</uap:FileType> uap:FileType.doc</uap:FileType> uap:FileType.xls</uap:FileType> </uap:SupportedFileTypes> </uap:FileTypeAssociation> </uap:Extension> <uap:Capability Name=“documentsLibrary” />

And that's it.  I can understand why they have all these restrictions, but they can be frustrating for programmers.

Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2022