President Trump said Friday there is tremendous anger over what he called the FBI's "disgraceful" behavior, taking aim at the bureau just before he appeared at its training facility to praise the nation's police officers.

"It's a shame what's happened with the FBI," the president told reporters as he prepared to depart the White House for a ceremony at the FBI's National Academy in Quantico, where more than 200 law enforcement officers graduated from a program that imparts FBI expertise and standards.

"We're going to rebuild the FBI," Trump said. "It'll be bigger and better than ever. But it is very sad when you look at those documents, and how they've done that is really, really disgraceful, and you have a lot of very angry people that are seeing it."

The president appeared to be referring to revelations that senior FBI officials exchanged anti-Trump and pro-Hillary Clinton text messages while working on last year's probe of Clinton's use of a private email server while she was secretary of state and again during special counsel Robert S. Mueller III's investigation into Russian interference in the presidential election, including the possibility of coordination with the Trump campaign.

[Internal FBI texts reveal senior officials disparaged Trump, hoped for Clinton]

The remarks come at a fraught time in the White House's relationship with the FBI and the Justice Department. Trump has said the FBI's reputation is "in tatters" over its handling of politically sensitive cases.

His comments also highlighted what has become a recurring theme during his presidency: Trump's tendency to criticize the FBI, the nation's premier law enforcement agency, and extol the local police officers who have become central to his law-and-order agenda. Police departments, Trump said while at Quantico, "are totally underappreciated, except by me."

Trump made the group a promise.

"I want you to know that with me as your president, America's police will have a true friend and loyal champion in the White House," Trump said, "more loyal than anyone else can be, I tell you."

While the event was held at a well-known FBI facility, the audience did not include many FBI personnel. The graduating class comprised law enforcement officials from elsewhere, so the crowd was mostly those individuals and their families.

There were some FBI instructors and officials on hand but few rank-and-file agents, according to people involved in the event. A last-minute invitation went out to FBI personnel, but almost immediately FBI personnel were told there was no room for additional attendees, according to these people.

Trump appeared on a stage alongside FBI Director Christopher A. Wray and Attorney General Jeff Sessions, who introduced the president by calling him "our nation's highest law enforcement official." That title carries possible implications for the ongoing criminal probe into whether Trump attempted to obstruct justice leading up to the firing of FBI Director James B. Comey in May.

The president's defenders say that, as the nation's top law enforcement official, he cannot obstruct justice by firing the head of the FBI. However, in past administrations that phrase has been used to describe the attorney general, not the president.

During a news conference at the Justice Department later Friday, Sessions spoke positively about the FBI but suggested the agency might have problems that need fixing.

"You can have arrogance sneak into, sometimes, an institution," he said. "I don't share the view that the FBI is not functioning at a high level."

[FBI director defends bureau’s integrity as GOP lawmakers press him on Trump, Clinton probes]

When Trump took the lectern Friday, he did not mention or allude to any of his problems with the bureau, instead focusing on the police and other law enforcement officers present.

"We as a country must do a better job showing our police officers our respect and gratitude that you have earned, and we will do that," the president said.

Trump said immigration and crime in Chicago, specifically, were among his greatest concerns when it comes to police work. "What the hell is happening there?" he asked, to some applause from the graduates.

Chicago has struggled with a spike in gun violence and murders in recent years, but police say homicides and shootings there have decreased from last year, which was the deadliest in two decades.

He repeated his call to prevent terrorism by changing immigration policies, including the issuance of visas to relatives of U.S. citizens, a practice that critics call "chain migration."

In stark language, he denounced the MS-13 street gang, which is active in Central America and many parts of the United States. He called its members "savages'' who should be jailed or sent back to their native countries.

"We don't want them. We don't want them," the president said. "Our cities should not be sanctuaries for criminals. They should be sanctuaries for America."

He linked the two recent terrorist attacks in New York City to what he called the nation's "dysfunctional immigration system."

"One came through chain migration — chain migration — the other, visa lottery," Trump said of the alleged terrorists.

"They have a lottery. You pick people. Do you think the country is giving us their best people? No," he said to laughter. "What kind of a system is that?"

"They come in by lottery. They give us their worst people. They put them in a bin," Trump went on, apparently comparing the system to televised lottery drawings that use a barrel or bin.

"In his hand, when he's picking them is, really, the worst of the worst," Trump continued. "Congratulations! You're going to the United States!"

The audience laughed.

Trump drew more laughter when he pointed out journalists there to cover his remarks.

"There's the fake news back there. Look, everybody," Trump said. "Fake news. No, actually, some of them are fine people," he said, smiling. "About — let's see, who's back there? Yeah, about 30 percent."