<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.aras.com/community/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Thomas Hotz さんのアクティビティ</title><link>https://www.aras.com/community/members/t_2d00_hotz</link><description>Thomas Hotz さんの最近のアクティビティ</description><dc:language>ja-JP</dc:language><generator>Telligent Community 12</generator><item><title>Search an item by id across all item types</title><link>https://www.aras.com/community/f/development/3102/search-an-item-by-id-across-all-item-types</link><pubDate>Tue, 06 Dec 2016 09:05:53 GMT</pubDate><guid isPermaLink="false">916d3f7e-8ddc-42f8-8d45-380822f51406:650f7d0f-fb50-474d-8b50-2336b2966ccc</guid><dc:creator>Thomas Hotz</dc:creator><description>I sometimes had the problem that I have an ID of an Item within Aras, but I don&amp;#39;t know the Item Type that it belongs to.
(for example, I find the id in an error log somewhere but it&amp;#39;s not clear where it came from.)

Since I don&amp;#39;t know any direct way of searching an item through all item types, I created an easy search util for that. It consists of a server-side method, a client-side method and a generic action that I can call from the main aras window.

This is what the code does:
- Ask the user to enter an 32-char ID.
- Search through all item types for an item that has this ID
- Show the result of the search: the ItemType and the keyed_name of the item that was found
- It also asks the user if he/she wants to open the aras tear-off window for that item.

Maybe this is helpful for other Aras users as well, so I post the code here (it&amp;#39;s the AML that you can use to import as a small package or through AML Studio).

Thomas


Server side method:
&lt;pre&gt;&lt;code&gt;&amp;lt;AML&amp;gt;
 &amp;lt;Item type=&amp;quot;Method&amp;quot; id=&amp;quot;6FBCE5A606B44A25AD805C1418B88388&amp;quot; action=&amp;quot;add&amp;quot;&amp;gt;
  &amp;lt;comments&amp;gt;Admin action to search for an ID in all ItemTypes&amp;lt;/comments&amp;gt;
  &amp;lt;execution_allowed_to keyed_name=&amp;quot;Administrators&amp;quot; type=&amp;quot;Identity&amp;quot;&amp;gt;2618D6F5A90949BAA7E920D1B04C7EE1&amp;lt;/execution_allowed_to&amp;gt;
  &amp;lt;method_code&amp;gt;&amp;lt;![CDATA[var inn = this.getInnovator();

// Validate input
var searchId = this.getProperty(&amp;quot;search_id&amp;quot;);
if (searchId == null)
{
    return inn.newError(&amp;quot;Please provide a search_id&amp;quot;);
}
if (searchId.Length != 32)
{
    return inn.newError(&amp;quot;search_id needs to be 32 characters.&amp;quot;);
}

// Search all available ItemTypes
var searchForItemTypes = inn.newItem(&amp;quot;ItemType&amp;quot;, &amp;quot;get&amp;quot;);
searchForItemTypes.setAttribute(&amp;quot;select&amp;quot;, &amp;quot;name,instance_data&amp;quot;);
searchForItemTypes.setAttribute(&amp;quot;where&amp;quot;, &amp;quot;implementation_type=&amp;#039;table&amp;#039;&amp;quot;); // ignore polymorphic and federated ItemTypes
var allItemTypes = searchForItemTypes.apply();

// In every ItemType, search for the given ID
for (int i = 0; i &amp;lt; allItemTypes.getItemCount(); i++)
{
    var itemType = allItemTypes.getItemByIndex(i);
    var itemTypeName = itemType.getProperty(&amp;quot;name&amp;quot;);
    var tableName = itemType.getProperty(&amp;quot;instance_data&amp;quot;);
    // Search via SQL to avoid any permission problems
    var sql = &amp;quot;select keyed_name from [&amp;quot; + tableName + &amp;quot;] where id=&amp;#039;&amp;quot; + searchId + &amp;quot;&amp;#039;&amp;quot;;
    var res = inn.applySQL(sql);

    // If one item is found, return the result.
    if (res.getItemCount() &amp;gt; 0)
    {
        res.setAttribute(&amp;quot;type&amp;quot;, itemTypeName);
        return res;
    }

}

// No ID found, return error message
return inn.newError(&amp;quot;ID &amp;quot; + searchId + &amp;quot; not found!&amp;quot;);]]&amp;gt;&amp;lt;/method_code&amp;gt;
  &amp;lt;method_type&amp;gt;C#&amp;lt;/method_type&amp;gt;
  &amp;lt;name&amp;gt;s_find_element_by_id_server&amp;lt;/name&amp;gt;
 &amp;lt;/Item&amp;gt;
&amp;lt;/AML&amp;gt;&lt;/code&gt;&lt;/pre&gt;


Client-side method:
&lt;pre&gt;&lt;code&gt;&amp;lt;AML&amp;gt;
 &amp;lt;Item type=&amp;quot;Method&amp;quot; id=&amp;quot;7CF952DCB88F4EC5A2376E320AFDD05E&amp;quot; action=&amp;quot;add&amp;quot;&amp;gt;
  &amp;lt;comments&amp;gt;Admin action to search for a given ID in any ItemType&amp;lt;/comments&amp;gt;
  &amp;lt;execution_allowed_to keyed_name=&amp;quot;Administrators&amp;quot; type=&amp;quot;Identity&amp;quot;&amp;gt;2618D6F5A90949BAA7E920D1B04C7EE1&amp;lt;/execution_allowed_to&amp;gt;
  &amp;lt;method_code&amp;gt;&amp;lt;![CDATA[var inn = this.getInnovator();
        

// Ask the user for an ID
top.aras.prompt(&amp;quot;Please enter ID:&amp;quot;, &amp;quot;&amp;quot;).then(
    
    // Callback function after the prompt
    function (search_id) {
    
        if (search_id===undefined) {
            // User canceled the dialog
        } else {
            
            // Check server for ID
            var r = inn.applyMethod(&amp;quot;s_find_element_by_id_server&amp;quot;,&amp;quot;&amp;lt;search_id&amp;gt;&amp;quot; + search_id + &amp;quot;&amp;lt;/search_id&amp;gt;&amp;quot;);
        
            // If server returned an error, show error to user
            if (r.isError()) {
                top.aras.AlertError(r.getErrorString());
            } else {
                
                // If server found an item, show the ItemType and Keyed name of the item,
                // and ask user if he wants to see the form of the item
                if (top.aras.confirm(&amp;quot;Search for ID: &amp;quot; + search_id + &amp;quot;\n\n&amp;quot; +
                    &amp;quot;ItemType: &amp;quot; + r.getType() +&amp;quot;\n&amp;quot; + 
                    &amp;quot;Keyed name: &amp;quot; + r.getProperty(&amp;quot;keyed_name&amp;quot;) + &amp;quot;\n\n&amp;quot;+
                    &amp;quot;Do you want to open the form of the item?&amp;quot;)) {
            
                    // open form of item, if requested
                    top.aras.uiShowItem(r.getType(), search_id);
                }
            }
        }    
    }
);
]]&amp;gt;&amp;lt;/method_code&amp;gt;
  &amp;lt;method_type&amp;gt;JavaScript&amp;lt;/method_type&amp;gt;
  &amp;lt;name&amp;gt;s_find_element_by_id_client&amp;lt;/name&amp;gt;
 &amp;lt;/Item&amp;gt;
&amp;lt;/AML&amp;gt;&lt;/code&gt;&lt;/pre&gt;


Action:
&lt;pre&gt;&lt;code&gt;&amp;lt;AML&amp;gt;
 &amp;lt;Item type=&amp;quot;Action&amp;quot; id=&amp;quot;BAFCB980FF014963B7A6D394774F0D08&amp;quot; action=&amp;quot;add&amp;quot;&amp;gt;
  &amp;lt;item_query /&amp;gt;
  &amp;lt;label xml:lang=&amp;quot;en&amp;quot;&amp;gt;Find Element By ID&amp;lt;/label&amp;gt;
  &amp;lt;location&amp;gt;client&amp;lt;/location&amp;gt;
  &amp;lt;method keyed_name=&amp;quot;s_find_element_by_id_client&amp;quot; type=&amp;quot;Method&amp;quot;&amp;gt;
   &amp;lt;Item type=&amp;quot;Method&amp;quot; action=&amp;quot;get&amp;quot; select=&amp;quot;id&amp;quot;&amp;gt;
    &amp;lt;name&amp;gt;s_find_element_by_id_client&amp;lt;/name&amp;gt;
   &amp;lt;/Item&amp;gt;
  &amp;lt;/method&amp;gt;
  &amp;lt;target&amp;gt;none&amp;lt;/target&amp;gt;
  &amp;lt;type&amp;gt;generic&amp;lt;/type&amp;gt;
  &amp;lt;name&amp;gt;s_find_element_by_id&amp;lt;/name&amp;gt;
 &amp;lt;/Item&amp;gt;
&amp;lt;/AML&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>