Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

[WS] SearchRecordsByReport multiple pages

SergeiBakhaev
Contributor III

Hello!

 

Recently, I've faced a report in Archer, that have a lot of records. So, in API request i need to make several requests with different page numbers.

I'm using powershell, and this is my search function:

function Search($session_token, $reportGUID) {
    $api_url = $base_url + "/ws/search.asmx"
    try {
        $ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
        $page = 1
        Do {
            [xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
            $xml_file += $xdoc.InnerXml
            $page++
        } While ($xdoc.Records.Record.Count -gt 0)       
    }
    catch {
        Error -exception $_.Exception
    }
    return $xml_file
}

Search itself work great, but in result I have invalid xml file. It contains multiple xml declarations, field descriptions, etc. (because of multiple pages in report, i suppose).

 

So, anyone faced same issue?

How to properly get all of the records from report without extra information?

1 ACCEPTED SOLUTION

Accepted Solutions

To return proper XML, with field definitions and etc., I've updated function.

function Error($exception) {
    $exception | Format-List -Force
    exit
}

function SearchRecordsByReport($session_token, $reportGUID) {
    $api_url = $base_url + "/ws/search.asmx"
    $xml_file = New-Object System.Xml.XmlDocument
    try {
        $ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
        $page = 1
        Do {
            [xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
            if ($page -eq 1) {
                $xml_file = $xdoc
            }
            else {
                ForEach ($record in $xdoc.SelectNodes("/Records/Record")) {
                    $records = $xml_file.SelectSingleNode("/Records")
                    $records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
                }
            }
            $page++
        } While ($xdoc.Records.Record.Count -gt 0)       
    }
    catch {
        Error -exception $_.Exception
    }
    return $xml_file.InnerXml
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

7 REPLIES 7

SergeiBakhaev
Contributor III

If anyone faces same issue, here is my updated function.

 

Basically, instead of adding whole XML text of the response, I'm merging only records itself.

function Search($session_token, $reportGUID) {
    $api_url = $base_url + "/ws/search.asmx"
    $xml_file = New-Object System.Xml.XmlDocument
    $xml_file.LoadXML("<Records></Records>")
    try {
        $ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
        $page = 1
        Do {
            [xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
            ForEach ($record in $xdoc.SelectNodes("/Records/Record")) {
                $records = $xml_file.SelectSingleNode("/Records")
                $records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
            }
            $page++
        } While ($xdoc.Records.Record.Count -gt 0)       
    }
    catch {
        Error -exception $_.Exception
    }
    return $xml_file.InnerXml
}

To return proper XML, with field definitions and etc., I've updated function.

function Error($exception) {
    $exception | Format-List -Force
    exit
}

function SearchRecordsByReport($session_token, $reportGUID) {
    $api_url = $base_url + "/ws/search.asmx"
    $xml_file = New-Object System.Xml.XmlDocument
    try {
        $ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
        $page = 1
        Do {
            [xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
            if ($page -eq 1) {
                $xml_file = $xdoc
            }
            else {
                ForEach ($record in $xdoc.SelectNodes("/Records/Record")) {
                    $records = $xml_file.SelectSingleNode("/Records")
                    $records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
                }
            }
            $page++
        } While ($xdoc.Records.Record.Count -gt 0)       
    }
    catch {
        Error -exception $_.Exception
    }
    return $xml_file.InnerXml
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

PrathameshYedav
Collaborator II

Hi Sergei Bakhaev‌,

We tried executing the above code but are unable to retrieve the location of the returned file.

PrathameshYedav
Collaborator II

MadhusudhanRama
Contributor II

I am able to see all the records on the console. However, I am unable to save them to a file. It is only saving the header (page 2 of the response - which has only the header)

 


try
{
$api_url = $base_url + "/ws/search.asmx"
$ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
$page = 1
$xml_file = New-Object System.Xml.XmlDocument

Do
{

"Page " + $page
[xml] $xdoc = $ws.SearchRecordsByReport($session_token, $searchXml, $page)
if ($page -eq 1)
{
$xml_file = $xdoc
}

else
{
ForEach ($record in $xdoc.SelectNodes("/Records/Record"))
{
$records = $xml_file.SelectSingleNode("/Records")
$records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
}

}


$v= $xdoc.InnerXml
Write-Host "xml file = $v"
$v |Out-File "c:\test\Archer1.xml"
$page++
}
While ($xdoc.Records.Record.Count -gt 0)

}
catch
{
Error -exception $_.Exception
}

MadhusudhanRama
Contributor II

Hi Sergei

The code snippet 
{
return $xml_file.InnerXml
}
still contains multiple xml declarations, field descriptions, etc.