In a victory for the Reagan administration, the Supreme Court ruled yesterday that legal and political advocacy groups may be barred from participating in the government's $120 million annual charity drive.

The decision, announced as the court concluded its 1984-85 term, said the groups have a First Amendment right to solicit contributions during the Combined Federal Campaign from some 4 million civilian and military employes.

But the administration was justified in trying to exclude them, Justice Sandra Day O'Connor wrote, as the court split 4 to 3 on the case with two justices not participating. The campaign is the only way in which charitable groups may solicit federal employes at work.

The campaign, unlike public streets and parks, is not a public forum, O'Connor said, so the administration acted reasonably in trying to restrict the organizations' participation.

She wrote, "The government does not violate the First Amendment when it limits participation in order to minimize disruption to the federal work place, to ensure the success of the fund-raising effort or to avoid the appearance of political favoritism."

President Reagan, who two years ago issued an ordering barring legal-advocacy groups from the campaign, "could reasonably conclude that a dollar spent directly on providing food or shelter to the needy is more beneficial than a dollar spent on litigation that might or might not result in aid to the needy," O'Connor wrote.

The First Amendment, O'Connor said in Cornelius v. NAACP Legal Defense and Educational Fund Inc., "does not forbid a viewpoint-neutral exclusion" of groups.

But the ruling, overturning a decision last year by the U.S. Court of Appeals here, does not end the controversy over the administration's attempts to limit the campaign to groups that provide what it calls "direct health and welfare services" to the poor. It sends the case back to U.S. District Court Judge Joyce Hens Green to decide whether the government improperly excluded the groups for political reasons.

Steve Ralston, an attorney for the NAACP fund, said, "The battle is not over. We intend to continue the fight to remain in the campaign and hope to be able to prove that the real reason for our exclusion is the Reagan administration's hostility toward civil rights and environmental organizations."

If Green decides, as she did in a case involving Planned Parenthood, that the government's motives were improper, the government may have difficulty appealing, Ralston said. Green, a liberal, voiced strong displeasure with the administration's actions during the Planned Parenthood litigation.

The NAACP fund, which has collected $1 million in campaign contributions in the last four years, apparently will be allowed to participate next fall while litigation continues, Ralston said, as will other legal-advocacy organizations.

The ruling brought a lengthy dissent by Justice Harry A. Blackmun, joined by Justice William J. Brennan Jr.

"The fact that the president or his advisers may believe the money is best 'directly spent on providing food or shelter to the needy' starkly fails to explain why" these particular groups were excluded while "other groups that do not spend money to provide food or shelter directly to the needy are allowed to be included," Blackmun wrote.

He cited the District of Columbia Music Center, the Howard Theatre Foundation and the D.C. Striders, a group of promising high school athletes. He also said that contributions, rather than declining because of "controversy," actually increased during the years advocacy groups were included.

Justice John Paul Stevens, in a separate dissent, said the government's "supposed fear of controversy in the work place is pure nonsense."

"One might as well prohibit discussions of politics, recent judicial decisions or sporting events," he said, adding that "expressions of affection for the Dallas Cowboys would surely be forbidden in all federal offices located in the District of Columbia if the avoidance-of-controversy rationale were valid."

Justice Thurgood Marshall, who had been legal director for the NAACP, did not participate. Justice Lewis F. Powell Jr., who missed oral argument because of illness, did not vote.