{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":336,"path":"api-reference/developer-guides/mobsdk-and-retrieve_attach_metadata.md","title":"Retrieve attachments and attachment metadata","bundle":null,"doc_type":null,"release":"australia","body":"\n# Retrieve attachments and attachment metadata\n\nThe NowAttachmentService API enables you to perform CRUD operations on attachments and retrieve attachment metadata from your ServiceNow instance.\n\n**Note:** The NowAttachment API works for guest users in the base ServiceNow system.\n\nUsing this API you can:\n\n-   Upload attachments to your ServiceNow instance and associated them to a specific record.\n-   Download one or more attachments.\n-   Delete attachments.\n-   Validate an attachment by comparing the computed hash of the attachment to the expected hash.\n-   Download attachment metadata. This metadata is generated by your ServiceNow instance when an attachment is uploaded.\n\nFor additional information on working with attachments, see [Attachment API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-apis/c_AttachmentAPI.md).\n\nThe following shows how to instantiate the NowAttachmentService:\n\n```\n/**\n * Helper class used to handle different Now service instances. \n * It has an application scope or is a Singleton.\n */\n@Singleton\nclass SdkManager @Inject constructor() {\n\n\n    private var nowAttachmentService: NowAttachmentService? = null\n\n    /**\n     * Create the NowAttachmentService once in the lifetime of the application \n     * inside the Application class or another manager class\n     * that will be injected into other classes via dagger/hilt.\n     * NowAttachmentService should be created after initializing the NowSDK\n     */\n    suspend fun getNowAttachmentService(): NowAttachmentService? {\n        if (nowAttachmentService != null) return nowAttachmentService\n\n        return NowDataSDK.makeAttachmentService(URL(\"https://instance-name.service-now.com\"))\n            .getOrThrow()\n            .also { this.nowAttachmentService = it }\n    }\n}\n```\n\nThe following shows how to obtain a [Call](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/CallAndroidInterface.md) object that fetches metadata for a particular attachments. It creates an executable object that is able to perform the specified request and return `Response<NowAttachmentMetadata>`.\n\n```\nsuspend fun fetchMetadata(sysId: String) {\n    val response = runCatching {\n        sdkManager.getNowAttachmentService()?.attachmentMetadata(sysId)?.execute()\n    }\n\n    if (response.isSuccess) {\n        val nowAttachmentMetadata = response.getOrNull()?.body\n    } else {\n        Log.e(\"NowSDK\", \"result not successful\")\n    }\n}\n```\n\nThe following shows how to obtain a [Call](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/CallAndroidInterface.md) object that fetches metadata for multiple attachments. It creates an executable object that is able to perform the specified request and return `Response< List<NowAttachmentMetadata>>`.\n\n```\nsuspend fun fetchMultipleMetadata() {\n    val filterQuery = \"content_type=text/plain\"\n    val limit = 5\n\n    val response = runCatching {\n        sdkManager.getNowAttachmentService()?.attachmentMetadata(Filter(filterQuery), limit)?.execute()\n    }\n\n    if (response.isSuccess) {\n        val attachmentMetadataList = response.getOrNull()?.body\n    } else {\n        Log.e(\"NowSDK\", \"result not successful\")\n    }\n}\n```\n\n## Attachment metadata pagination\n\nYou can use the NowAttachmentService methods to download attachment metadata for one or more attachments. When downloading metadata from multiple attachments, you may want to use the [NowAttachmentService - attachmentMetadataPaginator\\(filter: Filter? = null, limit: Int? = null\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowAttachServiceAndroidInterface.md) method, which returns a Paginator object that enables you to easily iterate over the potentially large amount of data that is returned.\n\nYou can then iterate through the returned metadata using Paginator methods and receiving the response in the observe\\(\\) callback.\n\n```\n suspend fun createAttachmentMetadataPaginator() {\n  val filterQuery: String = \"content_type=text/plain\"\n  val filter = filterQuery.let(::Filter)\n  val limit = 10\n\n  val paginator = sdkmanager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)\n    ?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {\n      override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {\n        // Handle response\n      }\n\n      override fun onFailure(e: NowDataError) {\n        // Handle error\n      }\n\n    })\n    ?: throw Exception(\"Response is null\")\n\n  // Use paginator operators to navigate. Example\n  while (paginator.hasNext() && !paginator.isBusy()) {\n    paginator.next()\n  }\n}\n```\n\nThe returned Paginator object provides the following methods that enable you to page through the returned records. You must first call the observe\\(\\) function to set the Paginator's callback.\n\n-   first\\(\\)\n-   hasNext\\(\\)\n-   hasPrevious\n-   isBusy\n-   last\\(\\)\n-   next\\(\\)\n-   observe\\(\\)\n-   previous\\(\\)\n-   reset\\(\\)\n\n**Note:** Some Paginator methods may throw an exception, such as when there are no more pages to fetch.\n\n"}}