Enables integration with Microsoft Graph.
The component handles authentication and HTTP requests, but you will need to refer to the the Graph API documentation to learn how each API is used. The Graph Explorer is also a valuable tool for experimenting with Graph APIs.
The system settings GraphDefaultTenantId and GraphDefaultCredentials may be used to set defaults for the credentials used to access Graph.
Used to dynamically overwrite the default authorization settings and credentials and to set a batch id.
Gets the initial commands to be performed by the component.
| Possible value | Description | 
|---|---|
| DELETE | |
| GET | |
| PATCH | |
| POST | |
| PUT | 
| Possible value | Description | 
|---|---|
| beta | |
| v1.0 | 
Specifies custom request headers to send with the call. See the API documentation for each individual call to determine which, if any, headers it accepts. The Authorization header is automatically set by the component.
You can not specify any Content-* headers in this call. Those are specified by the Request data call instead.
Called once for each command. This call can optionally emit new commands to perform.
| Possible value | Description | 
|---|---|
| <XXX> | Any error code from Microsoft Graph | 
| Softadmin_ApplicationThrottled | The component tried to make a call disregarding previous returned @RetryAfter. The call has been ignored. | 
| Possible value | Description | 
|---|---|
| DELETE | |
| GET | |
| PATCH | |
| POST | |
| PUT | 
| Possible value | Description | 
|---|---|
| beta | |
| v1.0 | 
Call made when all commands have finished.
Displays a user friendly error message to the user. This blocks any forwarding for the user.
Displays a user friendly confirmation message to the user with a delete button as the confirm button. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Delete'.
Displays a user friendly confirmation message to the user with a question style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.
Displays a user friendly confirmation message to the user with a warning style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.
Displays a user friendly info message to the user. When the user clicks OK the user is forwarded.
Displays a user friendly success message to the user. When the user clicks OK the user is forwarded.
The dialog alias of a predefined dialog to show the user. Must be the first column in the result set table. Use multiple result set tables to combine with other forwarding.
Use the menu item "Admin > Dialogs" to register new dialogs or find aliases for existing ones.
Any column without special meaning in the result set with the first column ADMIN_Dialog will be used to make replacements of placeholders in the message and title text.
Additional information to show the developer when using ADMIN_Dialog.
Deprecated. Use ADMIN_CancelMenuItem instead.
Id of the menuitem to execute if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelmenuitemid specified in the query string.
Alias of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.
Deprecated. Use ADMIN_ForwardMenuGroup instead.
Id of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.
Deprecated. Use ADMIN_ForwardMenuItem instead.
Id of the menu item to execute after execution (instead of former menu item). This value overrides any destination specified by the query string.
Displays a user friendly error message to the user.
If this column is anything but NULL the popup will be closed and the parent will be reloaded. Only select this column if the menu item is opened in a popup. Avoid using this feature if the opener is a newEdit as that may interrupt the user's ongoing input.
Changes the text of the Cancel button when used with ADMIN_Force, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete.
Changes the text of the OK button when used with ADMIN_ErrorMessage, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete, ADMIN_InfoMessage, ADMIN_SuccessMessage, ADMIN_Message, ADMIN_Force, or ADMIN_Forward. ADMIN_Force,
Allows you to validate the SQL parameters before any other SQL is run in the component. This call is only made if the SQL is a stored procedure and Validate parameters is checked.
Use this call to restrict which entries a user is allowed to view and edit, and to log which entries a user views.
Access to a menu item is normally controlled through functions and roles alone but some entities need more fine grained control. For example, a user may have access to the View Member menu item for normal members but not for members with a protected identity.
The menu items a user visits are always logged (in ADMINLogMenuItem) but for sensitive data you may need to log exactly what entries are viewed. Do the logging in this call as the common ways of viewing data (grid and InfoSQL) are not allowed to modify the database.
If you bind a scalar function instead of a stored procedure to this call then its name must end with '_GrantAccess'.
CREATE OR ALTER PROCEDURE [Example].[Graph]
	@Action varchar(max) = NULL,
	@Id varchar(max) = NULL,
	@ResponseJson nvarchar(max) = NULL,
	@HttpStatusCode int = NULL,
	@ErrorMessage varchar(max) = NULL,
	@ErrorCode varchar(max) = NULL,
	@RetryAfterUtc datetime2(2) = NULL
AS
BEGIN
	SET XACT_ABORT ON;
	IF @Action = 'Init'
	BEGIN
		RETURN;
	END;
	
	IF @Action = 'Commands'
	BEGIN
		SELECT
			GC.Path AS Path,
			'v1.0' AS ApiVersion,
			GC.HttpMethod AS HttpMethod,
			NULL AS BeginExtraParams,
			GC.GraphCommandQueueId AS Id
		FROM
			dbo.GraphCommandQueue GC
			LEFT JOIN (SELECT MAX(GS.RetryAfter) AS RetryAfter FROM dbo.GraphState GS) S ON 1 = 1
		WHERE
			SYSDATETIME() > ISNULL(S.RetryAfter, '1900');
		RETURN;
	END;
	IF @Action = 'RequestData'
	BEGIN
		SELECT
			GC.RequestJson
		FROM
			dbo.GraphCommandQueue GC
		WHERE
			GC.GraphCommandQueueId = @Id;
		RETURN;
	END;
	IF @Action = 'StoreResponse'
	BEGIN
		BEGIN TRAN;
		DELETE dbo.GraphState;
		IF @RetryAfter IS NOT NULL
		BEGIN
			INSERT INTO dbo.GraphState
			(
				RetryAfter
			)
			VALUES
			(
				@RetryAfter
			);
		END;
		COMMIT;
		BEGIN TRAN;
		IF @HttpStatusCode = 2XX
		BEGIN
			DELETE dbo.GraphCommandQueue
			WHERE
				GraphCommandQueueId = @Id;
		END;
		ELSE
		BEGIN
			UPDATE dbo.GraphCommandQueue SET
				ResponseJson = @ResponseJson,
				ErrorCode = @ErrorCode,
				ErrorMessage = @ErrorMessage,
				HttpStatusCode = @HttpStatusCode
			WHERE
				GraphCommandQueueId = @Id;
			DECLARE @LogMessage varchar(max) = 
				CONCAT('Error occured when performing Action for dbo.GraphCommandQueue: GraphCommandQueueId:', @Id);
			EXEC SoftadminApi.Log_LogError
				@LogMessage = @LogMessage;
		END;
		COMMIT;
		RETURN;
	END;
	IF @Action = 'Finished'
	BEGIN
		RETURN;
	END;
END;
								This demonstrates how you can use the 64x64 URL segment to download resized photos, and use ETags to only download updated photos.
CREATE OR ALTER PROCEDURE Example.SyncPhotosFromGraph
	@Action			varchar(50),
	@UserId			int = NULL,
	@HttpStatusCode	int = NULL,
	@ResponseJson	varchar(max) = NULL,
	@ResponseBinary	varbinary(max) = NULL,
	@ErrorCode		varchar(300) = NULL,
	@ErrorMessage	varchar(max) = NULL,
	@RetryAfterUtc	datetime2(0) = NULL
AS
BEGIN
	IF @Action = 'Init'
	BEGIN
		RETURN;
	END;
	IF @Action = 'Commands'
	BEGIN
		SELECT
			'GET' AS HttpMethod,
			CONCAT(
				'/users/',
				U.Username,
				'/photos/64x64/$value'
			) AS Path,
			'/v1.0' AS ApiVersion,
			1 AS SendCustomRequestHeaders, -- We want to send ETag.
			1 AS ReceiveResponseHeaders, -- We want to receive ETag.
			1 AS ResponseIsBinary, -- We're downloading photos, not JSON.
			NULL AS BeginExtraParams,
			U.UserId -- Pass the user ID to RequestHeaders and StoreResponse.
		FROM
			SoftadminApi.[User] U
		WHERE
			U.IsEnabled = 1 AND
			U.Username LIKE '%@%' AND
			1=1; -- Add your own logic for deciding which users to sync here.
		RETURN;
	END;
	IF @Action = 'RequestData'
	BEGIN
		-- All requests are GET-request, meaning they don't have a body,
		-- so this Action will never get called.
		RETURN;
	END;
	IF @Action = 'RequestHeaders'
	BEGIN
		-- Use the If-None-Match header to only fetch changed profile pictures.
		SELECT
			'If-None-Match' AS HttpHeaderName,
			ETag AS HttpHeaderValue
		FROM
			SoftadminApi.UserPhoto
		WHERE
			UserId = @UserId;
		RETURN;
	END;
	IF @Action = 'StoreResponse'
	BEGIN
		IF @HttpStatusCode = 304
		BEGIN
			-- The photo is unchanged since previous sync.
			RETURN;
		END;
		IF @HttpStatusCode = 200
		BEGIN
			DECLARE
				@ResponseContentType varchar(300) = (
					SELECT HttpHeaderValue
					FROM #ResponseHeaders
					WHERE HttpHeaderName = 'content-type'),
				@ResponseETag varchar(300) = (
					SELECT HttpHeaderValue
					FROM #ResponseHeaders
					WHERE HttpHeaderName = 'etag');
			BEGIN TRANSACTION;
				-- Remove the existing user photo and generate a new one with a new ID.
				-- User photos remain cached for a year, reusing the same UserPhotoId would
				-- result in all browsers retaining the previous photo.
				DELETE SoftadminApi.UserPhoto WHERE UserId = @UserId;
				INSERT SoftadminApi.UserPhoto
				(
					UserId,
					UserPhoto,
					UserPhotoContentType,
					ETag,
					UpdateDatetime
				)
				VALUES
				(
					@UserId,
					@ResponseBinary,
					@ResponseContentType,
					@ResponseETag,
					SYSDATETIMEOFFSET()
				);
			COMMIT TRANSACTION;
			RETURN;
		END;
		IF @HttpStatusCode = 404
		BEGIN
			-- The user does not exist or does not have a photo.
			RETURN;
		END;
		IF @HttpStatusCode BETWEEN 400 AND 599
		BEGIN
			-- Log unexpected errors.
			DECLARE @LogMessage varchar(MAX) = CONCAT('Graph returned an error when fetching photo: ', @ErrorCode);
			EXEC SoftadminApi.Log_LogError
				@LogMessage	= @LogMessage,
				@LogTrace	= @ErrorMessage,
				@MenuItemId	= 123; -- Use your own menu item ID.
		END;
		RETURN;
	END;
	IF @ErrorCode IS NOT NULL OR @ErrorMessage IS NOT NULL
	BEGIN
		RAISERROR('%s %s', 16, 1,@ErrorCode, @ErrorMessage);
	END;
END;