[comment]: # ({8c618db1-8c618db1})
# template.massadd

[comment]: # ({/8c618db1-8c618db1})

[comment]: # ({399b9834-399b9834})
### 描述

`object template.massadd(object parameters)`

此方法允许同时向指定模板添加多个关联的objects

::: noteclassic
此方法仅限*管理员*和*超级管理员*用户类型使用。调用该方法的权限可通过用户角色设置进行撤销。更多信息请参阅[User
roles](/manual/web_interface/frontend_sections/administration/user_roles)

:::

[comment]: # ({/399b9834-399b9834})

[comment]: # ({82b631eb-4167b841})
### 参数

`(object)` 参数包含需要update的模板ID以及要添加到模板的objects.

该方法接受以下参数.

| 参数 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| **templates**<br>(required) | object/array | 需要更新的模板.<br><br>模板必须定义`templateid`属性. |
| groups | object/array | 要将给定模板添加到的主机组.<br><br>主机组必须定义`groupid`属性. |
| macros | object/array | 要为给定模板创建的用户宏. |
| templates\_link | object/array | 要链接到给定模板的模板.<br><br>模板必须定义`templateid`属性. |

[comment]: # ({/82b631eb-4167b841})

[comment]: # ({dcba01c8-dcba01c8})
### 返回值

`(object)` 返回一个包含更新模板ID的object，该ID位于`templateids`属性下。

[comment]: # ({/dcba01c8-dcba01c8})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({f8a75407-f8fe44e4})
#### 将组关联至模板

将主机组 "2"添加到两个模板中。

请求:

```json
{
    "jsonrpc": "2.0",
    "method": "template.massadd",
    "params": {
        "templates": [
            {
                "templateid": "10085"
            },
            {
                "templateid": "10086"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "templateids": [
            "10085",
            "10086"
        ]
    },
    "id": 1
}
```

[comment]: # ({/f8a75407-f8fe44e4})

[comment]: # ({c0240db4-610a67a7})
#### 将两个模板链接至一个模板

将模板"10106"和"10104"链接至模板。

请求:

```json
{
    "jsonrpc": "2.0",
    "method": "template.massadd",
    "params": {
        "templates": [
            {
                "templateid": "10073"
            }
        ],
        "templates_link": [
            {
                "templateid": "10106"
            },
            {
                "templateid": "10104"
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "templateids": [
            "10073"
        ]
    },
    "id": 1
}
```

[comment]: # ({/c0240db4-610a67a7})

[comment]: # ({bd8dd370-bd8dd370})
### 另请参阅

-   [template.update](update)
-   [主机](/manual/api/reference/host/object#主机)
-   [主机组](/manual/api/reference/hostgroup/object#主机组)
-   [用户宏-object](/manual/api/reference/usermacro/object#用户宏-object)

[comment]: # ({/bd8dd370-bd8dd370})

[comment]: # ({2f7abcb1-2f7abcb1})
### 来源

CTemplate::massAdd() 位于 *ui/include/classes/api/services/CTemplate.php* 文件中。

[comment]: # ({/2f7abcb1-2f7abcb1})
