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:

uwpdoclib1

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

uwpdoc2

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:

<Capabilities>
    <uap:Capability Name="documentsLibrary" />
  </Capabilities>
</Package>

If there are already Capabilities then just add the line:

    <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:

      <Extensions>
        <uap:Extension Category="windows.fileTypeAssociation">
          <uap:FileTypeAssociation Name=".txt">
            <uap:DisplayName>Text</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>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <uap:Capability Name="documentsLibrary" />
  </Capabilities>
</Package>

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
Twitter

© Paul Michaels 2024