The statute is a gate, not a suggestion
Business and Professions Code § 7031 lets an unlicensed contractor be denied recovery in court — and can force disgorgement of amounts already paid. The license classification must cover the work you actually performed, not the trade you usually market.
Substantial compliance is narrow
Courts sometimes excuse a brief lapse if you were licensed at the start and the work stayed in class. That is not a plan. Letting the license expire mid-job, using someone else's number, or working outside your classification is how § 7031 motions get granted.
Put the number on the notice
Preliminary notices and liens should carry your CSLB number. Owners and title companies look for it. If you cannot show a license that covered the work on the dates you furnished labor, generate the document after you fix the license problem — not before.