From 3bc616943469ca7e285e01ba9435d3b046daf719 Mon Sep 17 00:00:00 2001
From: IceOnly
Date: Mon, 10 Feb 2025 15:11:30 +0100
Subject: [PATCH] Add FolderExistsByServerRelativeUrl method to SharePoint
Client (#2898)
#### Summary
Adds FolderExists function to the SharePoint System Application Module
#### Work Item(s)
Fixes #2897
Fixes
[AB#565066](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/565066)
---
.../src/SharePointClient.Codeunit.al | 10 ++++++
.../src/SharePointClientImpl.Codeunit.al | 36 +++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/src/System Application/App/SharePoint/src/SharePointClient.Codeunit.al b/src/System Application/App/SharePoint/src/SharePointClient.Codeunit.al
index 90c2ee875f..007fc48469 100644
--- a/src/System Application/App/SharePoint/src/SharePointClient.Codeunit.al
+++ b/src/System Application/App/SharePoint/src/SharePointClient.Codeunit.al
@@ -403,6 +403,16 @@ codeunit 9100 "SharePoint Client"
exit(SharePointClientImpl.CreateFolder(ServerRelativeUrl, SharePointFolder));
end;
+ ///
+ /// Checks if a folder exists.
+ ///
+ /// URL of the folder to check.
+ /// True if the folder exists; otherwise - false.
+ procedure FolderExistsByServerRelativeUrl(ServerRelativeUrl: Text): Boolean
+ begin
+ exit(SharePointClientImpl.FolderExistsByServerRelativeUrl(ServerRelativeUrl));
+ end;
+
///
/// Deletes a folder.
///
diff --git a/src/System Application/App/SharePoint/src/SharePointClientImpl.Codeunit.al b/src/System Application/App/SharePoint/src/SharePointClientImpl.Codeunit.al
index 6b15af2309..e728aa07fb 100644
--- a/src/System Application/App/SharePoint/src/SharePointClientImpl.Codeunit.al
+++ b/src/System Application/App/SharePoint/src/SharePointClientImpl.Codeunit.al
@@ -652,6 +652,25 @@ codeunit 9101 "SharePoint Client Impl."
exit(true);
end;
+ procedure FolderExistsByServerRelativeUrl(ServerRelativeUrl: Text): Boolean
+ var
+ Result: Text;
+ FolderExists: Boolean;
+ begin
+ SharePointUriBuilder.ResetPath();
+ SharePointUriBuilder.SetMethod('GetFolderByServerRelativeUrl', ServerRelativeUrl);
+ SharePointUriBuilder.SetObject('Exists');
+
+ SharePointRequestHelper.SetAuthorization(Authorization);
+ SharePointOperationResponse := SharePointRequestHelper.Get(SharePointUriBuilder);
+ if not SharePointOperationResponse.GetDiagnostics().IsSuccessStatusCode() then
+ exit(false);
+
+ SharePointOperationResponse.GetResultAsText(Result);
+ ParseFolderExistsByServerRelativeUrlResult(Result, FolderExists);
+ exit(FolderExists);
+ end;
+
procedure DeleteFolder(OdataId: Text): Boolean
begin
//DELETE https://{site_url}/_api/web/GetFolderByServerRelativeUrl('{folder_name}')
@@ -778,5 +797,22 @@ codeunit 9101 "SharePoint Client Impl."
SharePointOperationResponse.GetResultAsText(Txt);
exit(true);
end;
+
+ local procedure ParseFolderExistsByServerRelativeUrlResult(Payload: Text; var FolderExists: Boolean)
+ var
+ JObject: JsonObject;
+ JToken: JsonToken;
+ begin
+ if not JObject.ReadFrom(Payload) then
+ exit;
+
+ if not JObject.Get('value', JToken) then
+ exit;
+
+ if not JToken.IsValue() then
+ exit;
+
+ FolderExists := JToken.AsValue().AsBoolean();
+ end;
#endregion
}
\ No newline at end of file