> For the complete documentation index, see [llms.txt](https://docs.sharinpix.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sharinpix.com/documentation/features/search-images/sharinpix-aggregate-search-developer-oriented.md).

# SharinPix Aggregate Search (Developer-Oriented)

This article, we will see how to personalize the SharinPix search when querying images from a large number of records.

The following sections demonstrate how to use the SharinPix Album to perform a personalized aggregate search to display all the images present on the **Contact** records related to a particular **Account** record. This can be done by embedding the SharinPix component inside an **Iframe**.

Therefore, you will need:

* An [Apex Class Controller](#apex-controller) that will build the query and generate the token. In the Apex controller you will need to:
  * [build a token to initiate the SharinPix search](#build-a-token-to-initiate-the-sharinpix-search)
  * [split in group of 20 album IDs](#split-records-in-groups-of-20-album-ids)
  * [build the query string for each groups and generate the token for each](#build-query-strings-and-generate-tokens-for-each-groups)
* A [Visualforce Page](#visualforce-page) that will embed the Iframe

## Apex Controller

The Apex Class controller implementation can be found in the following sections.

### Build a token to initiate the SharinPix search

```apex
public String getSearchUrl() {
  loadParams = new Map<String, Object> {
    'path' => '/search?search_bar=false',
    'q' => '',
    'download' => true,
    'download_filename' => acc.Name,
    'download_filenames' => acc.Name
  };
  return clientInstance.token(loadParams);
}
```

### Split records in groups of 20 album IDs

```apex
public List<String> slice(List<String> original, Integer startIndex, Integer endIndex) {
  if (startIndex == 0 && endIndex == original.size()) {
  	return original;
  }
  List<String> sliced = new List<String>();
  for (Integer i = startIndex; i < endIndex && i < original.size(); i++) {
  	sliced.add(original[i]);
  }
  return sliced;
}
```

### Build query Strings and generate tokens for each groups

```apex
public List<Contact> cnt;
public Account acc;

public SharinPixAggregateSearch(ApexPages.StandardController stdCtrl) {
  Id accId = stdCtrl.getId();
  cnt = [SELECT Id FROM Contact WHERE AccountId = :accId];

  acc = [SELECT Name FROM Account WHERE Id = :accId];
  setParameters();
}

public void setParameters() {
	Map<String, Object> query = new Map<String, Object>();
	list<string> ids = new list<string>();
    for (Contact contact : cnt) {
      ids.add('"'+contact.Id+'"');
    }

    tokens = new List<String>();
    for (Integer i = 0; i < ids.size(); i += 20) {
      List<String> slicedIds = slice(ids, i, i + 20);
      if (slicedIds.size() == 0 || String.isBlank(slicedIds[0])) return '';
      String queryStr = '"' + String.join(albumIds, '" "') + '"';

      params = new Map<String, Object> {
        'path' => '/search?search_bar=false',
        'q' => queryStr,
        'download' => true,
        'download_filename' => acc.Name,
        'download_filenames' => acc.Name
      };

      tokens.add(clientInstance.token(params));
    }
}
```

## Visualforce Page

The code snippet below shows the implementation of the Visualforce Page used to display the search results on a SharinPix Album:

```html
<script>
  const eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
  const eventer = window[eventMethod];
  const messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
  var tokens = JSON.parse("{! JSENCODE(parameters) }");

  document.getElementById("sharinpixIframe").onload = function() {
  	this.contentWindow.postMessage({ token: "{! generateBaseToken }" }, 'https://app.sharinpix.com');
  }

  window.addEventListener('message', event => {
  	const { origin, data: { name, componentId } } = event
  
  	if (origin !== 'https://app.sharinpix.com') { return }
    if (name === 'search-ready') {
      for(i = 0; i < tokens.length; i++) {
        document.getElementById("sharinpixIframe").contentWindow.postMessage({ payload: tokens[i], name: 'search-aggregate' }, 'https://app.sharinpix.com');
      }
    }
  });
</script>
```

{% hint style="danger" %}
**Note:**

The SharinPix Aggregate Search is limited to 500 results.

If your search includes more than 500 images, it stops once the limit is reached and the extra results are not displayed.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sharinpix.com/documentation/features/search-images/sharinpix-aggregate-search-developer-oriented.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
