After two years and more than half a million dollars in legal fees, the Virginia Supreme Court on Friday rejected Attorney General Ken Cuccinelli II’s assertion that the state’s flagship university had to turn over documents related to global warming.

The decision was a defeat for Cuccinelli (R), a global-warming skeptic who has garnered national attention for a string of high-profile lawsuits, just as he kicks off his campaign for governor next year.

And it comes months after a federal appeals court tossed out Cuccinelli’s challenge to the new federal health-care law.

The state’s highest court wrote in an opinion that Cuccinelli lacked the authority to subpoena records — including e-mails, drafts and handwritten notes — from the University of Virginia involving well-known climate scientist Michael Mann’s research.

Mann, now a professor at Pennsylvania State University, accused the attorney general of engaging in a two-year “character assassination’’ against him. He just completed a book, “The Hockey Stick and the Climate Wars,” about global-warming skeptics, including Cuccinelli, and what he calls their attacks on scientists.

“It’s a victory for science,’’ Mann said of Friday’s decision. “I hope that this is the end of this long and unfortunate episode so I, and other scientists, can get back to work.”

In 2010, Cuccinelli issued a civil investigative demand — essentially a subpoena — for Mann’s grant applications and correspondence between Mann and research assistants, secretaries and 39 other scientists across the country.

Cuccinelli used a 2002 state law designed to catch government employees defrauding the public of tax dollars to investigate whether Mann, to obtain grants, used manipulated data to show that there has been a rapid, recent rise in the Earth’s temperature.

“From the beginning, we have said that we were simply trying to review documents that are unquestionably state property to determine whether or not fraud had been committed,” Cuccinelli said in a statement.

In an unusual step, U-Va. hired its own attorney and fought back, arguing that the demand exceeded Cuccinelli’s authority under state law and intruded on the rights of professors to pursue academic inquiry free from political pressure. U-Va. spokeswoman Carol Wood said the school spent $570,698 on legal fees — all of which came from private funds.

“This is an important decision that will be welcomed here and in the broader higher education community,” U-Va. President Teresa Sullivan said in a statement. “I am grateful for the ongoing support of our own faculty and the faculty at many institutions around the world.”

Mann’s work has long been under attack by global-warming skeptics, particularly after references to a statistical “trick” he used in his research surfaced in a series of leaked e-mails from the University of East Anglia’s Climatic Research Unit. He and others have said that the e-mail was taken out of context.

Some of his methodology has been criticized by other scientists, but several inquiries, including a high-profile one by the National Science Foundation, concluded that there was no evidence that Mann engaged in efforts to falsify or suppress data.

“For two years, the attorney general has joined a small but vocal minority in a pointless and costly investigation that has done nothing but distract Virginia from the real challenge: mitigating and adapting to climate change,’’ said Michael Halpern, a program manager for the Union of Concerned Scientists’ scientific integrity program.

A Circuit Court judge initially dismissed the subpoena, ruling that Cuccinelli had failed to provide evidence of wrongdoing by Mann or any other climate scientist.

Cuccinelli then filed a new, more specific demand pertaining to just one $214,700 state grant, but he also appealed the ruling to the Supreme Court.

The Supreme Court ruled that the state anti-fraud act does not authorize the attorney general to issue civil investigative demands against U-Va. or other state agencies because under the act, they are not considered “persons.”

A spokesman for Cuccinelli said he has no recourse for appeal. Both sides said they will ask an Albemarle County Circuit Court judge to dismiss the case.