Skip to main content
Table of Contents

Search and sharing

Beyond browsing folder by folder, TTMSFNCCloudMicrosoftOneDrive can search the account directly and request a share link for an item without making it fully public. Both are useful once an account holds more content than a user can reasonably browse — a search narrows the account down to a handful of candidates, and a share link lets you hand a specific file to someone outside your app without granting them access to the whole OneDrive. This chapter covers the two search variants and requesting a share URL.

Searching

Search looks for items matching a query, optionally scoped to a folder path — pass an empty folder name to search from the account root. SearchList searches from a given folder for items matching a query, with an exact-or-partial match option. Both are asynchronous; read the result from OnSearch, not from the method's return value.

Search a OneDrive accountPascalC++
procedure TForm1.SearchForInvoices;
begin
  TMSFNCCloudMicrosoftOneDrive1.OnSearch := OneDriveSearchResults;
  // Search the root for items whose name or content matches "invoice".
  TMSFNCCloudMicrosoftOneDrive1.Search('invoice');
end;

procedure TForm1.OneDriveSearchResults(Sender: TObject;
  const ASearchResults: TTMSFNCCloudMicrosoftOneDriveItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  i: Integer;
begin
  if not ARequestResult.Success then
    Exit;

  for i := 0 to ASearchResults.Count - 1 do
    Memo1.Lines.Add(ASearchResults[i].FileName);
end;
void __fastcall TForm1::SearchForInvoices()
{
  TMSFNCCloudMicrosoftOneDrive1->OnSearch = OneDriveSearchResults;
  // Search the root for items whose name or content matches "invoice".
  TMSFNCCloudMicrosoftOneDrive1->Search("invoice");
}

void __fastcall TForm1::OneDriveSearchResults(TObject *Sender,
  TTMSFNCCloudMicrosoftOneDriveItems *ASearchResults,
  TTMSFNCCloudBaseRequestResult *ARequestResult)
{
  if (!ARequestResult->Success)
    return;

  for (int i = 0; i < ASearchResults->Count; i++)
    Memo1->Lines->Add(ASearchResults->Items[i]->FileName);
}

GetShare requests a URL for an item, raising OnGetShare when it is available. The ReadOnly parameter controls whether the link grants view-only or edit access — default it to True unless the recipient needs to edit the file.

Request a share link for a OneDrive itemPascalC++
procedure TForm1.ShareItemReadOnly(AItem: TTMSFNCCloudMicrosoftOneDriveItem);
begin
  TMSFNCCloudMicrosoftOneDrive1.OnGetShare := OneDriveShareReceived;
  TMSFNCCloudMicrosoftOneDrive1.GetShare(AItem, True); // True = view-only link
end;

procedure TForm1.OneDriveShareReceived(Sender: TObject; const AShare: string;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add('Share link: ' + AShare);
end;
void __fastcall TForm1::ShareItemReadOnly(TTMSFNCCloudMicrosoftOneDriveItem *AItem)
{
  TMSFNCCloudMicrosoftOneDrive1->OnGetShare = OneDriveShareReceived;
  TMSFNCCloudMicrosoftOneDrive1->GetShare(AItem, true); // true = view-only link
}

void __fastcall TForm1::OneDriveShareReceived(TObject *Sender, const System::UnicodeString AShare,
  TTMSFNCCloudBaseRequestResult *ARequestResult)
{
  if (ARequestResult->Success)
    Memo1->Lines->Add("Share link: " + AShare);
}

Combining a search with a share request

Searching first lets you locate an item without already holding a reference to it — here a search resolves an item by name, and a read-only share link is requested for the first match once the search completes:

Search for an item, then request a share linkPascalC++
procedure TForm1.SearchAndShare(const AQuery: string);
begin
  TMSFNCCloudMicrosoftOneDrive1.OnSearch := OneDriveSearchThenShare;
  TMSFNCCloudMicrosoftOneDrive1.OnGetShare := OneDriveSearchedItemShared;
  TMSFNCCloudMicrosoftOneDrive1.Search(AQuery);
end;

procedure TForm1.OneDriveSearchThenShare(Sender: TObject;
  const ASearchResults: TTMSFNCCloudMicrosoftOneDriveItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if not ARequestResult.Success then
    Exit;

  if ASearchResults.Count > 0 then
    TMSFNCCloudMicrosoftOneDrive1.GetShare(ASearchResults.Items[0], True);
end;

procedure TForm1.OneDriveSearchedItemShared(Sender: TObject; const AShare: string;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    ShowMessage('Share link for the top match: ' + AShare);
end;
void __fastcall TForm1::SearchAndShare(const System::UnicodeString AQuery)
{
  TMSFNCCloudMicrosoftOneDrive1->OnSearch = OneDriveSearchThenShare;
  TMSFNCCloudMicrosoftOneDrive1->OnGetShare = OneDriveSearchedItemShared;
  TMSFNCCloudMicrosoftOneDrive1->Search(AQuery);
}

void __fastcall TForm1::OneDriveSearchThenShare(TObject *Sender,
  TTMSFNCCloudMicrosoftOneDriveItems *ASearchResults,
  TTMSFNCCloudBaseRequestResult *ARequestResult)
{
  if (!ARequestResult->Success)
    return;

  if (ASearchResults->Count > 0)
    TMSFNCCloudMicrosoftOneDrive1->GetShare(ASearchResults->Items[0], true);
}

void __fastcall TForm1::OneDriveSearchedItemShared(TObject *Sender, const System::UnicodeString AShare,
  TTMSFNCCloudBaseRequestResult *ARequestResult)
{
  if (ARequestResult->Success)
    ShowMessage("Share link for the top match: " + AShare);
}

Common mistakes

  • Reading the return value of Search/SearchList. Both execute asynchronously — the returned collection is not guaranteed to be populated yet. Use OnSearch.
  • Requesting an edit link when a view-only link is intended. ReadOnly defaults to True; pass False explicitly only when the recipient needs to modify the file.
  • Sharing a base TTMSFNCCloudItem. GetShare takes a TTMSFNCCloudMicrosoftOneDriveItem; items returned by a search or listing are already typed correctly, so this only matters when you construct or cast an item yourself.

See also