One of the thirty-nine Melachot (categories of activity) that are forbidden on Shabbat is “Me’amer,” which means collecting into a pile. For example, it is forbidden on Shabbat to collect scattered stalks of grain or other produce into a pile. (According to some views, the Torah prohibition is violated only if one actually ties the pile together.) The Sages enacted a provision extending the prohibition even to items that do not grow from the ground. One example would be collecting salt from a quarry. Since salt does not grow from the ground, collecting it would not transgress the Torah prohibition of “Me’amer,” but it is nevertheless forbidden by force of Rabbinic enactment.
The question thus arises as to the permissibility of the common custom to throw candies in the synagogue in honor of a Hatan or Bar Misva boy, and to throw Lambass on Simhat Torah. The candies that are thrown are often collected into a basket or a bag, which would seemingly violate the prohibition of “Me’amer.” Although this certainly does not transgress the Torah prohibition, which, as mentioned, applies only to items that grow from the ground, it seems, at first glance, to violate the Rabbinic prohibition forbidding collecting even items that do not grow from the ground.
Hacham Ovadia Yosef, in his Hazon Ovadia (vol. 4, p. 109; listen to audio recording for precise citation), rules that it is permissible to collect the candies thrown in the synagogue on Shabbat. Firstly, the Ramban (Rabbi Moshe Nahmanides, Spain, 1194-1270), in Masechet Shabbat (143), writes that the prohibition of “Me’amer” – even on the level of Rabbinic enactment – applies only in “Makom Gidulo,” meaning, in the place where the items originate. Thus, for example, as mentioned, it would be forbidden to collect scattered salt grains in a quarry, because this is where the salt originates and where it is normally collected. It is not forbidden, however, to collect scattered items in other locations. The synagogue is obviously not where candies originate, and it is therefore permissible to collect candies into a pile on Shabbat. Furthermore, according to some Rishonim (Medieval Halachic scholars), there is a rule that “En Imur Ahar Imur” – meaning, once items have been collected, one is allowed to collect them again if they become scattered. The candies had already been together in the bag in which they were sold, and therefore, according to this position, it would be permissible to collect them after they are thrown in the synagogue. Hacham Ovadia emphasizes that it is permissible even to collect them in a basket, and certainly to collect them in one’s pocket.
Summary: It is permissible on Shabbat to collect the candies that are thrown in the synagogue in honor of a groom, Bar Misva, or other occasion.