1+ import * as common from './common' ;
2+ import * as nodeApi from 'azure-devops-node-api' ;
3+
4+ import * as DashboardApi from 'azure-devops-node-api/DashboardApi' ;
5+ import * as CoreApi from 'azure-devops-node-api/CoreApi' ;
6+ import * as DashboardInterfaces from 'azure-devops-node-api/interfaces/DashboardInterfaces' ;
7+ import * as CoreInterfaces from 'azure-devops-node-api/interfaces/CoreInterfaces' ;
8+
9+ export async function run ( projectId : string ) {
10+ const webApi : nodeApi . WebApi = await common . getWebApi ( ) ;
11+ const dashboardApiObject : DashboardApi . IDashboardApi = await webApi . getDashboardApi ( ) ;
12+ const coreApiObject : CoreApi . CoreApi = await webApi . getCoreApi ( ) ;
13+ const project : CoreInterfaces . TeamProject = await coreApiObject . getProject ( projectId ) ;
14+
15+ common . banner ( 'Dashboard Samples' ) ;
16+
17+ common . heading ( 'Create a Dashboard' ) ;
18+ const teamContext : CoreInterfaces . TeamContext = { project : project . name ,
19+ projectId : project . id ,
20+ team : project . defaultTeam . name ,
21+ teamId : project . defaultTeam . id } ;
22+ const createDashboardContext : DashboardInterfaces . Dashboard = { _links : null ,
23+ description : 'fancy new dashboard' ,
24+ eTag : '' ,
25+ id : null ,
26+ name : 'dash' ,
27+ ownerId : null ,
28+ position : null ,
29+ refreshInterval : 1 ,
30+ url : null ,
31+ widgets : [ ] } ;
32+ let dashboard : DashboardInterfaces . Dashboard = await dashboardApiObject . createDashboard ( createDashboardContext , teamContext ) ;
33+ //Ensure its been created and we can get it.
34+ dashboard = await dashboardApiObject . getDashboard ( teamContext , dashboard . id ) ;
35+ console . log ( 'Created dashboard' , dashboard . name ) ;
36+
37+ common . heading ( 'Create a widget' ) ;
38+ let widget : DashboardInterfaces . Widget = { _links : null ,
39+ allowedSizes : null ,
40+ artifactId : null ,
41+ configurationContributionId : null ,
42+ configurationContributionRelativeId : null ,
43+ contentUri : null ,
44+ contributionId : 'ms.vss-dashboards-web.Microsoft.VisualStudioOnline.Dashboards.OtherLinksWidget' ,
45+ dashboard : dashboard ,
46+ eTag : null ,
47+ id : null ,
48+ isEnabled : null ,
49+ isNameConfigurable : null ,
50+ lightboxOptions : null ,
51+ loadingImageUrl : null ,
52+ name : 'widge' ,
53+ position : { row : 1 , column : 1 } ,
54+ settings : null ,
55+ settingsVersion : { major : 1 , minor : 0 , patch : 0 } ,
56+ size : { rowSpan : 1 , columnSpan : 2 } ,
57+ typeId : null ,
58+ url : null
59+ } ;
60+ const widgetMetadata : DashboardInterfaces . WidgetMetadataResponse = await dashboardApiObject . getWidgetMetadata ( 'ms.vss-dashboards-web.Microsoft.VisualStudioOnline.Dashboards.OtherLinksWidget' ) ;
61+ console . log ( 'Creating widget with metadata:' , widgetMetadata ) ;
62+ widget = await dashboardApiObject . createWidget ( widget , teamContext , dashboard . id ) ;
63+ //Ensure its been created and we can get it.
64+ widget = await dashboardApiObject . getWidget ( teamContext , dashboard . id , widget . id ) ;
65+ console . log ( 'Created widget' , widget . name ) ;
66+
67+ common . heading ( 'Update a widget' ) ;
68+ widget . name = 'new name' ;
69+ widget = await dashboardApiObject . updateWidget ( widget , teamContext , dashboard . id , widget . id ) ;
70+ //Ensure its been updated and we can get it.
71+ widget = await dashboardApiObject . getWidget ( teamContext , dashboard . id , widget . id ) ;
72+ console . log ( 'Widget name updated to' , widget . name ) ;
73+
74+ common . heading ( 'Get Widget data' ) ;
75+ const widgetTypes : DashboardInterfaces . WidgetTypesResponse = await dashboardApiObject . getWidgetTypes ( DashboardInterfaces . WidgetScope . Project_Team ) ;
76+ console . log ( 'First widget type:' , widgetTypes . widgetTypes [ 0 ] ) ;
77+
78+ common . heading ( 'Delete a widget' ) ;
79+ await dashboardApiObject . deleteWidget ( teamContext , dashboard . id , widget . id ) ;
80+ widget = await dashboardApiObject . getWidget ( teamContext , dashboard . id , widget . id ) ;
81+ console . log ( 'Widget deleted, trying to get it returns:' , widget ) ;
82+
83+ common . heading ( 'Delete dashboard' ) ;
84+ //Can't delete last dashboard for a team, will create a new one so we can delete the old one.
85+ createDashboardContext . description = 'this dashboard should survive' ;
86+ createDashboardContext . name = 'tempDash' ;
87+ await dashboardApiObject . createDashboard ( createDashboardContext , teamContext ) ;
88+ //Will delete old dashboard
89+ await dashboardApiObject . deleteDashboard ( teamContext , dashboard . id ) ;
90+ dashboard = await dashboardApiObject . getDashboard ( teamContext , dashboard . id ) ;
91+ console . log ( 'Dashboard deleted, trying to get it returns:' , dashboard ) ;
92+ }
0 commit comments