Graph API
The graph api library will help you make requests from SharePoint under the context of the user. A benefit of this method is that it will not require you to register the app.
Token
The first step is to get an access token for the graph api requests. The graph library has a Token
property that can be set, so you do not need to pass it for each request. All requests will be made under the user context.
Token Properties
The request will return the following data.
Name | Description |
---|---|
access_token | The graph api access token. |
expires_on | Expiration date of the token. |
resource | The graph api cloud environment. |
scope | The user’s permissions. |
token_type | The token type. |
Cloud Environments
There are different Graph API cloud environments, per the Microsoft Docs. Below are examples for the different cloud environments.
Default
The default cloud environment used is the https://graph.microsoft.com environment.
import { ContextInfo, Graph } from "gd-sprest";
// Get the access token
Graph.getAccessToken().execute(token => {
// Set the default token for the api requests
Graph.Token = token.access_token;
});
China
import { ContextInfo, Graph, SPTypes } from "gd-sprest";
// Get the access token
Graph.getAccessToken(SPTypes.CloudEnvironment.China).execute(token => {
// Set the default token for the api requests
Graph.Token = token.access_token;
});
US Level 4
import { ContextInfo, Graph, SPTypes } from "gd-sprest";
// Get the access token
Graph.getAccessToken(SPTypes.CloudEnvironment.USL4).execute(token => {
// Set the default token for the api requests
Graph.Token = token.access_token;
});
US Level 5
import { ContextInfo, Graph, SPTypes } from "gd-sprest";
// Get the access token
Graph.getAccessToken(SPTypes.CloudEnvironment.USL5).execute(token => {
// Set the default token for the api requests
Graph.Token = token.access_token;
});
Requests
The ContextInfo will be used to reference the current site-id for the code examples. The code examples will also assume that the Token has already been set.
Request Properties
Name | Description |
---|---|
accessToken | The access token. Defaults to the Graph.Token value if not presented. |
cloud | The cloud environment to access. Defaults to the Graph.Cloud or Default cloud environment if not presented. |
requestType | The GET/POST request type. |
url | The graph api url for the request. |
version | The version of the graph api to access. Defaults to the Graph.Version or 1.0 version if not presented. |
Setting the Default Request Properties
To set the properties, simples reference the Graph library and set the property.
import { Graph, SPTypes } from "gd-sprest";
// Set the default properties
Graph.Cloud = SPTypes.CloudEnvironment.USL5;
Graph.Version = "2.0";
// Get the access token
Graph.getAccessToken().execute(token => {
// Set the default token value
Graph.Token = token.access_token;
});
Code Examples
Get the User Information
import { Graph } from "gd-sprest";
// Get my information
Graph({ url: "me" }).execute(userInfo => {
userInfo.businessPhones;
userInfo.display_name;
userInfo.givenName;
userInfo.id;
userInfo.jobTitle;
userInfo.mail;
userInfo.mobilePhone;
userInfo.officeLocation;
userInfo.preferredLanguage;
userInfo.surname;
userInfo.userPrincipalName;
});
Get the root site
import { Graph } from "gd-sprest";
// Get the root site collection
Graph({ url: "/sites/root" }).execute(rootSite => {
rootSite.createdDateTime;
rootSite.description;
rootSite.displayName;
rootSite.id;
rootSite.lastModifiedDate;
rootSite.name;
rootSite.root;
rootSite.siteCollection;
});
Get the Current Site
import { ContextInfo, Graph } from "gd-sprest";
// Get the current site collection
Graph({ url: "/sites/" + ContextInfo.siteId }).execute(siteInfo => {
siteInfo.createdDateTime;
siteInfo.description;
siteInfo.displayName;
siteInfo.id;
siteInfo.lastModifiedDate;
siteInfo.name;
siteInfo.root;
siteInfo.siteCollection;
});
Get the Lists in the Current Site Collection
import { ContextInfo, Graph } from "gd-sprest";
// Get the lists in the current site collection
Graph({ url: "/sites/" + ContextInfo.siteId + "/lists" }).execute(lists => {
// Parse the lists
for(let i=0; i<lists.value.length; i++) {
lists.createdBy;
lists.createdDateTime;
lists.description;
lists.displayName;
lists.eTag;
lists.id;
lists.lastModifiedBy;
lists.lastModifiedDateTime;
lists.list;
lists.name;
lists.parentReference;
lists.webUrl;
}
});
Create a List in the Current Site Collection
import { ContextInfo, Graph } from "gd-sprest";
// Create a list in the current site collection
Graph({ url: "/sites/" + ContextInfo.siteId + "/lists", requestType: "POST", data: {
"displayName": "Books",
"columns": [
{
"name": "Author",
"text": { }
},
{
"name": "PageCount",
"number": { }
}
],
"list": {
"template": "genericList"
}
}}).execute(list => {
list.createdBy;
list.createdDateTime;
list.description;
list.displayName;
list.eTag;
list.id;
list.lastModifiedBy;
list.lastModifiedDateTime;
list.list;
list.name;
list.parentReference;
list.webUrl;
});