Associate and Search on a document metadata using Google Drive APIs - google-drive-api

Is there a way I can have my own metadata associated to a document that I am uploading to Google Drive? If yes, how do I mention that metadata in the API?
Also, can I perform a search using the metadata using the APIs. Say if there are 3 documents with the metadata tag as 'cooking' ... can I use the search API where I send the tag 'cooking' as a search query and I get a list of all the docs associated with that metadata.
Regards, Saurabh

Yes, see custom file properties. https://developers.google.com/drive/web/properties
To search for custom file properties, add this to your search parameters:
properties has { key = 'foo' and value = 'bar' and visibility = 'private'}
Note that all 3 parts are required.

Related

Is there anyway to use the Google Drive API to list which files are sharable to anyone with a link or are publicly searchable?

I currently have a lot of files in Google Drive and would like to programmatically find which ones might be available to the web or available to those with the link of a file. I'd like to search which files were set to "Anyone with the link" or "Public on the web". The picture below shows this. Is there a way to easily list this with the Drive v3 API?
You just have to call Files: list and use the search query parameter (q) to list only the files with certain visibility settings. For example, if you want to retrieve both the files shared with anyone with link and anyone on the Internet, you can set q the following way:
q: "visibility='anyoneCanFind' or visibility='anyoneWithLink'"
From the docs:
visibility (=, '!='): The visibility level of the file. Valid values are anyoneCanFind, anyoneWithLink, domainCanFind, domainWithLink, and limited.
Note:
The rest of parameters provided to Files: list will depend on the exact search you want to make (from your Drive, from a shared Drive, etc.). I'm assuming this is not part of your question.
Reference:
Files: list
Search query terms

Location of a comment in its corresponding Google doc

Given a google doc retrieved through the Google docs API and its comments retrived with the Drive API,
How do you know where in the document a comment corresponds?
I've been inspecting all the fields and couldn't find a relation except the quotedFileContent that contains the commented text in the doc, but this would resolve ambiguously if that text is duplicated.
You cannot retrieve the location of a comment using Drive API.
As you can see in the official documentation, the Comment resource has an anchor field that provides information on the region of the document. Nevertheless, if you use the API to get information about a comment, you will see that the anchor field in the response is actually a string identifier composed of a kix prefix and a certain anchor ID as a suffix, which don't really give you an idea of where the comment is located.
The problem is not just about retrieving the locations to which a comment is anchored, but also about creating anchored comments in a desired location using the API, as you can see here.
Basically, as it is currently implemented, this anchor tool is meant for third-party integrations to use this API to specify their own custom anchor data.
There are several open cases in Issue Tracker related to this:
https://issuetracker.google.com/issues/36756056
https://issuetracker.google.com/issues/36763384
Reference:
https://developers.google.com/drive/api/v3/manage-comments
https://www.youtube.com/watch?v=ZBU52nacbLw

Google Drive API get only one property

I'm using the Google Drive API V3 to get custom properties from alot of files.
I only need one property out of the properties collection. Is it possible to set the fields attribute so i can only get the one property?
Thank you in advance!
fields is part of the Standard Query Parameters valid for all of the Google discovery apis.
fields Selector specifying a subset of fields to include in the
response. For more information, see the partial response section in
the Performance Tips document. Use for better performance.
Yes you can specify fields.

If you are using the Documents List API and the changes feed, how does one exclude files a user has open?

The drive SDK has includeSubscribed for drive.changes.list. Is there an equivalent in Documents List ?
There is no equivalent according to my current knowledge.

Search for Google Drive files based on new properties resource

Google just introduced a new feature in Google Drive SDK which is the ability to set properties to files, as key/value pairs.
According to the blog post on Google Developers blog we can use these properties as searchable fields. However I cannot see in the documentation how to search for files using these properties.
For example: retrieve all files where property A has the value X.
I know the feature is brand new but I could really make use of this in my current project. Have I missed anything ?
Well I find, its possible to search on drive based on properties param.
Check doc. https://developers.google.com/drive/web/search-parameters
Code Snippet :
resp = newDriveService.files().list(q="properties has { key='customKeyA' and value='customKeyAValue2' and visibility='PUBLIC' }").execute()
Note : you have to specify all 3 params viz. key, value and visibility while searching. If you don't use it will throw Invalid Params exception.
Unfortunately, we don't currently support this. This is currently a high priority for Google and they are working on it. Stay tuned.