Table of Contents

Folder and file operations

Once connected, TTMSFNCCloudMicrosoftOneDrive organises the account's structure: listing folders, resolving items directly by ID, creating new folders, and moving, renaming or deleting existing items. Every request is asynchronous — a method returns immediately and the outcome arrives in a matching On... event carrying a TTMSFNCCloudBaseRequestResult. This chapter covers listing, resolving, creating, and managing items.

The async result model

No OneDrive method blocks waiting for the network. Check ARequestResult.Success first, and read ARequestResult.ResultString for the raw response or error detail on failure. Returned items are TTMSFNCCloudMicrosoftOneDriveItem objects (a TTMSFNCCloudItem descendant) exposing FileName, Size, ID and ParentID — every item now carries its ParentID, populated from the server's parent-folder reference, so you can walk back up the hierarchy without a separate lookup.

Listing a folder

GetFolderList lists a folder — pass nil for the account root, or a folder item to list its contents. Items arrive in OnGetFolderList.

procedure TForm1.ListRootFolder;
begin
  TMSFNCCloudMicrosoftOneDrive1.OnGetFolderList := RootFolderListed;
  TMSFNCCloudMicrosoftOneDrive1.GetFolderList(nil); // nil = account root
end;

procedure TForm1.RootFolderListed(Sender: TObject; const AFolderList: TTMSFNCCloudItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  i: Integer;
begin
  if not ARequestResult.Success then
    Exit;

  for i := 0 to AFolderList.Count - 1 do
    Memo1.Lines.Add(AFolderList[i].FileName);
end;

For a recursive tree, GetFolderListHierarchical walks subfolders. To resolve a single item you already know the provider ID for, GetFileByID returns it directly and synchronously — there is no completion event for this one call. GetFileInfo and GetFolderInfo request the same metadata asynchronously instead, both reporting through OnGetFileInfo.

function TForm1.GetItemNameByID(const AItemID: string): string;
var
  it: TTMSFNCCloudItem;
begin
  Result := '';
  it := TMSFNCCloudMicrosoftOneDrive1.GetFileByID(AItemID);
  if Assigned(it) then
    Result := it.FileName;
end;

procedure TForm1.RequestFileInfoAsync(const AItemID: string);
begin
  TMSFNCCloudMicrosoftOneDrive1.OnGetFileInfo := OneDriveFileInfo;
  TMSFNCCloudMicrosoftOneDrive1.GetFileInfo(AItemID);
end;

procedure TForm1.OneDriveFileInfo(Sender: TObject;
  const AFile: TTMSFNCCloudMicrosoftOneDriveItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add(AFile.FileName + ' (parent: ' + AFile.ParentID + ')');
end;

Creating, moving, renaming and deleting

CreateFolder adds a folder (pass nil as the parent for the root) and reports the new item in OnCreateFolder. MoveFile moves an item into a target folder, MoveFileToRoot moves it back to the account root, and RenameFile changes its name — each raises OnMoveFile / OnRenameFile. Delete removes an item, by reference or by provider ID.

procedure TForm1.CreateReportsFolder;
begin
  TMSFNCCloudMicrosoftOneDrive1.OnCreateFolder := OneDriveFolderCreated;
  TMSFNCCloudMicrosoftOneDrive1.CreateFolder(nil, 'Reports'); // nil parent = root
end;

procedure TForm1.OneDriveFolderCreated(Sender: TObject; const AFolder: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add('Created folder ' + AFolder.FileName);
end;

procedure TForm1.MoveRenameDeleteItem(AItem, ATargetFolder: TTMSFNCCloudItem);
begin
  TMSFNCCloudMicrosoftOneDrive1.OnMoveFile := OneDriveFileMoved;
  TMSFNCCloudMicrosoftOneDrive1.OnRenameFile := OneDriveFileRenamed;
  TMSFNCCloudMicrosoftOneDrive1.OnDeleteItem := OneDriveItemDeleted;

  TMSFNCCloudMicrosoftOneDrive1.MoveFile(AItem, ATargetFolder);
  TMSFNCCloudMicrosoftOneDrive1.RenameFile(AItem, 'Q4-final.pdf');
  TMSFNCCloudMicrosoftOneDrive1.Delete(AItem);
end;

procedure TForm1.OneDriveFileMoved(Sender: TObject; const AFile: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add('Moved to ' + AFile.FileName);
end;

procedure TForm1.OneDriveFileRenamed(Sender: TObject; const AFile: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add('Renamed to ' + AFile.FileName);
end;

procedure TForm1.OneDriveItemDeleted(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add('Item deleted');
end;

Combining create and move

Because each step is event-driven, you chain operations by starting the next one from the previous completion event. This creates a folder, then moves an item into it once the folder exists:

// Assumes a private field: FItemToArchive: TTMSFNCCloudItem;
// (needed to carry the item across the CreateFolder -> MoveFile event chain).
procedure TForm1.ArchiveItem(AItem: TTMSFNCCloudItem);
begin
  FItemToArchive := AItem;
  TMSFNCCloudMicrosoftOneDrive1.OnCreateFolder := OneDriveArchiveFolderCreated;
  TMSFNCCloudMicrosoftOneDrive1.CreateFolder(nil, 'Archive');
end;

procedure TForm1.OneDriveArchiveFolderCreated(Sender: TObject; const AFolder: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    TMSFNCCloudMicrosoftOneDrive1.MoveFile(FItemToArchive, AFolder);
end;

Common mistakes

  • Reading results outside the event. A method returns before the network call completes — use the On... event, not the method's return value (GetFileByID is the one exception; it blocks and returns the resolved item directly).
  • Deleting by a stale reference. Delete the item returned by a current listing, not a cached one whose path may have changed after a rename or move.

See also