Show:      

MSGraphClient

Created by programaths on 17.03.23.
Signature
global with sharing class MSGraphClient

MSGraphClient Methods

createFolder(userId, driveId, parentId, name)

Create folder "name" in drive identified by "driveId" with parent identified by "parentId"
Signature
global CollabMSGraphClient.SaveFileResponse createFolder(String userId, String driveId, String parentId, String name)
Parameters
userId
Type: String
user ID
driveId
Type: String
drive ID
parentId
Type: String
parent ID
name
Type: String
folder name
Returns
created folder
Example
  //For Unit Testing, make sure tho init the client like below 
  MSGraphClient client; 
   if(Test.isRunningTest()){ 
       client=MSGraphClient.createMock(new MSGraphClient.MSGraphClientStub()); 
   }else{ 
       client=new MSGraphClient(); 
   } 
 //the client is now initiated for Unit Testing or general execution. The rest of the calls can be the same for Unit Testing or general execution. 
 //   below and example of a call 
 
 String libraryUrl = 'https://company.sharepoint.com/sites/MySiteName'; 
 cadmus_una.CollabMSGraphClient.GetDriveResponse dr=cadmus_una.CollabMSGraphClient.getSiteDrive(UserInfo.getUserId(), 
      'https://cloudcrossingautomation.sharepoint.com/sites/Marketing21/Gedeelde documenten'); 
 cadmus_una.CollabMSGraphClient.GetDriveResponse di=cadmus_una.CollabMSGraphClient.getDriveItemByPath(UserInfo.getUserId(), dr.data.id, '/General'); 
 cadmus_una.MSGraphClient.SaveFileResponse fileResponse = client.createFolder(UserInfo.getUserId(), dr.data.id, di.data.id, '/MyCoolFolder');

createMock(stubProvider)

the unit test stub which implements the mock methods
Signature
global static MSGraphClient createMock(StubProvider stubProvider)
Parameters
stubProvider
Type: StubProvider
use the default Stub from COLLABORATION Butler "new MSGraphClient.MSGraphClientStub()". Or create your own Stub for each method you want to use.
Returns
Mock
Example
  MSGraphClient client; 
   if(Test.isRunningTest()){ 
       client=MSGraphClient.createMock(new MSGraphClient.MSGraphClientStub()); 
   }else{ 
       client=new MSGraphClient(); 
   } 
 //the client is now initiated for Unit Testing or general execution. The rest of the calls can be the same for Unit Testing or general execution. 
 //   below and example of a call 
 
 String libraryUrl = 'https://company.sharepoint.com/sites/MySiteName'; 
 cadmus_una.MSGraphClient.GetDriveResponse driveResponse = client.getSiteDrive(UserInfo.getUserId(), libraryUrl);

ensurePath(userId, driveId, path)

Ensures that the given path exists in the specified drive. If any folder in the path doesn't exist, this method attempts to create it.
Signature
global Boolean ensurePath(String userId, String driveId, String path)
Parameters
userId
Type: String
The user ID for which the path should be ensured.
driveId
Type: String
The drive ID in which the path should be ensured.
path
Type: String
The path to be ensured, with each folder separated by a forward slash (/).
Returns
true if the path exists or has been successfully created, false otherwise.
Example
 String userId = "user123"; 
 String driveId = "drive123"; 
 String path = "Projects/MyProject/SubFolder"; 
 
 Boolean result = cadmus_una.MSGraphClient.ensurePath(userId, driveId, path); 
 if (result) { 
     System.out.println("Path ensured: " + path); 
 } else { 
     System.out.println("Failed to ensure path: " + path); 
 }

getDriveItemByPath(userId, driveId, path)

Retrieve drive item in drive "driveId" at path <var>path</var>
Signature
global CollabMSGraphClient.GetDriveResponse getDriveItemByPath(String userId, String driveId, String path)
Parameters
userId
Type: String
User Id
driveId
Type: String
drive ID
path
Type: String
path
Returns
drive item at path <var>path</var>. See: GetDriveResponse
Example
  void listChildren(String driveId,String childId,Integer level){ 
        cadmus_una.MSGraphClient.GetDriveContentResponse contentResponse=cadmus_una.MSGraphClient.getDriveItemsById( 
            UserInfo.getUserId(),driveId,childId); 
        for(cadmus_una.MsGraphContentResponse.Value v: contentResponse.data.value){ 
            System.debug(LoggingLevel.DEBUG,'-'.repeat(level)+v.name+'('+((v.folder==null)?'file':v.folder.childCount+')')); 
            System.debug(LoggingLevel.DEBUG,v.folder); 
            if(v.folder!=null){ 
                listChildren(driveId,v.id,level+1); 
            } 
        } 
    } 
    cadmus_una.MSGraphClient.GetDriveResponse dr=cadmus_una.MSGraphClient.getSiteDrive(UserInfo.getUserId(), 
            'https://cloudcrossingautomation.sharepoint.com/sites/Marketing21/Gedeelde documenten'); 
    cadmus_una.MSGraphClient.GetDriveResponse di=cadmus_una.MSGraphClient.getDriveItemByPath(UserInfo.getUserId(), dr.data.id, '/General'); 
    listChildren(dr.data.id,di.data.id,0);

getSiteDrive(userId, libraryUrl)

Get drive from URL, tacking into account sites and libraries.
Signature
global CollabMSGraphClient.GetDriveResponse getSiteDrive(String userId, String libraryUrl)
Parameters
userId
Type: String
user Id
libraryUrl
Type: String
site/library URL
Returns
corresponding drive: GetDriveResponse
Example
 String userId = '0051N0000017s9sQAA'; 
 String libraryUrl = 'https://company.sharepoint.com/sites/MySiteName'; 
 cadmus_una.CollabMSGraphClient.GetDriveResponse driveResponse = cadmus_una.CollabMSGraphClient.getSiteDrive(userId, libraryUrl); 
 
 if (driveResponse.data != null) { 
     System.debug('Drive ID: ' + driveResponse.data.id); 
 } else { 
     System.debug('Error: ' + driveResponse.error.message); 
 }

renameFolder(userId, url, isLibrary, basePath, sourceName, targetName)

Change the name of folder in a SharePoint Library
Signature
global static CollabMSGraphClient.SaveFileResponse renameFolder(String userId,String url, Boolean isLibrary, String basePath, String sourceName,String targetName)
Parameters
userId
Type: String
User ID
url
URR
isLibrary
Type: Boolean
Is <var>url</var> pointing to a Library ?
basePath
Type: String
Path to folder containing the driveItem to rename
sourceName
Type: String
driveItem source name
targetName
driveItem desired name
Returns
changed driveItem, the id field contains the list item id
Example
 //The folder we want to change is: /CollabAppExchange/Acme - 200 Widgets/CustomerInput/Unchanged 
 cadmus_una.MSGraphClient.renameFolder(UserInfo.getUserId(), 
           'https://cloudcrossingautomation.sharepoint.com/sites/COLLABORATIONTEST/Shared Documents', //=> Site and Library URL 
			 true, //=> handle this as a library, not a drive 
			 '/CollabAppExchange/Acme - 200 Widgets/CustomerInput',  //=> basePath to folder to change 
			 'Unchanged', //=> name of the folder to change 
			 'Changed')); //=> name of the folder to change it into

sanitizeFileName(dirtyFilename)

General cleanup of characters SharePoint cannot deal with
Signature
global String sanitizeFileName(String dirtyFilename)
Parameters
dirtyFilename
Type: String
Filename without the path
Returns
clean URL
Example
 // Becomes 'dsc001[new]_.jpeg' 
 MSGraphClient.sanitizeFileName('dsc001[new]/.jpeg')

sanitizeFolderName(dirtyFolderName)

General cleanup of characters SharePoint cannot deal with
Signature
global String sanitizeFolderName(String dirtyFolderName)
Parameters
dirtyFolderName
Type: String
Folder name not the entire path
Returns
clean URL

sanitizeFolderPath(dirtyFolderPath)

General cleanup of characters SharePoint cannot deal with
Signature
global String sanitizeFolderPath(String dirtyFolderPath)
Parameters
dirtyFolderPath
Type: String
Url path of the folder without filename (use{@link CollabMSGraphClient.sanitizeFileName})
Returns
clean URL
Example
 // Becomes 'dsc001[new]_jpegs_' 
 MSGraphClient.sanitizeFolderName('dsc001[new].jpegs/');

saveFileById(userId, driveId, parentId, fileName, fileContent)

Save file in a site drive using IDs
Signature
global CollabMSGraphClient.SaveFileResponse saveFileById(String userId, String driveId, String parentId, String fileName, Blob fileContent)
Parameters
userId
Type: String
user ID
driveId
Type: String
drive ID
parentId
Type: String
parent ID
fileName
Type: String
file name
fileContent
Type: Blob
file content
Returns
saved file

MSGraphClient.MSGraphClientStub

Default mock returning only succesful responses. The below will show how the Stub behaves and which values are returned so they can be used in testing and asserting.
Signature
global class MSGraphClientStub implements StubProvider
Example
 getSiteDrive -> GetDriveResponseData('42'); 
 getDriveItemByPath -> GetDriveResponseData('42'); 
 saveFileById -> SaveFileResponse('4242','test',4269,'https://example.com'); 
 createFolder -> SaveFileResponse('4242','test',4269,'https://example.com'); 
 renameFolder -> SaveFileResponse('4242','test',4269,'https://example.com');