The New York State attorney general’s office filed a scathingly worded lawsuit on Thursday taking aim at the Donald J. Trump Foundation, accusing the charity and the Trump family of sweeping violations of campaign finance laws, self-dealing and illegal coordination with the presidential campaign.

The lawsuit, which seeks to dissolve the foundation and bar President Trump and three of his children from serving on nonprofit organizations, was an extraordinary rebuke of a sitting president. The attorney general also sent referral letters to the Internal Revenue Service and the Federal Election Commission for possible further action, adding to Mr. Trump’s extensive legal challenges.

The lawsuit, filed in State Supreme Court in Manhattan, culminated a nearly two-year investigation of Mr. Trump’s charity, which became a subject of scrutiny during and after the 2016 presidential campaign. While such foundations are supposed to be devoted to charitable activities, the petition asserts that Mr. Trump’s was often improperly used to settle legal claims against his various businesses, even spending $10,000 on a portrait of Mr. Trump that was hung at one of his golf clubs.

The foundation was also used to curry political favor, the lawsuit asserts. During the 2016 race, the foundation became a virtual arm of Mr. Trump’s campaign, email traffic showed, with his campaign manager, Corey Lewandowski, directing its expenditures, even though such foundations are explicitly prohibited from political activities.