If you want to calculate what the electrostatic potential energy is, you either calculate the work that you have to do to bring all these charges in place, or, if it is easier, you can take the electric field everywhere in space, if you know that, and do an integration over all space. 