---
sourceDocument: Xanadu API リファレンス
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu API リファレンス

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# 添付ファイルと添付ファイルメタデータを取得

# 添付ファイルと添付ファイルメタデータを取得 {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：6分

NowAttachmentService API を使用すると、添付ファイルに対して CRUD 操作を実行し、ServiceNow インスタンスから添付ファイルのメタデータを取得できます。
注:  
NowAttachment API は、ベース ServiceNow システムのゲストユーザーに対して機能します。  
この API を使用すると、次のことができます。

* 添付ファイルを ServiceNow インスタンスにアップロードし、特定のレコードに関連付けます。
* 1 つ以上の添付ファイルをダウンロードします。
* 添付ファイルを削除します。
* 添付ファイルの計算されたハッシュを予想されるハッシュと比較して、添付ファイルを検証します。
* 添付ファイルのメタデータをダウンロードします。このメタデータは、添付ファイルがアップロードされたときに ServiceNow インスタンスによって生成されます。
{#mobsdk-and-retrieve_attach_metadata__ul_l5y_qjw_3qb}

添付ファイルの操作の詳細については、「 [添付ファイル API](https://servicenow-prod.fluidtopics.net/wBetsecnSLzsO1otMGyceA#c_AttachmentAPI "添付ファイル API は、添付ファイルのアップロードおよびクエリを可能にするエンドポイントを提供します。")」を参照してください。  
NowAttachmentService をインスタンス化する方法を次に示します。

    /**
     * Helper class used to handle different Now service instances. 
     * It has an application scope or is a Singleton.
     */
    @Singleton
    class SdkManager @Inject constructor() {


        private var nowAttachmentService: NowAttachmentService? = null

        /**
         * Create the NowAttachmentService once in the lifetime of the application 
         * inside the Application class or another manager class
         * that will be injected into other classes via dagger/hilt.
         * NowAttachmentService should be created after initializing the NowSDK
         */
        suspend fun getNowAttachmentService(): NowAttachmentService? {
            if (nowAttachmentService != null) return nowAttachmentService

            return NowDataSDK.makeAttachmentService(URL("https://instance-name.service-now.com"))
                .getOrThrow()
                .also { this.nowAttachmentService = it }
        }
    }

次に、特定の添付ファイルのメタデータを取得する [Call](https://servicenow-prod.fluidtopics.net/B9yfp0bhp_9Z2cEsUjZEIA#CallAndroidInterface "Call インターフェイスは、処理の準備が整った要求を表します。") オブジェクトを取得する方法を示します。指定された要求を実行し、 `Response<NowAttachmentMetadata>`を返すことができる実行可能オブジェクトを作成します。  

    suspend fun fetchMetadata(sysId: String) {
        val response = runCatching {
            sdkManager.getNowAttachmentService()?.attachmentMetadata(sysId)?.execute()
        }

        if (response.isSuccess) {
            val nowAttachmentMetadata = response.getOrNull()?.body
        } else {
            Log.e("NowSDK", "result not successful")
        }
    }

複数の添付ファイルのメタデータを取得する [Callオブジェクトを取得する](https://servicenow-prod.fluidtopics.net/B9yfp0bhp_9Z2cEsUjZEIA#CallAndroidInterface "Call インターフェイスは、処理の準備が整った要求を表します。") 方法は次のとおりです。指定された要求を実行し、 `Response< List<NowAttachmentMetadata>>`を返すことができる実行可能オブジェクトを作成します。  

    suspend fun fetchMultipleMetadata() {
        val filterQuery = "content_type=text/plain"
        val limit = 5

        val response = runCatching {
            sdkManager.getNowAttachmentService()?.attachmentMetadata(Filter(filterQuery), limit)?.execute()
        }

        if (response.isSuccess) {
            val attachmentMetadataList = response.getOrNull()?.body
        } else {
            Log.e("NowSDK", "result not successful")
        }
    }

## 添付ファイルメタデータのページネーション {#mobsdk-and-retrieve_attach_metadata__section_stt_34w_3qb}

NowAttachmentService メソッドを使用して、1 つ以上の添付ファイルの添付ファイルメタデータをダウンロードできます。複数の添付ファイルからメタデータをダウンロードする場合は、 [NowAttachmentService - attachmentMetadataPaginator(filter: Filter? = null, limit: Int? = null)](https://servicenow-prod.fluidtopics.net/lCK8t4BdEItAwQ_OIGEebA#NAttServ-attachMetadataPaginat_S_I "指定された基準を満たすすべての添付ファイルのメタデータを取得し、返されたメタデータのページを反復処理するためのページネーターを作成します。") メソッドを使用して、返される可能性のある大量のデータを簡単に反復処理できる Paginator オブジェクトを返すことができます。  
その後、Paginatorメソッドを使用して返されたメタデータを反復処理し、 observe() コールバックで応答を受け取ることができます。

     suspend fun createAttachmentMetadataPaginator() {
      val filterQuery: String = "content_type=text/plain"
      val filter = filterQuery.let(::Filter)
      val limit = 10

      val paginator = sdkmanager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)
        ?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {
          override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {
            // Handle response
          }

          override fun onFailure(e: NowDataError) {
            // Handle error
          }

        })
        ?: throw Exception("Response is null")

      // Use paginator operators to navigate. Example
      while (paginator.hasNext() && !paginator.isBusy()) {
        paginator.next()
      }
    }

返された Paginator オブジェクトは、返されたレコードをページングできるようにする次のメソッドを提供します。Paginatorのコールバックを設定するには、まず observe() 関数を呼び出す必要があります。

* first()
* hasNext()
* hasPrevious
* はビジー
* last()
* next()
* オブザーブ()
* previous()
* reset()

{#mobsdk-and-retrieve_attach_metadata__ul_pyb_2w3_3qb}  
注:  
一部の Paginator メソッドは、フェッチするページがなくなった場合など、例外をスローする場合があります。

