Skip to end of metadata
Go to start of metadata

Purpose

This is an example program which allows the user to specify a tenant and then pull in information for all SSPs (or a particular SSP) and output the enrollment codes associated with the SSP. This example is developed in C#. Best coding practices are not necessarily followed in order to allow for clarity in instruction of the use of the Afaria API. For more information about consuming the SAP Afaria API, please see the following starter guide:

SAP Afaria API 101 - Consuming the Afaria API Services and Creating a Proof of Concept Application

Overview

API Calls made:

  1. InitContext
  2. SetTenantIdContext
  3. GetSelfServicePortalConfigurations
  4. CloseContext

 

Example Program

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using ServerProperties_GetAllEnrollmentCodes.AfariaServerProperties;


namespace ServerProperties_GetAllEnrollmentCodes

{

    class Program

    {

        static void Main(string[] args)

        {

            string apiAddress = "127.0.0.1";

            string apiUserDomain = "afaria";

            string apiUserName = "afaria";

            string apiUserPass = "afaria";

            int tenantID = 0;


            string response = string.Empty;


            Console.Write("Enter the API Server address or hit enter for default: ");

            response = Console.ReadLine();

            if (!string.IsNullOrWhiteSpace(response))

            {

                apiAddress = response;

                Console.Write("Enter the API Service Account UserName or hit enter for default: ");

                response = Console.ReadLine();

                if (!string.IsNullOrWhiteSpace(response))

                {

                    apiUserName = response;

                    Console.Write("Enter the API Service Account User Domain or hit enter for default: ");

                    response = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(response))

                    {

                        apiUserDomain = response;

                    }


                    Console.Write("Enter the API Service Account User Password or hit enter for default: ");

                    response = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(response))

                    {

                        apiUserPass = response;

                    }

                }

            }


            //intialize the service client

            ServerPropertiesServiceClient svcServerProps = new ServerPropertiesServiceClient(

                            "NetTcpBinding_IServerPropertiesService"

                            , "net.tcp://" + apiAddress + ":7982/AfariaService/ServerProperties");

            svcServerProps.ClientCredentials.Windows.ClientCredential.Domain = apiUserDomain;

            svcServerProps.ClientCredentials.Windows.ClientCredential.UserName = apiUserName;

            svcServerProps.ClientCredentials.Windows.ClientCredential.Password = apiUserPass;

            svcServerProps.InitContext(Guid.NewGuid().ToString());


            Console.Write("Enter the Tenant ID(#) or hit enter for default: ");

            response = Console.ReadLine();

            if (!string.IsNullOrWhiteSpace(response))

            {

                int.TryParse(response, out tenantID);

            }

            //set the tenant ID which will be used from here for all calls

            svcServerProps.SetTenantIdContext(tenantID);


            try

            {

                //specify an invalid GUID, this will return all portal configurations. 

                //If a valid GUID for a portal is specified, only that portal's configuration will be returned

                SelfServicePortalConfiguration[] portalConfigs = svcServerProps.GetSelfServicePortalConfigurations(new Guid[] {Guid.NewGuid()});

                foreach(SelfServicePortalConfiguration portalConfig in portalConfigs)

                {

                    Console.WriteLine("---------------------------------\r\nPortal = " 

                            + portalConfig.Name 

                            + " - Tenant " 

                            + portalConfig.TenantId.ToString()

                            + "\r\n---------------------------------");

                    if(portalConfig.EnrollmentCodes != null)

                    {

                        foreach(SelfServicePortalEnrollmentCodeInfo code in portalConfig.EnrollmentCodes)

                        {

                            Console.WriteLine("ClientType=" + code.ClientType + " Code=" + code.EnrollmentCode);

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

                Console.WriteLine(ex.StackTrace);

                if (ex.InnerException != null)

                {

                    Console.WriteLine("Inner Exception\r\n----------------");

                    Console.WriteLine(ex.InnerException.Message);

                    Console.WriteLine(ex.InnerException.StackTrace);

                }

            }


            svcServerProps.CloseContext();

            svcServerProps.Close();


            Console.Write("\r\n\r\nPress any key to exit...");

            Console.ReadKey();

        }

    }

}

 

 

  • No labels