private/Get-OneListItem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Get-OneListItem {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string] $SiteId,

        [Parameter(Mandatory = $true)]
        [string] $DocumentLibraryId,

        [Parameter(Mandatory = $true)]
        [string] $FolderPath
    )

    begin {
        Write-Verbose "[$(Get-Date)] [BEGIN ] $($MyInvocation.MyCommand)"

        Write-Verbose "[$(Get-Date)] [CALL ] Test-OneConnection()"
        if (!(Test-OneConnection)) {
            Write-Error "[$(Get-Date)] [ERROR ] No connection established. Please use Connect-OneShortcut to create a session." -ErrorAction Stop
        }
    }

    process {
        $ApiRequest = @{
            ApiResource = "sites/$($SiteId)/lists/$($DocumentLibraryId)/items"
            ApiRequestMethod = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get
        }
 
        Write-Verbose "[$(Get-Date)] [CALL ] Invoke-OneApiRequest($($ApiRequest.ApiResource), $($ApiRequest.ApiRequestMethod))"
        $ApiResponse = (Invoke-OneApiRequest @ApiRequest)

        if ($ApiResponse.Response.StatusCode -eq "200") {
            $ApiResponse.Data = ($ApiResponse.Data.value | Where-Object { $_.webUrl -like "*$([uri]::EscapeDataString($FolderPath))" } )

            if (!$ApiResponse.Data) {
                $ApiResponse.Response.StatusCode = "404"
                $ApiResponse.Response.Message = "List Item not found."
            }
        }

        return $ApiResponse
    }

    end {
        Write-Verbose "[$(Get-Date)] [END ] $($MyInvocation.MyCommand)"
    }
}