5

Given files in Drive with an (arbitrary) extension *.abc, this code...

gapi.load("picker", { "callback": function () {
    if (!picker) {
        var view = new google.picker.DocsView(google.picker.ViewId.DOCS);

        view.setMimeTypes("application/vnd.google.drive.ext-type.abc");

        view.setMode(google.picker.DocsViewMode.LIST);
        picker = new google.picker.PickerBuilder();
        picker.setTitle(TEXT.PICKER_PROMPT);
        picker.setAppId(CONST.APP_ID);
        picker.addView(view);
        picker.setOAuthToken(session.OAuthToken.access_token);
        picker.setCallback(pickerCallback);
        picker.setInitialView(view);
    };
    picker.build().setVisible(true);
));

...doesn't find any of the existing 'abc' files in drive. These files are of mime type text/xml, and the following line DOES find them:

view.setMimeTypes("text/xml");

Why doesn't the search by extension work?

Rubén
  • 34,714
  • 9
  • 70
  • 166
HeyHeyJC
  • 2,627
  • 1
  • 18
  • 27
  • I think it makes sense that it doesn't work with extensions since the function sets the mime types and not extensions. In drive you can find that every file has a mime type, but it is not the same for extensions, you can have files without extensions. This is easier to see with native Google files (Spreadsheets, docs). – Gerardo Dec 01 '15 at 22:30

2 Answers2

7

For those finding this from Google, the question wasn't as daft as it sounded - there is a (pseudo) mime-type for each extension in the Drive world, but it's not usable in that way, at least not in the Picker.

A workable (ie user-friendly) solution is to use a query on the view:

view.setQuery("*.abc");

For completeness:

gapi.load("picker", { "callback": function () {
    if (!picker) {
        var view = new google.picker.DocsView(google.picker.ViewId.DOCS);

        view.setMimeTypes("text/xml");
        view.setMode(google.picker.DocsViewMode.LIST);
        view.setQuery("*.abc");

        picker = new google.picker.PickerBuilder();
        picker.setTitle(TEXT.PICKER_PROMPT);
        picker.setAppId(CONST.APP_ID);
        picker.addView(view);
        picker.setOAuthToken(session.OAuthToken.access_token);
        picker.setCallback(pickerCallback);
        picker.setInitialView(view);
    };
    picker.build().setVisible(true);
));
HeyHeyJC
  • 2,627
  • 1
  • 18
  • 27
  • Thank you so much for this! I was trying to find out how to use `setQuery` in a view and this code explained everything quite nicely! – NobleUplift Jun 11 '19 at 20:11
2

Adding on to HeyHeyJC's answer, you can use a double pipe (||) separating each file extension, if you want to use multiple.

For example, view.setQuery("*.abc || *.def || *.ghi");