This study formulates a novel optimisation problem for joint spectrum sensing and random access control (JS 2 RAC) in cognitive radio sensor networks (CRSNs). The JS 2 RAC is formulated as a network utility maximisation problem, which aims to maximise the sum of utilities over all links in the network but subject to the primary user protection constraint, the energy constraint, and the physical constraint. Due to the non-separable and non-convex nature of the JS 2 RAC problem, the authors propose a primal-decomposition-based iterative (PDI) algorithm which decomposes the JS 2 RAC problem into a spectrum sensing subproblem and a random access control subproblem, and solve the two subproblems iteratively. Then, the authors prove the convergence of the PDI algorithm and show its distributed implementation in practice. Simulations demonstrate the fast convergence and the near-optimal nature of the PDI algorithm and show its significant improvement in the network utility of CRSNs in comparison with the method of optimising spectrum sensing and random access separately.