<p>I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going:</p>

<ul>
<li>The classname of the tool i.e <code>AssetIndex</code></li>
<li>An array of asset sources or <code>*</code> for them all</li>
<li>An indexing session id</li>
<li>An array of parameters to send to the tool</li>
</ul>

<p>So I managed to get something working with the following code (this would go in your plugin controller)</p>

<pre><code>public function actionUpdateIndex()
{
  // Get the AssetIndex tool
  $tool = craft()-&gt;components-&gt;getComponentByTypeAndClass(ComponentType::Tool, 'AssetIndex');

  // Generate an AssetIndex session id for us to use
  $sessionId = craft()-&gt;assetIndexing-&gt;getIndexingSessionId();

  // Set up some params
  $params = array(
    'start' =&gt; '1', // Tells the tool this is the initial request
    'sources' =&gt; '*', // All of them or [1,2,3] just ids1
  );

  // Get an initial response from the tool, will contain an array containing the assets to index
  $response = $tool-&gt;performAction($params);

  // See [1]
  foreach($response['batches'] as $key =&gt; $value)
  {
    foreach($value as $key =&gt; $batch)
    {
       // All the needed params are set in the array to do the index
       $tool-&gt;performAction($batch['params']);
    }
  }

}
</code></pre>

<p>Each time you call <code>$tool-&gt;performAction($batch['params']);</code> you will an array back, something like:</p>

<pre><code>Array
(
 [success] =&gt; 1
)
</code></pre>

<hr>

<p><strong>[1]</strong> Because this is the initial request craft will return an array with a <code>batches</code> index, which we then have to do a few loops through, it looks something like this:</p>

<pre><code>Array
(
 [batches] =&gt; Array
  (
   [0] =&gt; Array
    (
     [0] =&gt; Array
      (
       [params] =&gt; Array
        (
         [sessionId] =&gt; 6d08c5ba-169f-4b99-a7bd-cdc26c9428c7
         [sourceId] =&gt; 1
         [total] =&gt; 12
         [offset] =&gt; 0
         [process] =&gt; 1
        )

       )

      )

    )

 [total] =&gt; 0
)
</code></pre>

<p>Not sure if this is the absolute best way to do it, I was learning as I was going, but this worked for me so hopefully it'll work for you too :)</p>

CC BY-SA 3.0
2015-08-16T12:55:09.730Z
Ouah thank you so much Alec. OK I tried but got a bug. Perhaps I am missing something here : `foreach($response['batches'] as $key => $value)`. From where do we get the array `$response['batches']` first time?
migmaker
CC BY-SA 3.0
2015-08-16T12:57:56.820Z
Oh dang, I missed out a piece of the puzzle, i've updated the answer, just below where the `$params` are set
Alec Ritson
CC BY-SA 3.0
2015-08-16T13:08:03.837Z
Awesome :) You are the boss it works fantastic! Just one last detail: its adding the new files in the index but it is not deleting the removed files (related to the popup that appear to display missing files and ask confirmation to remove in the Craft Settings). There should be a param to provide to `performAction` no? (`$params['deleteFile']` ??)
migmaker
CC BY-SA 3.0
2015-08-16T13:40:13.210Z
No worries! Yeah it looks that way, but an issue seems to be that the tool expects a template `assets/_missing_items` to be present and then from that somehow craft adds the `deleteFile` and `deleteFolder` indexes to the array. Sorry but I'm not too sure how to handle that at the moment...
Alec Ritson
CC BY-SA 3.0
2015-08-16T13:46:58.767Z
OK thx that already great. I think I can be ok with files not deleted in my case. Thx for your time and help.
migmaker
CC BY-SA 3.0
2015-09-12T05:24:06.140Z
Hi Alec. I got an urgent problem to solve : the reindexing is way too long for my client. Is there a way to filter in `params` the assets files by date of creation ?
migmaker
CC BY-SA 3.0
2015-09-12T13:45:36.363Z
Hey @migswd, from a look at the tools code it doesn't appear that you can, if you are worried about the page 'hanging' whilst this process is happening, you could always run it as a task, that way the request will end on the client side and the indexing will happen in the background. See this question about creating tasks: http://craftcms.stackexchange.com/questions/8399/documentation-on-creating-tasks
Alec Ritson
CC BY-SA 3.0
2015-09-13T19:16:34.463Z
In fact I got an error 500 in my task : there is a bad image file that is broken the indexing. Do you know how to bypass any error ? Then I tried to filter `sources` but it did not work. Did you succeed to filter on sources ? Thx again...
migmaker
CC BY-SA 3.0
2015-09-14T13:04:24.207Z
With the risk of this thread becoming too long, is this something you could post as a new question? Or feel free to reach me on the Craft slack!
Alec Ritson
CC BY-SA 3.0
2015-09-15T13:33:03.347Z
Finally ! We found a better solution... I am going to add it here.
migmaker
CC BY-SA 3.0
2015-08-16T07:58:07.193Z
2015-08-16T12:57:18.283Z
2015-08-16T12:57:18.283Z
Alec Ritson
<p>I am making a dashboard widget to trigger the assets indexing action.
I got the controller ready but I do not understand then how to trigger the action.</p>

<p>Should I go with <code>AssetIndexTool</code>? Then <code>performAction()</code>?<br>
Or using <code>ToolsController</code> ?</p>

<p>I am really lost. Thanks for help!  </p>

<assets><controller><reindex>
Assets indexing from plugin controller
# Assets indexing from plugin controller

(Asked by: username_0 on 2015-08-15)

I am making a dashboard widget to trigger the assets indexing action.
I got the controller ready but I do not understand then how to trigger the action.

Should I go with `AssetIndexTool`? Then `performAction()`?  
Or using `ToolsController` ?

I am really lost. Thanks for help!

**Tags:** assets, controller, reindex

---

## 1 answers

### Answer by username_1 on 2015-08-16. Score: 5 | Accepted answer

I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going:

The classname of the tool i.e AssetIndex
An array of asset sources or * for them all
An indexing session id
An array of parameters to send to the tool

So I managed to get something working with the following code (this would go in your plugin controller)
public function actionUpdateIndex()
{
  // Get the AssetIndex tool
  $tool = craft()->components->getComponentByTypeAndClass(ComponentType::Tool, 'AssetIndex');

  // Generate an AssetIndex session id for us to use
  $sessionId = craft()->assetIndexing->getIndexingSessionId();

  // Set up some params
  $params = array(
    'start' => '1', // Tells the tool this is the initial request
    'sources' => '*', // All of them or [1,2,3] just ids1
  );

  // Get an initial response from the tool, will contain an array containing the assets to index
  $response = $tool->performAction($params);

  // See [1]
  foreach($response['batches'] as $key => $value)
  {
    foreach($value as $key => $batch)
    {
       // All the needed params are set in the array to do the index
       $tool->performAction($batch['params']);
    }
  }

}

Each time you call $tool->performAction($batch['params']); you will an array back, something like:
Array
(
 [success] => 1
)


[1] Because this is the initial request craft will return an array with a batches index, which we then have to do a few loops through, it looks something like this:
Array
(
 [batches] => Array
  (
   [0] => Array
    (
     [0] => Array
      (
       [params] => Array
        (
         [sessionId] => 6d08c5ba-169f-4b99-a7bd-cdc26c9428c7
         [sourceId] => 1
         [total] => 12
         [offset] => 0
         [process] => 1
        )

       )

      )

    )

 [total] => 0
)

Not sure if this is the absolute best way to do it, I was learning as I was going, but this worked for me so hopefully it'll work for you too :)

> **Comments:**
> **username_0**: Ouah thank you so much Alec. OK I tried but got a bug. Perhaps I am missing something here : `foreach($response['batches'] as $key => $value)`. From where do we get the array `$response['batches']` first time?  
> **username_1**: Oh dang, I missed out a piece of the puzzle, i've updated the answer, just below where the `$params` are set  
> **username_0**: Awesome :) You are the boss it works fantastic! Just one last detail: its adding the new files in the index but it is not deleting the removed files (related to the popup that appear to display missing files and ask confirmation to remove in the Craft Settings). There should be a param to provide to `performAction` no? (`$params['deleteFile']` ??)  
> **username_1**: No worries! Yeah it looks that way, but an issue seems to be that the tool expects a template `assets/_missing_items` to be present and then from that somehow craft adds the `deleteFile` and `deleteFolder` indexes to the array. Sorry but I'm not too sure how to handle that at the moment...  
> **username_0**: OK thx that already great. I think I can be ok with files not deleted in my case. Thx for your time and help.  
> **username_0**: Hi Alec. I got an urgent problem to solve : the reindexing is way too long for my client. Is there a way to filter in `params` the assets files by date of creation ?  
> **username_1**: Hey @migswd, from a look at the tools code it doesn't appear that you can, if you are worried about the page 'hanging' whilst this process is happening, you could always run it as a task, that way the request will end on the client side and the indexing will happen in the background. See this question about creating tasks: http://craftcms.stackexchange.com/questions/8399/documentation-on-creating-tasks  
> **username_0**: In fact I got an error 500 in my task : there is a bad image file that is broken the indexing. Do you know how to bypass any error ? Then I tried to filter `sources` but it did not work. Did you succeed to filter on sources ? Thx again...  
> **username_1**: With the risk of this thread becoming too long, is this something you could post as a new question? Or feel free to reach me on the Craft slack!  
> **username_0**: Finally ! We found a better solution... I am going to add it here.  

---

