Query Files by Metadata

we trying to upload the documents from box application to our project from a specific folder. In this folder users may update the existing files and we need to upload those files and I wanted to extract those file. In the documentation I’ve observed this API but I am not understanding what values do I need to use in the variables

  1. from
  2. and value to set in the field mQuery.setFields(“metadata.enterprise_341532.test.customField”)
    Could you please tell me what values do I need to give in these fields.
String from = "enterprise_341532.test";
String query = "testfield = :arg";
String ancestorFolderId = "0";
MetadataQuery.OrderBy primaryOrderBy = MetadataQuery.OrderBy.ascending("primarySortKey");
MetadataQuery.OrderBy secondaryOrderBy = MetadataQuery.OrderBy.ascending("secondarySortKey");

MetadataQuery mQuery = new MetadataQuery(from);
mQuery.setOrderBy(primaryOrderBy, secondaryOrderBy);
mQuery.addParameter("arg", "test");
BoxResourceIterable<BoxItem.Info> results = MetadataTemplate.executeMetadataQuery(api, mQuery);

for (BoxItem.Info r: results) {
	if (r instanceof BoxFile.Info) {
        BoxFile.Info fileInfo = (BoxFile.Info) r;
        Metadata fileMetadata = fileInfo.getMetadata("test", "enterprise_341532");
        String customFieldValue = fileMetadata.getString("/customField");
    } else if (r instanceof BoxFolder.Info) {
        BoxFolder.Info folderInfo = (BoxFolder.Info) r;
        Metadata folderMetadata = folderInfo.getMetadata("test", "enterprise_341532");
        String customFieldValue = folderMetadata.getString("/customField");

> Blockquote

Hi @user57 , welcome back!

Would this sample code help?

Let us know

The Metadata Query feature allows you to find files and folders based on the values of the Box Metadata Instances that are attached to those files/folders (more here: https://developer.box.com/guides/metadata/queries/)

The meanings of the various things being set on the mQuery object (via the constructor and via the explicit setXXX calls) can be found on these pages at the Box Developer site:

In essence, the code snippet you provided is looking for all files and folders

  • in the caller’s root folder (“ancestorFolderId = 0”)
  • which have an attached Metadata Instance based on the “test” Metadata Template (from “enterprise_341532.test”)
  • where the Instance’s “testfield” field (“testfield = :arg”) has a value of “test” (“mQuery.addParameter(“arg”, “test”)”)
    For each file/folder that matches the above conditions, the result set will contain some basic file/folder information and the value of the “customField” field of the Metadata Instance (mQuery.setFields(“metadata.enterprise_341532.test.customField”))

@rbarbosa I’ve tried this method to get the updated files
“public static void updatedDateFilterExample(BoxSearchParameters bsp, BoxSearch bs)”
but I am getting this error

How can I resolve it and I can see we are not using any folder in this code then from which folder we will get the files.

And one more thing I am user of Box@IBM.

Hi @user57 ,

I was at one of IBM offices in Atlanta recently for a watson X workshop, very impressive.

I’m not a Java developer, so I’m out ofmy depth, hopefully @user62 (Lyall) can help further.

Best regards.

It looks like your build is missing the bcprov-jdk18on v1.77 library.

The readme file on the java SDK repo (box-java-sdk/README.md at main · box/box-java-sdk · GitHub, see the “JVM” section) has a list of the libraries that you’ll need for using the SDK.

Check that you’ve got all of these required dependencies included in your build.

@user62 thanks for your reply will update the dependencies and try it again.

Hi @user62 I’ve added all the dependencies and I am getting this error. I didn’t found any config.json in my custom application so I’ve just followed the config.json file in the git hub and gave my clienId and clientSecret and remaining fields I’ve left it as empty. Could you please help me to rectify this problem.

and I am using Client Credentials Grant security to my application.

The "keyPair" is null part of the error message makes me wonder if you’re running into the incompatible keypair issue mentioned here: box-java-sdk/README.md at main · box/box-java-sdk · GitHub (which is part of a larger section for setting up the bouncy-castle encryption stuff: box-java-sdk/README.md at main · box/box-java-sdk · GitHub)

Maybe either of the two solutions listed (the SDK upgrade or manually generating your keypair) might get you past this error.

There’s also a section in the Box Developer Documentation that outlines setting up the keypair (https://developer.box.com/guides/authentication/jwt/jwt-setup/) for your application.