Ok let me recreate this in mel for you (you should prototype these things in mel btw im showing the reasoning here):
createNode -n mylamb1 lambert;
createNode -n myset shadingEngine;
connectAttr -f mylamb1.outColor myset.surfaceShader;
this si what your doing.Observe what maya does:
shadingNode -n mylamb1 -asShader lambert;
sets -renderable true -noSurfaceShader true -empty -name myset;
connectAttr -f mylamb1.outColor myset.surfaceShader;
Very basic mel and api programming fundamentals to ask maya first , now these 2 go into the heart of the matter. Set are a bit special so with this knowledge you should be probably using MFnSet. However back to the original. let use compare the 2.
using list conections:
myset
mylamb1
well the later gives this
materialInfo1 defaultShaderList1 myset
lightLinker1 materialInfo1 lightLinker1 renderPartition mylamb1
Obviously theres still a tad to do... Anyway it wont work well.
Still one problem well see i said to you to use MFnSet well i mean it because otherwise the corresponding group might not come to existence into the meshes data. Off course all maya made meshes have but thats no guarantee for the api.
However might be even smarter to use MFnLambertShader instead of al the hastle