diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 9bce226ada19255c730da56c6e66084f125d0792..bf762627f684eee2ee25a5c45366c6714becbdc8 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -261,60 +261,47 @@ sub req_select_sp {
         my $spec = $self->{configuration}->{$id};
         if ($spec->{type} eq 'metadata') {
 
-            my $metadata;
-            eval {
-                $metadata = AccountManager::Metadata->new(
-                    file => $spec->{file}
-                );
-            };
-            $self->abort(
-                log  => "Failed to load federation metadata: $EVAL_ERROR",
-                user => "internal"
-            ) if $EVAL_ERROR;
-
-            my $entities = $metadata->parse(type => 'sp');
+            my $entities = AccountManager::Entity->get_entities(
+                db => $self->{db},
+                query => [
+                    type        => { eq   => 'sp' },
+                    federations => { like => "%\"$id\"%"  }
+                ],
+                debug => 1
+            );
             push @groups, {
                 id       => $id,
                 label    => $spec->{label},
                 type     => 'list',
-                entities => [
-                    map { {
-                        id         => $_->{entityid},
-                        name       => $_->{display_name},
-                        federation => $id
-                     } } @$entities
-                ]
+                entities => $entities
             };
-
-            # if user is authenticated, and its IdP is found in metadata,
-            # push all entities with the same organization URL in a list
-            if ($ENV{HTTP_SHIB_IDENTITY_PROVIDER}) {
-                my $idps = $metadata->parse(id => $ENV{HTTP_SHIB_IDENTITY_PROVIDER});
-                my $idp  = $idps->[0];
-                if ($idp) {
-                    my $organization = $idp->{organization};
-                    $self->{logger}->debugf(
-                        "idp %s found in federation %s metadata with organization %s",
-                        $ENV{HTTP_SHIB_IDENTITY_PROVIDER},
-                        $id,
-                        $organization
-                    );
-                    push @organization_entities,
-                        map { {
-                            id         => $_->{entityid},
-                            name       => $_->{display_name},
-                            federation => $id
-                        } }
-                        grep { $_->{organization} eq $organization }
-                        @$entities;
-                }
-            }
         } elsif ($spec->{type} eq 'organization') {
+            next unless $ENV{HTTP_SHIB_IDENTITY_PROVIDER};
+
+            my $idps = AccountManager::Entity->get_entities(
+                db    => $self->{db},
+                query => [
+                    type     => 'idp',
+                    entityid => $ENV{HTTP_SHIB_IDENTITY_PROVIDER},
+                ]
+            );
+            my $idp = $idps->[0];
+
+            next unless $idp;
+
+            my $entities = AccountManager::Entity->get_entities(
+                db    => $self->{db},
+                query => [
+                    type             => 'sp',
+                    organization_url => $idp->organization_url()
+                ]
+            );
+
             push @groups, {
                 id       => $id,
                 label    => $spec->{label},
                 type     => 'list',
-                entities => \@organization_entities,
+                entities => $entities
             };
         } elsif ($spec->{type} eq 'link') {
             push @groups, {
@@ -342,52 +329,16 @@ sub req_select_email {
     $self->check_authentication(action => 'select_email')
         if $self->{configuration}->{app}->{login_url};
 
-    my $entityid   = $self->get_parameter(name => 'entityid');
-    my $federation = $self->get_parameter(name => 'federation');
+    my $entityid = $self->get_parameter(name => 'entityid');
 
-    # Create a persistent service provider object
     my $sp = AccountManager::Entity->new(
         db       => $self->{db},
         entityid => $entityid
     );
-
-    if ($sp->load(speculative => 1)) {
-        # already present in DB, nothing todo
-    } else {
-        # extract information from metadata
-        my $file = $self->get_metadata_file(federation => $federation);
-        my $metadata;
-
-        eval {
-            $metadata = AccountManager::Metadata->new(
-                file => $file
-            );
-        };
-        $self->abort(
-            log  => "Failed to load federation metadata: $EVAL_ERROR",
-            user => "internal"
-        ) if $EVAL_ERROR;
-
-        my $entities = $metadata->parse(id => $entityid);
-        my $entity = $entities->[0];
-
-        $self->abort(
-            log  => "No such SP $entityid in metadata",
-            user => "no_such_entity"
-        ) if !$entity;
-
-        # complete persistent object
-        $sp->displayname($entity->{display_name});
-        $sp->url($entity->{url});
-        $sp->contacts(uniq map { $_->{EmailAddress} } @{$entity->{contacts}})
-            if $entity->{contacts};
-
-        # save in DB
-        $self->abort(
-            log  => "Failed to save service provider",
-            user => "internal"
-        ) if !$sp->save();
-    }
+    $self->abort(
+        log  => sprintf("No such SP '%s' in database", $entityid),
+        user => "no_such_entity"
+    ) if !$sp->load(speculative => 1);
 
     # override metadata contacts if needed
     my $contacts =
@@ -406,10 +357,9 @@ sub req_select_email {
     $self->respond(
         template => 'select_email.tt2.html',
         data     => {
-            action     => 'select_email',
-            federation => $federation,
-            sp         => $sp,
-            entityid   => $entityid,
+            action   => 'select_email',
+            sp       => $sp,
+            entityid => $entityid,
         }
     );
 }
diff --git a/templates/web/edugain/select_sp.tt2.html b/templates/web/edugain/select_sp.tt2.html
index ef07dffef6e7004f75b007c8731f5d85196ff39e..bdcabe8677c1ce9394302953a6d4edf111d65dcf 100644
--- a/templates/web/edugain/select_sp.tt2.html
+++ b/templates/web/edugain/select_sp.tt2.html
@@ -22,8 +22,8 @@
         [% CASE 'list' %]
             <select id="[% group.id %]" name="[% group.id %]">
                 <option value=""></option>
-            [% FOREACH entity IN group.entities.sort('name') %]
-                <option value="[% entity.id %]" data-federation="[% entity.federation %]">[% entity.name %]</option>
+            [% FOREACH entity IN group.entities.sort('display_name') %]
+                <option value="[% entity.entityid %]">[% entity.display_name %]</option>
             [% END %]
             </select>
         [% CASE 'link' %]
@@ -31,7 +31,6 @@
     [% END %]
         </fieldset>
 [% END %]
-        <input type="hidden" id="federation" name="federation"/>
         <input type="hidden" id="entityid" name="entityid"/>
 
         <div class="callout primary">[% lh.maketext("Those service providers have been extracted from the selected federation metadata.") %]</div>
@@ -187,8 +186,9 @@ $( document ).ready(function() {
         }
     });
 
-[% FOREACH group IN groups.keys() %]
-    $( "#[% group %]" ).combobox();
+[% FOREACH group IN groups %]
+    [% NEXT IF group.type != 'list' %]
+    $( "#[% group.id %]" ).combobox();
 [% END %]
 
     $.validator.messages.required = "[% lh.maketext("This information is required") %]";
diff --git a/templates/web/renater/select_sp.tt2.html b/templates/web/renater/select_sp.tt2.html
index 2ea753adb3de1e2cb4cfc66d5e98fc6bb6511429..796d466bfeb6eae1ce0eec54c32fe3a66a9f931b 100644
--- a/templates/web/renater/select_sp.tt2.html
+++ b/templates/web/renater/select_sp.tt2.html
@@ -22,16 +22,15 @@
         [% CASE 'list' %]
             <select id="[% group.id %]" name="[% group.id %]">
                 <option value=""></option>
-                [% FOREACH entity IN group.entities.sort('name') %]
-                <option value="[% entity.id %]" data-federation="[% entity.federation %]">[% entity.name %]</option>
-                [% END %]
+            [% FOREACH entity IN group.entities.sort('display_name') %]
+                <option value="[% entity.entityid %]">[% entity.display_name %]</option>
+            [% END %]
             </select>
         [% CASE 'link' %]
             <a class="button" href="[% group.url %]">[% lh.maketext(group.message) %]</a>
     [% END %]
         </fieldset>
 [% END %]
-        <input type="hidden" id="federation" name="federation"/>
         <input type="hidden" id="entityid" name="entityid"/>
 
         <div class="callout alert-callout-border primary">[% lh.maketext("Those service providers have been extracted from the selected federation metadata.") %]</div>
@@ -92,8 +91,6 @@ $( document ).ready(function() {
 
                     // retrieve federation and entityid
                     var entity     = this.element.val();
-                    var federation = this.element.find('option:selected').attr('data-federation');
-                    $("#federation").val(federation);
                     $("#entityid").val(entity);
                 },
  
@@ -188,8 +185,9 @@ $( document ).ready(function() {
         }
     });
 
-[% FOREACH group IN groups.keys() %]
-    $( "#[% group %]" ).combobox();
+[% FOREACH group IN groups %]
+    [% NEXT IF group.type != 'list' %]
+    $( "#[% group.id %]" ).combobox();
 [% END %]
 
     $.validator.messages.required = "[% lh.maketext("This information is required") %]";