Launching a Form from an external application - How to get the form URI?

1. What is the problem? Be very detailed.

I'm trying to modify an external application to directly open a particular form in ODK Collect to start filling it. The URIs of the forms to be opened will be pushed from the backend to this external application. I am using Kobotoolbox to create the forms. How can I get the form URI which Collect uses for a form, before the user fetches that form?

2. What app or server are you using and on what device and operating system? Include version numbers.

Kobotoolbox as the server.
App version is 1.30.1

3. What you have you tried to fix the problem?

Following the documentation provided here : https://docs.getodk.org/launch-collect-from-app/#launching-the-form-list-or-instance-list-activity
I am able to launch a form with URI
content://org.odk.collect.android.provider.odk.forms/forms/1,
content://org.odk.collect.android.provider.odk.forms/forms/2, etc..
But these correspond to the forms residing on ODK Collect currently (in order of date added from oldest to newest). I need to know these form IDs before the user syncs Collect and fetches the forms.

4. What steps can we take to reproduce the problem?
NA

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.
NA

Hi @ks_1
If you don't have those forms downloaded you don't know their ids in the Collect's database and you shouldn't try to predict them. What you can do is read the whole database and filter the values using formId and version (you should know them because those a part of a form so you can set them) and then read the id of a matching form in order to start it.

Collect tester shows how to read all the available forms:

there you should be able to filter the list using those parameters formId and version to find the id you want.

Thanks a lot @Grzesiek2010! Would it be right to say that the formId is the same as the formid auto generated by Kobotoolbox?

image

I don't know how Kobotoolbox works but if it's the same as formid in the output xml form then yes. In ODK we are able to specify that in Settings worksheet.

1 Like

@Grzesiek2010
Thanks a lot for your support on this, I got it working great.
Can I also change the login credentials of Collect from an external app? So that users don't have to go to Collect to sign in.

It is possible to override url/username/password used in ODK Collect when you download/upload forms via an external app. You can test that option in Collect Tester.
But those credentials will be used just for downloading/uploading triggered from that external app so if you open the original ODK Collect app you will still have your old credentials.

I followed the documentation here https://docs.getodk.org/launch-collect-from-app/#launching-the-form-list-or-instance-list-activity

I am using React Native but the code for Launching ODK is in Java which is called in my react native code using native modules.

      Intent intent = new Intent(Intent.ACTION_EDIT);
            Uri uri = Uri.parse("content://org.odk.collect.android.provider.odk.forms/forms/" + formID);
            intent.setData(uri);
            intent.setType("vnd.android.cursor.dir/vnd.odk.form");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.getReactApplicationContext().startActivity(intent);

This is what I have. The problem is it opens ODK but shows the entire list of forms and not a specific form. The formid is passed in the function correctly.

Got the solution. No need to put the intent.setType(...)

Hi, could you please help with how to read the whole database using api end point if it's possible? I tried downloading the form using the download url that comes with XML response of openrosa form list apis but the downloaded content do not have any formID apart from xml form id.

Are you talking about reading the whole local forms database (from your phone) or the remote one (central)?