1# @ohos.app.form.formAgent (FormAgent) (System API)
2
3The **FormAgent** module provides APIs related to the widget agent. Currently, you can use the APIs to request to publish widgets only.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> The APIs provided by this module are system APIs.
9
10## Modules to Import
11
12```ts
13import { formAgent } from '@kit.FormKit';
14```
15
16## requestPublishForm
17
18requestPublishForm(want: Want, callback: AsyncCallback<string>): void
19
20Requests to publish a widget to the widget host. This API uses an asynchronous callback to return the result. The widget host is usually the home screen.
21
22**Required permission**: ohos.permission.AGENT_REQUIRE_FORM
23
24**System capability**: SystemCapability.Ability.Form
25
26**System API**: This is a system API.
27
28**Parameters**
29
30| Name  | Type                               | Mandatory| Description                                                        |
31| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
32| want     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Publish request, which must contain the following fields:<br>**bundleName**: bundle name of the target widget.<br>**abilityName**: ability of the target widget.<br>parameters:<br>- **ohos.extra.param.key.form_dimension**: dimension of the target widget.<br>- **ohos.extra.param.key.form_name**: name of the target widget.<br>- **ohos.extra.param.key.module_name**: module name of the target widget.|
33| callback | AsyncCallback&lt;string&gt;         | Yes  |  Callback used to return the widget ID.|
34
35**Error codes**
36
37| ID| Error Message|
38| -------- | -------- |
39| 202 | The application is not a system application. |
40| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. |
41| 16500050 | IPC connection error. |
42| 16500100 | Failed to obtain the configuration information. |
43| 16501000 | An internal functional error occurred. |
44| 16501008 | Waiting for the form addition to the desktop timed out. |
45
46For details about the error codes, see [Form Error Codes](errorcode-form.md).
47
48**Example**
49
50```ts
51import { formAgent } from '@kit.FormKit';
52import { Want } from '@kit.AbilityKit';
53import { BusinessError } from '@kit.BasicServicesKit';
54
55let want: Want = {
56  bundleName: 'com.ohos.exampledemo',
57  abilityName: 'FormAbility',
58  parameters: {
59    'ohos.extra.param.key.form_dimension': 2,
60    'ohos.extra.param.key.form_name': 'widget',
61    'ohos.extra.param.key.module_name': 'entry'
62  }
63};
64try {
65  formAgent.requestPublishForm(want, (error: BusinessError, data: string) => {
66    if (error) {
67      console.error(`callback error, code: ${error.code}, message: ${error.message})`);
68      return;
69    }
70    console.log(`formAgent requestPublishForm, form ID is: ${JSON.stringify(data)}`);
71  });
72} catch (error) {
73  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`);
74}
75```
76
77## requestPublishForm
78
79requestPublishForm(want: Want): Promise&lt;string&gt;
80
81Requests to publish a widget to the widget host. This API uses a promise to return the result. The widget host is usually the home screen.
82
83**Required permission**: ohos.permission.AGENT_REQUIRE_FORM
84
85**System capability**: SystemCapability.Ability.Form
86
87**System API**: This is a system API.
88
89**Parameters**
90
91| Name         | Type                                                        | Mandatory| Description                                                        |
92| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
93| want     | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Publish request, which must contain the following fields:<br>**bundleName**: bundle name of the target widget.<br>**abilityName**: ability of the target widget.<br>parameters:<br>- **ohos.extra.param.key.form_dimension**: dimension of the target widget.<br>- **ohos.extra.param.key.form_name**: name of the target widget.<br>- **ohos.extra.param.key.module_name**: module name of the target widget.|
94
95**Return value**
96
97| Type         | Description                               |
98| :------------ | :---------------------------------- |
99| Promise&lt;string&gt; | Promise used to return the widget ID.|
100
101**Error codes**
102
103| ID| Error Message|
104| -------- | -------- |
105| 202 | The application is not a system application. |
106| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. |
107| 16500050 | IPC connection error. |
108| 16500100 | Failed to obtain the configuration information. |
109| 16501000 | An internal functional error occurred. |
110| 16501008 | Waiting for the form addition to the desktop timed out. |
111
112For details about the error codes, see [Form Error Codes](errorcode-form.md).
113
114**Example**
115
116```ts
117import { formAgent } from '@kit.FormKit';
118import { Want } from '@kit.AbilityKit';
119import { BusinessError } from '@kit.BasicServicesKit';
120
121let want: Want = {
122  bundleName: 'com.ohos.exampledemo',
123  abilityName: 'FormAbility',
124  parameters: {
125    'ohos.extra.param.key.form_dimension': 2,
126    'ohos.extra.param.key.form_name': 'widget',
127    'ohos.extra.param.key.module_name': 'entry'
128  }
129};
130try {
131  formAgent.requestPublishForm(want).then((data: string) => {
132    console.log(`formAgent requestPublishForm success, form ID is : ${JSON.stringify(data)}`);
133  }).catch((error: BusinessError) => {
134    console.error(`promise error, code: ${error.code}, message: ${error.message})`);
135  });
136} catch (error) {
137  console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`);
138}
139```
140