Skip to content

Commit

Permalink
Add FolderExistsByServerRelativeUrl method to SharePoint Client (#2898)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
Adds FolderExists function to the SharePoint System Application Module

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes #2897

Fixes
[AB#565066](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/565066)
  • Loading branch information
IceOnly authored Feb 10, 2025
1 parent 22e9e48 commit 3bc6169
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ codeunit 9100 "SharePoint Client"
exit(SharePointClientImpl.CreateFolder(ServerRelativeUrl, SharePointFolder));
end;

/// <summary>
/// Checks if a folder exists.
/// </summary>
/// <param name="ServerRelativeUrl">URL of the folder to check.</param>
/// <returns>True if the folder exists; otherwise - false.</returns>
procedure FolderExistsByServerRelativeUrl(ServerRelativeUrl: Text): Boolean
begin
exit(SharePointClientImpl.FolderExistsByServerRelativeUrl(ServerRelativeUrl));
end;

/// <summary>
/// Deletes a folder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -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
}

0 comments on commit 3bc6169

Please sign in to comment.