191
Views
4
Comments
Solved
Store file in Microsoft teams
Question


How can I store a file in Microsoft Teams via webservice?

2020-12-11 12-56-35
Miguel Garcia
Solution

Hi William, 

I've done this previously using the following method with the Microsoft Graph API:

Note:
You can use the Microsoft Graph API to upload files till 4 MB.
Teams is associated with SharePoint and you can create folders in both. In MS Teams open the Team you want to use and select "Open in SharePoint". In SharePoint you can check the URL easily.


1- Register an application in the Microsoft Azure Portal
Here you get:
Application (client) ID
Directory (tenant) ID
Object ID
Client secret


2 - Consume the following REST web service to get a token:
Add API Method: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
HTTP Method: POST
Request Format: Form URL Encoded
Response Format: JSON
In URL: {tenant} = [tenant ID from Azure]
Request Headers: "Content-Type" = "application/x-www-form-urlencoded"
Body Content:
   client_id=[from Azure]
   &scope=https://graph.microsoft.com/.default
   &client_secret=[from Azure]
   &grant_type=client_credentials


3 - Consume Microsoft Graph API REST:

3.1 - Get site ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{hostname}:/sites/{path}?$select=id
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {hostname} = "heiway.sharepoint.com"
             {path} = [path to the Main folder in SharePoint]

3.2 - Get Drive ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{siteId}/drive
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {siteId} = [Id from the response of the "Get site ID"]


3.3 - Get Folder Path ID:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/root:/{itempath}
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {itempath} = [sub-folder where to store the File]


3.4 - Put the File in the Folder:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{parentid}:/{filename}:/content
HTTP Method: PUT
Request Format: Binary
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {parentid} = [Id from the response of the "Get Folder Path ID"]
             {filename} = [name of the file to upload]
Request: [File binary content]

2020-04-21 08-15-30
William Antunes
 
MVP

thanks Miguel,

your solution worked just fine!!

J Miguel Garcia wrote:

Hi William, 

I've done this previously using the following method with the Microsoft Graph API:

Note:
You can use the Microsoft Graph API to upload files till 4 MB.
Teams is associated with SharePoint and you can create folders in both. In MS Teams open the Team you want to use and select "Open in SharePoint". In SharePoint you can check the URL easily.


1- Register an application in the Microsoft Azure Portal
Here you get:
Application (client) ID
Directory (tenant) ID
Object ID
Client secret


2 - Consume the following REST web service to get a token:
Add API Method: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
HTTP Method: POST
Request Format: Form URL Encoded
Response Format: JSON
In URL: {tenant} = [tenant ID from Azure]
Request Headers: "Content-Type" = "application/x-www-form-urlencoded"
Body Content:
   client_id=[from Azure]
   &scope=https://graph.microsoft.com/.default
   &client_secret=[from Azure]
   &grant_type=client_credentials


3 - Consume Microsoft Graph API REST:

3.1 - Get site ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{hostname}:/sites/{path}?$select=id
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {hostname} = "heiway.sharepoint.com"
             {path} = [path to the Main folder in SharePoint]

3.2 - Get Drive ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{siteId}/drive
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {siteId} = [Id from the response of the "Get site ID"]


3.3 - Get Folder Path ID:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/root:/{itempath}
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {itempath} = [sub-folder where to store the File]


3.4 - Put the File in the Folder:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{parentid}:/{filename}:/content
HTTP Method: PUT
Request Format: Binary
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {parentid} = [Id from the response of the "Get Folder Path ID"]
             {filename} = [name of the file to upload]
Request: [File binary content]



2018-03-20 11-51-40
João Bernardes

Hello,

I never did that, but you probably can get the API from microsoft teams page in order to use those services. Did you have them already?  

You've some documentation here:

https://docs.microsoft.com/en-us/graph/teams-concept-overview

2020-12-11 12-56-35
Miguel Garcia
Solution

Hi William, 

I've done this previously using the following method with the Microsoft Graph API:

Note:
You can use the Microsoft Graph API to upload files till 4 MB.
Teams is associated with SharePoint and you can create folders in both. In MS Teams open the Team you want to use and select "Open in SharePoint". In SharePoint you can check the URL easily.


1- Register an application in the Microsoft Azure Portal
Here you get:
Application (client) ID
Directory (tenant) ID
Object ID
Client secret


2 - Consume the following REST web service to get a token:
Add API Method: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
HTTP Method: POST
Request Format: Form URL Encoded
Response Format: JSON
In URL: {tenant} = [tenant ID from Azure]
Request Headers: "Content-Type" = "application/x-www-form-urlencoded"
Body Content:
   client_id=[from Azure]
   &scope=https://graph.microsoft.com/.default
   &client_secret=[from Azure]
   &grant_type=client_credentials


3 - Consume Microsoft Graph API REST:

3.1 - Get site ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{hostname}:/sites/{path}?$select=id
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {hostname} = "heiway.sharepoint.com"
             {path} = [path to the Main folder in SharePoint]

3.2 - Get Drive ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{siteId}/drive
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {siteId} = [Id from the response of the "Get site ID"]


3.3 - Get Folder Path ID:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/root:/{itempath}
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {itempath} = [sub-folder where to store the File]


3.4 - Put the File in the Folder:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{parentid}:/{filename}:/content
HTTP Method: PUT
Request Format: Binary
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {parentid} = [Id from the response of the "Get Folder Path ID"]
             {filename} = [name of the file to upload]
Request: [File binary content]

2020-04-21 08-15-30
William Antunes
 
MVP

thanks Miguel,

your solution worked just fine!!

J Miguel Garcia wrote:

Hi William, 

I've done this previously using the following method with the Microsoft Graph API:

Note:
You can use the Microsoft Graph API to upload files till 4 MB.
Teams is associated with SharePoint and you can create folders in both. In MS Teams open the Team you want to use and select "Open in SharePoint". In SharePoint you can check the URL easily.


1- Register an application in the Microsoft Azure Portal
Here you get:
Application (client) ID
Directory (tenant) ID
Object ID
Client secret


2 - Consume the following REST web service to get a token:
Add API Method: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
HTTP Method: POST
Request Format: Form URL Encoded
Response Format: JSON
In URL: {tenant} = [tenant ID from Azure]
Request Headers: "Content-Type" = "application/x-www-form-urlencoded"
Body Content:
   client_id=[from Azure]
   &scope=https://graph.microsoft.com/.default
   &client_secret=[from Azure]
   &grant_type=client_credentials


3 - Consume Microsoft Graph API REST:

3.1 - Get site ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{hostname}:/sites/{path}?$select=id
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {hostname} = "heiway.sharepoint.com"
             {path} = [path to the Main folder in SharePoint]

3.2 - Get Drive ID:
Add API Method: https://graph.microsoft.com/v1.0/sites/{siteId}/drive
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {siteId} = [Id from the response of the "Get site ID"]


3.3 - Get Folder Path ID:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/root:/{itempath}
HTTP Method: GET
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {itempath} = [sub-folder where to store the File]


3.4 - Put the File in the Folder:
Add API Method: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{parentid}:/{filename}:/content
HTTP Method: PUT
Request Format: Binary
Response Format: JSON
Request Headers: "Authorization" = [token_type] + " " + [access_token] (from the Token response)
In URL: {driveid} = [Id from the response of the "Get Drive ID"]
             {parentid} = [Id from the response of the "Get Folder Path ID"]
             {filename} = [name of the file to upload]
Request: [File binary content]



UserImage.jpg
Paul Davies

If your looking for something similar in code take a look at these modules which are accessing Planner which uses the same groups permissions.

https://www.outsystems.com/forge/component-overview/6927/ms-planner-connector

https://www.outsystems.com/forge/component-overview/6984/app-feedback-to-ms-planner-traditional




Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.