Citrix Virtual Apps and Desktops REST APIs

How to perform an advanced search for sessions in Citrix Virtual Apps and Desktops

Use REST APIs to perform an advanced search on sessions in your Citrix Virtual Apps and Desktops site.

Follow the prerequisites and examples to get started with this API.

You can make API requests using the PowerShell code, C# code, Python, or any tool that supports invoking the REST API.

Prerequisites to perform an advanced search for sessions

Perform an advanced search for sessions using any REST API tool

Learn from the following example to search for sessions belonging to a specific user using any REST API tool. Other useful cases may include:

  • List all sessions launched via a published application
  • List all sessions launched within a certain time span
  • List all sessions currently in a certain state, for example, disconnected state

For how to perform various searches and sort results, see the API specification.

Request

POST https://[DdcServerAddress]/cvad/manage/Sessions/$search HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Authorization: CWSAuth bearer=<token-from-prerequisites>
Citrix-CustomerId: loy6oujtu6a4
Citrix-InstanceId: 22ded57c-0306-47e4-b6e8-fed6252759e1

{
    "SearchFilters": [
        {
            "Property": "CurrentUser",
            "Value": "HTLabs04\\DevUser1",
            "Operator": "Equals"
        }
    ]
}
<!--NeedCopy-->

Response

HTTP/1.1 200 OK
citrix-transactionid: ce0d3943-e294-4fc8-b2c6-85271d4a0c3b
content-Length: 8320
content-Type: application/json; charset=utf-8
date: "Tue, 08 Dec 2020 11:34:57 GMT"
Server: Citrix Systems, Inc.

{
    "Items": [
        {
            "Id": "41ba32e9-d12b-485c-afd2-c4d0d047cd8b",
            "Uid": 8,
            "ApplicationsInUse": [
                {
                    "Id": "00000000-0000-0000-0000-000000000000",
                    "Uid": null,
                    "Name": "WorkingApp_1"
                }
            ],
            "AppState": "Active",
            "AppStateLastChangeTime": "11/16/2020 2:01:04 PM",
            "Brokering": {
                "AutonomouslyBrokered": false,
                "DurationMilliseconds": null,
                "Time": null,
                "UserName": null,
                "UserSid": null
            },
            "Client": {
                "DeviceId": null,
                "HardwareId": "55667788",
                "IPAddress": "10.107.1.120",
                "Name": "",
                "Platform": "Unknown",
                "ProductId": 9,
                "ReceiverIPAddress": null,
                "ReceiverName": null,
                "Version": "v1.0"
            },
            "Connection": {
                "ConnectedViaHostName": null,
                "ConnectedViaIP": "10.107.1.34",
                "ConnectionMode": "Brokered",
                "LaunchedViaHostName": null,
                "LaunchedViaIP": "20.62.252.155",
                "Protocol": "Hdx",
                "SecureIcaActive": false,
                "SmartAccessTags": []
            },
            "ContainerScopes": [
                {
                    "Scopes": [
                        {
                            "Id": "00000000-0000-0000-0000-000000000000",
                            "Uid": null,
                            "Name": "All",
                            "Description": null,
                            "IsBuiltIn": true,
                            "IsAllScope": true,
                            "IsTenantScope": false,
                            "TenantId": null,
                            "TenantName": null
                        }
                    ],
                    "ScopeType": "MachineCatalog"
                },
                {
                    "Scopes": [
                        {
                            "Id": "00000000-0000-0000-0000-000000000000",
                            "Uid": null,
                            "Name": "All",
                            "Description": null,
                            "IsBuiltIn": true,
                            "IsAllScope": true,
                            "IsTenantScope": false,
                            "TenantId": null,
                            "TenantName": null
                        }
                    ],
                    "ScopeType": "DeliveryGroup"
                }
            ],
            "EstablishmentDurationMilliseconds": 22260,
            "EstablishmentTime": "11/16/2020 2:01:04 PM",
            "IsAnonymousUser": false,
            "IsHidden": false,
            "LogoffInProgress": false,
            "LogonInProgress": false,
            "Machine": {
                "Id": null,
                "Uid": 15,
                "AgentVersion": "1912.0.1000.24525",
                "AllocationType": "Random",
                "ApplicationsInUse": null,
                "AssociatedUsers": null,
                "MachineCatalog": {
                    "Id": "0520987c-0312-4e1c-bd09-7714fc8732ec",
                    "Uid": 2,
                    "Name": "Sales-B"
                },
                "ContainerScopes": null,
                "ControllerDnsName": null,
                "DeliveryGroup": {
                    "Id": "5c796610-cb80-40ae-8378-5cbd5cefbd89",
                    "Uid": 2,
                    "Name": "Sales"
                },
                "DeliveryType": null,
                "Description": null,
                "DesktopConditions": null,
                "DnsName": "Sales-B03.htlabs04.example.com",
                "Hosting": {
                    "HostedMachineId": null,
                    "HostedMachineName": null,
                    "HostingServerName": null,
                    "LastHostingUpdateTime": null,
                    "HypervisorConnection": {
                        "Id": "00000000-0000-0000-0000-000000000000",
                        "Uid": null,
                        "Name": "SalesVda"
                    },
                    "ImageOutOfDate": false
                },
                "InMaintenanceMode": false,
                "IPAddress": "10.107.128.208",
                "IsAssigned": null,
                "MachineType": "Virtual",
                "LastConnectionFailure": null,
                "LastConnectionTime": null,
                "LastConnectionUser": null,
                "LastDeregistrationReason": null,
                "LastDeregistrationTime": null,
                "LastErrorReason": null,
                "LastErrorTime": null,
                "LoadIndex": null,
                "Name": "HTLABS04\\Sales-B03",
                "OSType": "Windows 2019",
                "OSVersion": null,
                "PersistUserChanges": "Discard",
                "PowerActionPending": null,
                "PowerState": "Unknown",
                "ProvisioningType": "MCS",
                "PublishedApplications": null,
                "RegistrationState": null,
                "ScheduledReboot": "Unknown",
                "SessionClientAddress": null,
                "SessionClientName": null,
                "SessionClientVersion": null,
                "SessionConnectedViaHostName": null,
                "SessionConnectedViaIP": null,
                "SessionCount": null,
                "SessionLaunchedViaHostName": null,
                "SessionLaunchedViaIP": null,
                "SessionProtocol": null,
                "SessionSecureIcaActive": null,
                "SessionSmartAccessTags": null,
                "SessionStartTime": null,
                "SessionState": null,
                "SessionStateChangeTime": null,
                "SessionSupport": "SingleSession",
                "SessionUserName": null,
                "Sid": null,
                "SummaryState": "Disconnected",
                "WillShutdownAfterUse": null,
                "WindowsConnectionSetting": null,
                "Zone": {
                    "Id": "1d405094-3abe-4ab9-b304-b21522bcd3e6",
                    "Uid": null,
                    "Name": "htlabs04-EastUs"
                },
                "SupportedPowerActions": null,
                "FaultState": "Unknown"
            },
            "SessionType": "Application",
            "StartTime": "11/16/2020 2:01:04 PM",
            "State": "Disconnected",
            "StateChangeTime": "11/16/2020 2:02:49 PM",
            "UntrustedUserName": "HTLABS04\\DevUser1",
            "User": {
                "DisplayName": "DevUser1",
                "SamName": null,
                "SamAccountName": null,
                "CommonName": null,
                "PrincipalName": "DevUser1@htlabs04.example.com",
                "Sid": "S-1-5-21-886277602-2428626936-2556752729-2162",
                "GroupSids": null,
                "DenyOnlySids": null,
                "PasswordCanExpire": null,
                "DaysUntilPasswordExpiry": null,
                "IsBuiltIn": null,
                "IsGroup": null,
                "Name": "HTLABS04\\DevUser1",
                "StreetAddress": null,
                "City": null,
                "State": null,
                "Country": null,
                "Mail": null,
                "TelephoneNumber": null,
                "HomePhone": null,
                "Mobile": null,
                "Enabled": null,
                "Locked": null,
                "PropertiesFetched": 0,
                "Domain": null,
                "Forest": null,
                "Guid": null,
                "DistinguishedName": null,
                "CanonicalName": null,
                "PossibleLookupFailure": false,
                "DirectoryServer": null
            }
        }
    ],
    "ContinuationToken": null,
    "TotalItems": 1
}
<!--NeedCopy-->

Perform an advanced search for sessions using PowerShell

Learn from the following example to search for sessions belonging to a specific user using any PowerShell code.

function SearchSessionsByUser {
    param (
        [Parameter(Mandatory=$true)]
        [string] $customerid,
        [Parameter(Mandatory=$true)]
        [string] $siteid,
        [Parameter(Mandatory=$true)]
        [string] $bearerToken,
        [Parameter(Mandatory=$true)]
        [string] $body
    )
    $requestUri = "https://[DdcServerAddress]/cvad/manage/Sessions/`$search"
    $headers = @{
        "Accept" = "application/json";
        "Authorization" = "CWSAuth Bearer=$bearerToken";
        "Citrix-CustomerId" = $customerid;
        "Citrix-InstanceId" = $siteid;
        "Content-Type" = "application/json";
    }

    $response = Invoke-RestMethod -Uri $requestUri -Method POST -Headers $headers -Body $body
    return $response
}

$customerId = "customer1"
$siteId = "61603f15-cdf9-4c7f-99ff-91636601a795"
$bearerToken = "ey1.."
$body = @"
{
    "SearchFilters": [
        {
            "Property": "CurrentUser",
            "Value": "HTLabs04\\DevUser1",
            "Operator": "Equals"
        }
    ]
}
"@
$response = SearchSessionsByUser $customerid $siteid $bearerToken $body
<!--NeedCopy-->

Perform an advanced search for sessions using C# code

Learn from the following example to search for sessions belonging to a specific user using any C# code.

public static async Task<string> SearchSessionsByUser(
    string customerid,
    string siteid,
    string bearerToken,
    MachineAndSessionSearchRequestModel model)
{
    var requestUri = "https://[DdcServerAddress]/cvad/manage/Sessions/$search";
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
        client.DefaultRequestHeaders.Add("Citrix-CustomerId", customerid);
        client.DefaultRequestHeaders.Add("Citrix-InstanceId", siteid);
        client.DefaultRequestHeaders.Add("Content-Type", "application/json");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("CWSAuth Bearer=" + bearerToken);

        var jsonBody = JsonConvert.SerializeObject(model, new JsonSerializerSettings
        {
            Converters = new JsonConverter[] { new StringEnumConverter() }
        });

        var response = await client.PostAsync(requestUri, new StringContent(jsonBody, Encoding.UTF8, "application/json"));

        if (response != null)
        {
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }

        return null;
    }
}
<!--NeedCopy-->

Perform an advanced search for sessions using Python

Learn from the following example to search for sessions belonging to a specific user using Python.

import requests 

def SearchSessionsByUser(bearerToken, customerid, siteid, username):
    request_uri = "https://[DdcServerAddress]/cvad/manage/Sessions/$search"
    headers = {
                'Authorization': 'CWSAuth Bearer=%s' % bearerToken,
                'Citrix-CustomerId': customerid,
                'Citrix-InstanceId': siteid,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
              }
    payload = json.dumps({
        "SearchFilters": [
            {
                "Property": "CurrentUser",
                "Value": username,
                "Operator": "Equals"
            }
        ] 
    })

    response = requests.post(request_uri, headers = headers, verify = False, data = payload)

    return response.json()
<!--NeedCopy-->
Resources
Citrix Virtual Apps and Desktops REST APIs OpenAPI Specification
Copy Download
How to perform an advanced search for sessions in Citrix Virtual Apps and Desktops