SQL Server guide

Call generic API request


Calls any Office 365 / Outlook API using the generic_request endpoint. Use when the connector does not define a specific endpoint but you need to call that API.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT * FROM generic_request
  WITH (
  URL='/users/049beb7f-03e0-4b0d-825d-73567b6786e9'
  , RequestMethod='GET'
  , Filter='$' --optional if you like to read from array inside document
  --Try commenting below line or define static metadata (speed up API calls by not requesting columns)
  , Meta='businessPhones:String(220); displayName:String(230); givenName:String(100); jobTitle:String(255); mail:String(310); mobilePhone:String(255); officeLocation:String(255); preferredLanguage:String(255); surname:String(140); userPrincipalName:String(500); id:String(360); '
  )

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY], 'SELECT * FROM generic_request
  WITH (
  URL=''/users/049beb7f-03e0-4b0d-825d-73567b6786e9''
  , RequestMethod=''GET''
  , Filter=''$'' --optional if you like to read from array inside document
  --Try commenting below line or define static metadata (speed up API calls by not requesting columns)
  , Meta=''businessPhones:String(220); displayName:String(230); givenName:String(100); jobTitle:String(255); mail:String(310); mobilePhone:String(255); officeLocation:String(255); preferredLanguage:String(255); surname:String(140); userPrincipalName:String(500); id:String(360); ''
  )')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM generic_request
  WITH (
  URL=''/users/049beb7f-03e0-4b0d-825d-73567b6786e9''
  , RequestMethod=''GET''
  , Filter=''$'' --optional if you like to read from array inside document
  --Try commenting below line or define static metadata (speed up API calls by not requesting columns)
  , Meta=''businessPhones:String(220); displayName:String(230); givenName:String(100); jobTitle:String(255); mail:String(310); mobilePhone:String(255); officeLocation:String(255); preferredLanguage:String(255); surname:String(140); userPrincipalName:String(500); id:String(360); ''
  )'
EXEC (@MyQuery) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY]