JavaScript 开发工具包

JavaScript 开发工具包

在开发自定义部件时,您可以使用 JS API 函数来调用您的部件或任何第三方应用程序。


JS API 必须从自定义部件中调用。

 

自定义部件的一般函数 SDP.init()   

通过建立产品与自定义部件之间的连接,初始化 JS SDK 并定义实用功能。 SDP.init() 将自定义部件与其他 JS 应用程序接口连接起来,使用户可以看到该部件。 只有在初始化完成后,才会定义其他 JS API。

返回值:

Promise - Resolved with API Success or Rejected with API Failure

语法:

 SDP.init().then(function(response) { 

     console.log(response); 

 }).catch(function(response) { 

     console.log(response); 

 }); 

 

 

API 调用

成功响应

SDP.init().then(function(response) {
    console.log(response);
}).catch(function(response) {
    console.log(response);
});

{

    "meta": {

        "module": "request",

        "module_entity_id": "4",

        "location": "request.detail.rightpanel",

        "instance": {

            "appid": 1

        },

        "userLanguage": "en_US",

        "userLocale": "en_US",

        "userType": "Technician",

        "userId": 5,

        "userName": "administrator",

        "isSDAdmin": true,

        "widget": {

            "id": "2103",

            "name": "teamviewer1",

            "description": "A widget that allows technicians to gain remote access to devices across different platforms.",

            "custom_widget": {

                "id": "2702"

            }

        }

    },

    "uniqueID": "3b12a30c-2c4b-48cd-afe0-c16d7eab9abd",

    "location": "request.detail.rightpanel"

}

 

添加/编辑/删除服务台数据  

 

SDP.get(options) 

从服务器上获取部件数据,如 URL 或自定义部件中的任何输入数据。您还可以使用此函数从自定义部件执行跨门户调用。请注意,使用此函数只能调用 SDP V3 API 调用。


示例:使用人力资源门户中的 URL 访问 IT 门户中的请求。

参数

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

url*

字符串

仅提及实体 URL。 API 上下文将自动设置。

input_data

对象

指定要在 API 调用中传递的输入数据。 

 

* 必填参数

返回:

Promise - Resolved with API Success or Rejected with API Failure.

 

API 调用

成功响应

失败响应

SDP.get({

url: "/requests"

}).then(function(response) {

console.log(response);

}).catch(function(response) {

console.log(response);

});

 

//Refer API documentation for success response.

//Refer API documentation for failure response.

 

SDP.add(options)

从自定义部件向服务器添加数据。您还可以使用此函数从自定义部件执行跨门户调用。请注意,使用此函数只能调用 SDP V3 API。

参数:

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

说明

url*

字符串

仅提及实体 URL。 API 上下文将自动设置。

input_data

对象

指定要在 API 调用中传递的输入数据。 有关输入数据格式,请参阅 API 文档。

 

* 必填参数

返回:

Promise - Resolved with API Success or Rejected with API Failure. Refer API documentation for success/failure response formats.

语法:

 SDP.add(options).then(function(response) { 

     console.log(response); 

 }).catch(function(response) { 

     console.log(response); 

 }); 

 

 

API 调用

成功响应

失败响应

SDP.add({
    url: "/requests",
    input_data: {
        request: {
            subject: 'Request created from Widget'
        }
    }
}).then(function(response) {
    console.log(response);
}).catch(function(response) {
    console.log(response);
});

 

//Refer API documentation for success response.

//Refer API documentation for failure response.

 

SDP.edit(options)

通过提及实体 ID,从自定义部件编辑服务器中的数据。您还可以使用此函数从自定义部件执行跨门户调用。请注意,使用此功能只能调用 SDP V3 API。


您还可以使用此函数执行批量编辑。

参数

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

url*

字符串

仅提及实体 URL。 API 上下文将自动设置。

input_data

对象

指定要在 API 调用中传递的输入数据。 有关输入数据格式,请参阅 API 文档。

 

* 必填参数

返回:

Promise - Resolved with API Success or Rejected with API Failure.

 

语法:

 SDP.edit(options).then(function(response) { 

     console.log(response); 

 }).catch(function(response) { 

     console.log(response); 

 }); 

 

API 调用

成功响应

失败响应

SDP.edit({
    url: "/requests/100000000000032746",
    input_data: {
        request: {
            subject: 'Request created from Widget'
        }
    }
}).then(function(response) {
    console.log(response);
}).catch(function(response) {
    console.log(response);
});

 

//Refer API documentation for success response.

//Refer API documentation for failure response.

 

 

SDP.del(options)

通过提及实体 ID,从自定义部件中删除服务器中的数据。您也可以使用此函数从自定义部件执行跨门户调用。请注意,使用此函数只能调用 SDP V3 API。


目前,您只能使用此函数删除 ServiceDesk Plus 中的自定义模块实体。


参数:

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

url*

字符串

仅提及实体 URL。 API 上下文将自动设置。

ids

字符串

实体 ID。 多个 ID 可以逗号分隔的字符串形式传递。

 

* 必填参数

返回值:

Promise - Resolved with API Success or Rejected with API Failure.

 

API 调用

响应成功

响应失败

SDP.del({

    url:"/cm_nat/",

    ids:"307"

});

 

//Refer API documentation for success response.

//Refer API documentation for failure response.

 

 SDP.connect(options)

 

使用自定义部件的授权令牌调用外部 api。

 

参数

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

url*

字符串

表示 API URL。

method*

字符串

表示 API 调用的方法。

headers

对象

以key值格式表示 API 的参数。

params

 

对象

以key值格式表示 API 的参数。

data

字符串

指定要与 url 一起发送的 JSON 字符串数据。

 

 

 

 

 

API 调用

响应成功

响应失败

SDP.connect({

            url: "https://webapi.teamviewer.com/api/v1/sessions",

            headers: {

                "Content-Type": "application/json"

            },

            method: "post",

            data: JSON.stringify({

                "groupname": "SDPOnDemand",

                "waiting_message": "Please wait for the technician to join session.",

            }),

            connection: connection_link_name

        });

 

 

Based on the third party API

Based on the third party API

 

 

SDP.refreshWidget()

刷新自定义部件。

语法
SDP.refreshWidget()

SDP.show_alert(options)

在部件中执行操作时显示提示框。

参数:

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

message*

字符串

告警框中要显示的信息。

alert_type*

字符串

定义告警类型。 支持的告警类型:信息、成功和失败。

autohide

布尔

指定告警信息是自动关闭还是需要用户手动关闭。

 

 

 

API 调用

响应成功

响应失败

SDP.show_alert({

message:"widget data successfully stored",

alert_type:"success"

})

 

 

Displays an alert message

Message cannot be empty

 

SDP.show_confirm(options)

在部件中执行操作时,显示带有给定信息和按钮的确认对话框。

 

参数:

名称

类型

描述

options*

对象

包含 API 详细信息的对象。

属性

类型

描述

message*

字符串

确认对话框中要显示的信息。

buttons*

矩阵

请说明应显示哪些按钮。

inputfields

矩阵

定义应显示的输入字段。

 

 

 

 

API 调用

响应成功

响应失败

SDP.show_confirm({
message:"are you sure to enter the meeting",
buttons:[{
value:"yes",

label:"yes"

}
,{
value:"no",

label:"no"

}],
})

 

A confirmation screen with Yes or No buttons is displayed.

When you press any button, the corresponding value is sent as a response.

 

 

 

SDP.showIndicator(options)

显示应用程序页面的加载状态。目前,该功能仅适用于加载栏。

语法

SDP.showIndicator({

category:"loading"
})

 

SDP.hideIndicator

隐藏显示的指示器。

 

语法

SDP.hideIndicator({

category:"loading"

})

 

管理自定义部件中的变量/连接

 

SDP.getTempVariables()

在打开 ServiceDesk Plus 窗口的浏览器中获取与自定义部件相关的所有临时变量。


临时变量用于在有限时间内存储值,例如直到窗口重新加载。

返回值:

Promise - Resolved with Success or Rejected with Failure.

语法:

 SDP.getTempVariables().then(function(response) { 

     console.log(response); 

 }).catch(function(response) { 

     console.log(response)}) 

e)

示例:

API 调用

成功响应

SDP.getTempVariables().then(function(response) {

console.log(response);

}).catch(function(response) {

console.log(response);

});

{

"key": value,

}

 

 

SDP.getTempVariable(key)

获取指定的临时变量。

参数:

名称

类型

描述

key*

字符串

表示临时变量键,用于指定要获取的变量详细信息。

* 必填参数

返回:

Promise - Resolved with Success or Rejected with Failure.

语法:

SDP.getTempVariable(key).then(function(response) {

    console.log(response);

}).catch(function(response) {

    console.log(response)})

 

 

示例

API 调用

成功响应

SDP.getTempVariable(“passphrase").then(function(response) {

console.log(response);

}).catch(function(response) {

console.log(response);

});

{

"key": "passphrase",

"value": "*****"

}

 

SDP.setTempVariable(key, value)

更新与部件相关联的变量值。


这个函数在配置部件中特别有用,因为它们在用户界面中没有可以更新变量值的配置页面。

参数:

名称

类型

描述

key*

字符串

表示要更新的变量的key值。

value*

-

表示要更新到变量中的值。 如果不传递,变量值将在应用程序中重置。

*必填参数

返回值:

Promise - Resolved with Success or Rejected with Failure.

语法:

SDP.setTempVariable(key, value).then(function(response) { console.log(response); }).catch(function(response) { console.log(response); });

});

 

示例:

API 调用

成功响应

失败响应

SDP.setTempVariable(“admin”, “john-doe”).then(function(response) {

console.log(response);

}).catch(function(response) {

console.log(response);

});

Variables saved in the application window successfully.

-

 

添加/编辑请求表专用 API

 

SDP.getValueFromForm(field)

从请求表中获取字段值。

参数:

名称

类型

描述

Field*

字符串

字段的名称

* 必填参数

返回:

Promise - Resolved with Success or Rejected with Failure.

 

语法:

SDP.getValueFromForm(field).then(function(response) {

    console.log(response);

}).catch(function(response) {

    console.log(response);

});

 

示例:

API 调用

成功响应

失败响应

SDP.getValueFromForm(“SUBJECT").then(function(response) {

console.log(response);

}).catch(function(response) {

console.log(response);

});

{

value: “Error in connecting to certain websites/URLs”

}

{

message: “The requested action is not supported in this view”

}

 

获取自定义部件/用户详细信息

 

SDP.getCurrentInstance();

获取加载自定义部件的实例的详细信息。

返回:

Object – Returns instance details object.

语法:

var instanceDetails = SDP.getCurrentInstance();

示例响应:

{

   appdisplayname: "IT Desk"

   appid: "15459854"

   appname: "itdesk"

}

 

SDP.getCurrentUserId();

获取登录用户 ID。

返回:

String – Returns user ID as a string.

语法:

var userId = SDP.getCurrentUserId();

示例响应:

"1000000000087326"

 

SDP.getCurrentUserType();

获取登录用户类型(请求者/技术人员)。 请注意,该函数不会获取角色。

返回:

String – Returns user type as a string.

语法:

var userType = SDP.getCurrentUserType();

 

示例响应:

"Technician"

 

SDP.getCurrentUserName();

获取登录的用户名。

返回:

String – Returns user name as a string.

 

SDP.getCurrentUserEmail();

获取当前用户的电子邮件地址。

返回:

String – Returns user email as a string.

语法:

var userEmail = SDP.getCurrentUserEmail();

 

示例响应:

"johndoe@somecompany.com"

 

SDP.getWidgetInfo();

获取当前部件信息。

返回:

Object – Returns the widget info object.

语法:

var widgetInfo = SDP.getWidgetInfo();

 

示例响应:

{
    "id": "120747000002587069",

    "name": "Custom Widget",

    "description": "",

    "custom_widget": {

        "id": "120747000002587067"

    }

}

 

SDP.getWidgetMeta();

获取传递给部件的 widget 元。

返回:

Object – Returns the widget meta-object.

语法:

var widgetMeta = SDP.getWidgetMeta();

 

示例响应:

{

    "module": "header",

    "instance": {

        "appid": "709641411",

        "appname": "widgetdev",

        "appdisplayname": "Widget Development"

    },

    "personalization": {

        "time_zone": "Asia/Kolkata",

        "time_format": "MMM d, yyyy hh:mm a",

        "date_format": "MMM d, yyyy"

    },

    "userLanguage": "en",

    "userLocale": "en_US",

    "userType": "Technician",

    "userId": "120747000000232217",

    "userName": "Widget Development",

    "userEmail": "widget_dev@sdpdev.onmicrosoft.com",

    "isSDAdmin": true,

    "widget": {

        "id": "120747000002587069",

        "name": "Custom Widget",

        "description": "",

        "custom_widget": {

            "id": "120747000002587067"

        }

    }

}

 

SDP.getWidgetInvokedLocation();   

获取当前部件的位置。

返回:

String – Returns the widget location.

语法:

var location = SDP.getWidgetInvokedLocation();

 

响应示例:

“webtab”

 

SDP.getDataPassedByparent();   

获取从父部件传递过来的数据。

返回

Object – Returns the data object passed by the parent widget.

语法

var data = SDP.getDataPassedByparent();

 

示例

{

mode: "new_user"

}

 

SDP.getParentLocation();

获取调用了子 widget 的父 widget 的位置。

返回

String – Returns the parent widget location.

语法

var parentLocation = SDP.getParentLocation();

 

响应示例

“webtab”

 

SDP.getParentWidgetInfo();   

获取调用了子 widget 的父 widget 的详细信息。

返回

Object – Returns the parent widget info object.

语法

var parentInfo = SDP.getParentWidgetInfo();

 

示例响应:

{

  "id":"100000000000032273",

  "name":"Sample Widget",

  "description":"Sample Widget",

  "custom_widget":{

   "id":"100000000000032271"

  }

}

 

 Ajaxbar

返回正在加载的 Ajax 条形码 html。

 

语法

  SDP.ajaxBar();