For a more in depth and other programming language samples, download the Sample Client Install

C# Copy imageCopy
using System;
using System.Collections.Generic;
using System.Net;
using IWSConsoleSample.IWSServiceReference;

namespace IWSConsoleSample
{
    class RemoveInboundItemsByIDSampleClass
    {
        public static void RemoveInboudItemsByIDSample()
        {
            string username = "mbxXXXXXXXX";
            string password = "XXXX";

            ///////////////////////////////////////////////////////////
            ///OAuth 2.0 Authentication Start, 
            ///Comment this block out if you are using username and password authentication
            // Get an access token from Concord Identity Server

            string endPoint = "https://login.concord.net/v1/";
            string grantType = "password";
            string scope = "IWS";
            string IWSClientId = "ConcordIWSClient";
            string IWSClientSecret = "";

            AccessTokenRequest accessTokenRequest = null;
            AccessTokenResponse accessTokenResponse = null;
            TokenManager tokenManager = new TokenManager(endPoint);

            accessTokenRequest = new AccessTokenRequest(
                clientId: IWSClientId,
                clientSecret: IWSClientSecret,
                username: username,
                password: password,
                grantType: grantType,
                scope: scope,
                redirectUri: null,
                code: null,
                refreshToken: null,
                state: null);

            ///OAuth 2.0 Authentication End
            ///////////////////////////////////////////////////////////

            using (IWSAuthWrapper ws = new IWSAuthWrapper())
            {
                Authentication authentication = null;
                RemoveInboundItemsByIDRequest request = null;
                RemoveInboundItemsByIDResponse response = null;

                authentication = new Authentication();
                authentication.UserId = username;

                if (accessTokenResponse == null)
                {
                    Console.WriteLine("Failed to get an AccessToken, returned null");

                    return;
                }

                // Check to see if we failed to get an Access token via a Refresh token that has expired or been revoked, if so we need to re-authenticate
                if (accessTokenResponse.HttpStatusCode == HttpStatusCode.BadRequest && accessTokenResponse.Error == "invalid_grant")
                {
                    Console.WriteLine("Refresh token expired, attempting to get a new Access Token");
                    accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest, ignoreCache: true);
                }

                if (accessTokenResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription);

                    return;
                }

                // User Name and Password authentication
                 // authentication.Password = password;

                // OAuth 2.0 Authentication requires that you pass in an access token as part of the Authorization: header 
                // (No Password required)

                ws.AccessToken = accessTokenResponse.AccessToken;

                request = new RemoveInboundItemsByIDRequest();

                // Optional. Default is to archive messages once deleted.
                // To override set this property to True
                // request.DoNotArchiveItems;

                //Array of strings representing InboundItemId's to remove
                request.InboundItemIDs = new string[] { 
                    "ct12065551212-20120322150106062-341-5",
                    "ct12065551212-20120322150106023-342-3" 
                    };

                try
                {
                    response = ws.RemoveInboundItemsByID(authentication, request);

                    if(!response.Success)
                    {
                        bool requiresNewToken = false;

                        foreach (WSError error in response.Messages)
                        {
                            Console.WriteLine("Error deleting fax documents. Error Code: {0} Error Description: {1}", error.ErrorCode, error.ErrorDescription);

                            if (error.ErrorCode == -486 && error.ErrorDescription == "Invalid access token") requiresNewToken = true;
                        }

                        // Check to see if response indicates access token has expired, if so attempt to Refresh or Get New OAuth access token and try method call again
                        if (requiresNewToken)
                        {
                            Console.WriteLine("AccessToken is not valid");

                            // Attempt to get a new access token or use a refresh token, if we fail we should stop
                            accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest);

                            if (accessTokenResponse == null)
                            {
                                Console.WriteLine("Failed to get an AccessToken, returned null");

                                return;
                            }

                            if (accessTokenResponse.HttpStatusCode != HttpStatusCode.OK)
                            {
                                Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription);

                                return;
                            }
                        }

                        return;
                    }

                    if(response.FailureCount > 0)
                    {
                        if(response.FailureCount == request.InboundItemIDs.Length)
                        {
                            Console.WriteLine("All FaxDocuments requested to be deleted failed");
                        }
                        else
                        {
                            Console.WriteLine("One ore more FaxDocuments failed to be deleted");
                        }


                        List<string> failedItems = new List<string>();

                        failedItems.AddRange(response.FailedItemIDs);

                        foreach(string item in request.InboundItemIDs)
                        {
                            if(!failedItems.Contains(item))
                            {
                                Console.WriteLine("Successfully deleted FaxDocument with MessageId: {0}", item);
                            }
                        }

                        foreach(WSError wsError in response.Messages)
                        {
                            if(wsError.ErrorCode != 0)
                            {
                                Console.WriteLine("FaxDocument delete failed with Error Code: {0} Error Description: {1}", wsError.ErrorCode, wsError.ErrorDescription);
                            }
                        }

                    }
                    else
                    {
                        Console.WriteLine("Successfully deleted all FaxDocuments from server");

                        foreach(string item in request.InboundItemIDs)
                        {
                            Console.WriteLine("Successfully deleted FaxDocument with MessageId: {0}", item);
                        }
                    }

                }
                catch(Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                }

                Console.WriteLine("Finished deleting inbound items");
            }
        }
    }
}

See Also