You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/spfx/call-microsoft-graph-using-graphhttpclient.md
+40-22Lines changed: 40 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,16 @@
1
+
---
2
+
title: Tutorial - Use GraphHttpClient to call Microsoft Graph
3
+
description: Use the GraphHttpClient class to make calls to the Microsoft Graph REST API by using the get(), post(), and fetch() methods.
4
+
ms.date: 02/02/2018
5
+
ms.prod: sharepoint
6
+
---
7
+
1
8
# Use GraphHttpClient to call Microsoft Graph
9
+
2
10
> [!IMPORTANT]
3
-
>The **GraphHttpClient** is currently in preview and is subject to change. It is not currently supported for use in production environments.
11
+
>The **GraphHttpClient** is currently in preview and is subject to change. It is not currently supported for use in production environments.
4
12
5
-
Use the **GraphHttpClient** class to make calls to the Microsoft Graph REST API. You can make GET, POST, and PATCH requests using the **get()**, **post()**, and **fetch()** methods. This article shows how to build a web part that uses **GraphHttpClient**, but you can use **GraphHttpClient** in any SharePoint Framework client code.
13
+
Use the **GraphHttpClient** class to make calls to the Microsoft Graph REST API. You can make GET, POST, and PATCH requests by using the **get()**, **post()**, and **fetch()** methods. This article shows how to build a web part that uses **GraphHttpClient**, but you can use **GraphHttpClient** in any SharePoint Framework client code.
6
14
7
15
## Retrieve Office 365 groups using a GET call
8
16
@@ -36,11 +44,11 @@ You can use the **get()** method to make a REST call to Microsoft Graph. This ex
36
44
* Select **Web Part** as the type of client-side component to create.
37
45
* Enter **HelloGraph** as your web part name.
38
46
* Enter **Calls the Microsoft Graph Groups API** as the web part description.
39
-
* Accept the default **No javascript web framework** as the framework, and choose Enter.
47
+
* Accept the default **No javascript web framework** as the framework, and select Enter.
40
48
41
49

42
50
43
-
5. The Yeoman generator will build the web part. When the scaffolding is finished, open your project folder in your code editor. This article uses Visual Studio Code in the steps and screenshots, but you can use any editor that you prefer.
51
+
5. The Yeoman generator builds the web part. When the scaffolding is finished, open your project folder in your code editor. This article uses Visual Studio Code in the steps and screenshots, but you can use any editor that you prefer.
44
52
45
53
6. Run the gulp serve command and confirm that it runs in the local workbench correctly.
46
54
@@ -49,13 +57,14 @@ You can use the **get()** method to make a REST call to Microsoft Graph. This ex
49
57
```
50
58
51
59
### Add a button and placeholder for results
60
+
52
61
Next, you'll modify the HTML to provide a button to retrieve Office 365 groups. The HTML also needs a placeholder to display the groups.
53
62
54
63
1. In your code editor, open the **/src/webparts/helloGraph/HelloGraphWebPart.ts** file.
55
64
56
-
2. Modify the **render()** method so that it contains a button and a **div** where the code will list the Office 365 groups after they are retrieved.
65
+
2. Modify the **render()** method so that it contains a button and a **div** where the code lists the Office 365 groups after they are retrieved.
57
66
58
-
Your code should look like the following TypeScript.
67
+
Your code should look like the following TypeScript:
59
68
60
69
```typescript
61
70
publicrender(): void {
@@ -96,6 +105,7 @@ Next, you'll modify the HTML to provide a button to retrieve Office 365 groups.
96
105
```
97
106
98
107
### Use the GraphHttpClient.get method to retrieve Office 365 groups
108
+
99
109
Next, you'll call the **GraphHttpClient.get()** method to make a REST call to Microsoft Graph to retrieve a list of Office 365 groups.
100
110
101
111
1. Import the **GraphHttpClient** class and related types by adding the following import statement near the top of the **HelloGraphWebPart.ts** file.
@@ -122,13 +132,13 @@ Next, you'll call the **GraphHttpClient.get()** method to make a REST call to Mi
122
132
}
123
133
```
124
134
125
-
In the previous code, the context property has the GraphHttpClient instance. When you call the **get()** method, a REST call is made to Microsoft Graph that passes the specified URL. In this case, the URL is **v1.0/groups?orderby=displayName**. This issues a GET request and Microsoft Graph will return all Office 365 groups in the tenant in order by display name.
135
+
In the previous code, the context property has the GraphHttpClient instance. When you call the **get()** method, a REST call is made to Microsoft Graph that passes the specified URL. In this case, the URL is **v1.0/groups?orderby=displayName**. This issues a GET request, and Microsoft Graph returns all Office 365 groups in the tenant in order by display name.
126
136
127
-
You can issue any GET request by using this technique and entering the correct URL values. To find the URL values, see the [Microsoft Graph documentation](https://developer.microsoft.com/en-us/graph/docs/concepts/overview). For example, you can use URL specified in the [Groups GET request](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_get) topic to get groups.
137
+
You can issue any GET request by using this technique and entering the correct URL values. To find the URL values, see the [Microsoft Graph documentation](https://developer.microsoft.com/en-us/graph/docs/concepts/overview). For example, you can use the URL specified in the [Groups GET request](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_get) topic to get groups.
128
138
129
-
The **get()** method returns an **HttpClientResponse**, which you can use to determine whether the call was successful. The returned JSON is in the **result.value**. Because you expect multiple groups to be returned, you will pass the value to a **_renderTable()** method, which will build a table of rows for each group.
139
+
The **get()** method returns an **HttpClientResponse**, which you can use to determine whether the call was successful. The returned JSON is in the **result.value**. Because you expect multiple groups to be returned, you pass the value to a **_renderTable()** method, which builds a table of rows for each group.
130
140
131
-
3. Create a **_renderTable()** method to render the returned groups in a table where each row represent a group. Add the following method to the **HelloGraphWebPart** class.
141
+
3. Create a **_renderTable()** method to render the returned groups in a table where each row represents a group. Add the following method to the **HelloGraphWebPart** class.
@@ -159,6 +169,7 @@ Next, you'll call the **GraphHttpClient.get()** method to make a REST call to Mi
159
169
```
160
170
161
171
### Run the web part to call Microsoft Graph
172
+
162
173
The code needs to call the **GraphHttpClient** application that runs on SharePoint, so you can't run it on the local workbench. You have to package and deploy it to SharePoint.
163
174
164
175
1. Use gulp to package your solution.
@@ -170,34 +181,36 @@ The code needs to call the **GraphHttpClient** application that runs on SharePoi
170
181
2. Deploy the solution to your SharePoint tenant:
171
182
* Go to your site's App Catalog.
172
183
* Upload or drag and drop the **hellograph-webpart.sppkg** to the App Catalog.
173
-
* When prompted, if you trust the **hellograph-webpart-client-side-solution**, select **Make this solution available to all sites in the organization**, and choose**Deploy**.
184
+
* When prompted, if you trust the **hellograph-webpart-client-side-solution**, select **Make this solution available to all sites in the organization**, and select**Deploy**.
174
185
175
186
3. Use gulp serve to host the web part.
176
187
177
188
```
178
189
gulp serve --nobrowser
179
190
```
180
191
181
-
4. Add the web part to a web page, or use the SharePoint workbench.
192
+
4. Add the web part to a web page, or use the SharePoint Workbench.
182
193
183
194
You should see the following on your page.
195
+
184
196

185
197
186
-
When you choose**Read Groups**, you will see a list of all the Office 365 groups on your tenant. If no groups are listed, you'll just see a message that indicates that there were no groups. You will create a new group next.
198
+
When you select**Read Groups**, you see a list of all the Office 365 groups on your tenant. If no groups are listed, you'll just see a message that indicates that there were no groups. You will create a new group next.
187
199
188
200
## Create a new Office 365 group using a POST call
189
201
190
202
You can issue POST calls to the Microsoft Graph API by using the **GraphHttpClient.post()** method. You'll use the **post()** method to create a new Office 365 group.
191
203
192
204
193
205
### Add a button and placeholder for results
194
-
Again, you need to modify the HTML to add a button that will create a new group.
206
+
207
+
Again, you need to modify the HTML to add a button that creates a new group.
195
208
196
209
1. In your code editor, open the **/src/webparts/helloGraph/HelloGraphWebPart.ts** file.
197
210
198
-
2. Modify the **render()** method so that it contains a button and a **div** that will indicate whether the creation was successful.
211
+
2. Modify the **render()** method so that it contains a button and a **div** that indicates whether the creation was successful.
199
212
200
-
Your code should look like the following TypeScript.
213
+
Your code should look like the following TypeScript:
201
214
202
215
```typescript
203
216
publicrender(): void {
@@ -254,7 +267,7 @@ Again, you need to modify the HTML to add a button that will create a new group.
254
267
}
255
268
```
256
269
257
-
The previous code creates a simple group using the code example from the Microsoft Graph [Create group](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_post_groups) article.
270
+
The previous code creates a simple group by using the code example from the Microsoft Graph [Create group](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_post_groups) article.
258
271
259
272
The **post()** issues a POST REST API call to the **v1.0/groups** URL. The third parameter is an **IGraphHttpClientOptions** value in which the JSON body is specified to describe the new group. The **HttpClientResponse** is used to determine whether the call was successful, and to display an appropriate result.
260
273
@@ -271,24 +284,25 @@ Again, you need to modify the HTML to add a button that will create a new group.
271
284
2. Deploy the solution to your SharePoint tenant:
272
285
* Go to your site's App Catalog.
273
286
* Upload or drag and drop the **hellograph-webpart.sppkg** to the App Catalog.
274
-
* Because your solution is already registered, you'll be prompted as to whether you want to replace it. Choose**Replace it**.
275
-
* When prompted as to whether you trust the solution, choose**Deploy**.
287
+
* Because your solution is already registered, you are prompted as to whether you want to replace it. Select**Replace it**.
288
+
* When prompted as to whether you trust the solution, select**Deploy**.
276
289
277
290
3. Use gulp serve to host the web part.
278
291
279
292
```
280
293
gulp serve --nobrowser
281
294
```
282
295
283
-
4. Add the web part to a web page, or use the SharePoint workbench.
296
+
4. Add the web part to a web page, or use the SharePoint Workbench.
284
297
285
298
You should see the following on your page.
299
+
286
300

287
301
288
-
5. When you choose**Create New Group**, the code will create a new Office 365 group.
302
+
5. When you select**Create New Group**, the code creates a new Office 365 group.
289
303
290
304
> [!NOTE]
291
-
>If you try to create the same group again, the code will return an error because the group already exists. The error is logged to the console, which you can view in the browser's developer mode.
305
+
>If you try to create the same group again, the code returns an error because the group already exists. The error is logged to the console, which you can view in the browser's developer mode.
292
306
293
307
## Update a group using a PATCH call
294
308
@@ -316,3 +330,7 @@ The following code shows how to call the **fetch()** method to update an existin
316
330
```
317
331
318
332
The ID of the group is specified in the URL. Get the ID by using a GET call first. The **method** parameter is set to **PATCH**. The body specifies which group properties to modify.
333
+
334
+
## See also
335
+
336
+
-[Overview of the GraphHttpClient class](overview-graphhttpclient.md)
To use the generated class names in your code, you first import the styles of your component, and then use the property pointing to the particular class:
Copy file name to clipboardExpand all lines: docs/spfx/extensions/get-started/building-simple-cmdset-with-dialog-api.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ Open the **HelloWorldCommandSet.ts** file in the **src\extensions\helloWorld** f
94
94
95
95
Notice that the base class for the ListView Command Set is imported from the **sp-listview-extensibility** package, which contains SharePoint Framework code required by the ListView Command Set.
96
96
97
-
```ts
97
+
```typescript
98
98
import { override } from '@microsoft/decorators';
99
99
import { Log } from '@microsoft/sp-core-library';
100
100
import {
@@ -112,7 +112,7 @@ The **onListViewUpdated()** event occurs separately for each command (for exampl
112
112
113
113
When using the method `tryGetCommand`, you get a Command object, which is a representation of the command that shows in the UI. You can modify its values, such as `title`, or `visible`, to modify the UI element. SPFx uses this information when re-rendering the commands. These objects keep the state from the last render, so if a command is set to `visible = false`, it remains invisible until it is set back to `visible = true`.
@@ -126,7 +126,7 @@ When using the method `tryGetCommand`, you get a Command object, which is a repr
126
126
The **OnExecute()** method defines what happens when a command is executed (for example, the menu item is selected). In the default implementation, different messages are shown based on which button was selected.
Copy file name to clipboardExpand all lines: docs/spfx/extensions/get-started/building-simple-field-customizer.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ Open the **HelloWorldFieldCustomizer.ts** file in the **src\extensions\helloWorl
93
93
94
94
Notice that the base class for the Field Customizer is imported from the **sp-listview-extensibility** package, which contains SharePoint Framework code required by the Field Customizer.
95
95
96
-
```ts
96
+
```typescript
97
97
import { Log } from '@microsoft/sp-core-library';
98
98
import { override } from '@microsoft/decorators';
99
99
import {
@@ -110,7 +110,7 @@ The logic for your Field Customizer is contained in the **OnInit()**, **onRender
110
110
111
111
The following are the contents of **onRenderCell()** and **onDisposeCell()** in the default solution:
Copy file name to clipboardExpand all lines: docs/spfx/extensions/get-started/using-page-placeholder-with-extensions.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ You can also follow these steps by watching the video on the [SharePoint PnP You
23
23
24
24
Application Customizer extensions are supported with `Site`, `Web`, and `List` scopes. You can control the scope by deciding where or how the Application Customizer is registered in your SharePoint tenant. When the Application Customizer exists in the scope and is being rendered, you can use the following method to get access to the placeholder.
25
25
26
-
```ts
26
+
```typescript
27
27
// Handling the Bottom placeholder
28
28
if (!this._bottomPlaceholder) {
29
29
this._bottomPlaceholder=
@@ -44,7 +44,7 @@ Notice that you're requesting a well-known placeholder by using the correspondin
44
44
45
45
2. Add the `PlaceholderContent` and `PlaceholderName` to the import from `@microsoft/sp-application-base` by updating the import statement as follows:
46
46
47
-
```ts
47
+
```typescript
48
48
import {
49
49
BaseApplicationCustomizer,
50
50
PlaceholderContent,
@@ -54,7 +54,7 @@ Notice that you're requesting a well-known placeholder by using the correspondin
54
54
55
55
Also add the following import statements after the `strings` import at the top of the file:
56
56
57
-
```ts
57
+
```typescript
58
58
import styles from './AppCustomizer.module.scss';
59
59
import { escape } from '@microsoft/sp-lodash-subset';
60
60
```
@@ -97,7 +97,7 @@ Notice that you're requesting a well-known placeholder by using the correspondin
97
97
> [!NOTE]
98
98
> If your Command Set uses the ClientSideComponentProperties JSON input, it is deserialized into the `BaseExtension.properties` object. You can define an interface to describe it.
@@ -106,7 +106,7 @@ Notice that you're requesting a well-known placeholder by using the correspondin
106
106
107
107
6. Add the following private variables inside the **HelloWorldApplicationCustomizer** class. In this scenario, these can just be local variables in an `onRender` method, but if you want to share them with other objects, define them as private variables.
108
108
109
-
```ts
109
+
```typescript
110
110
export default class HelloWorldApplicationCustomizer
@@ -213,7 +213,7 @@ Notice that you're requesting a well-known placeholder by using the correspondin
213
213
214
214
9. Add the following method after the `_renderPlaceHolders` method. In this case, you simply output a console message when the extension is removed from the page.
215
215
216
-
```ts
216
+
```typescript
217
217
private _onDispose(): void {
218
218
console.log('[HelloWorldApplicationCustomizer._onDispose] Disposed custom top and bottom placeholders.');
0 commit comments