SHANGHAI, Aug. 9 — After years of fighting unionization efforts at its stores, Wal-Mart, the world’s largest retailer, said today that it would work closely with Chinese officials to establish labor unions at all of its outlets here.

Wal-Mart said it would form an alliance with the government-backed All China Federation of Trade Unions because it wanted to create “an effective and harmonious way of facilitating the establishment of grassroots unions” at its stores.

The announcement came less than two weeks after Wal-Mart employees established their first union in China, the first time that a union had ever been formed at a Wal-Mart store. Since then, four other Wal-Mart stores in China have also formed unions, according to the government union officials.

Wal-Mart’s decision surprised observers because while the company had signaled earlier that it would not do anything to stop unions from forming at its Chinese stores, it had never suggested that it would actively participate in backing unionization efforts.