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