I have a feature which provisions a custom Default.aspx file to my publishing site.
In the elements.xml, I have various AllUsersWebPart nodes which populate the zones with web parts. I need to supply a hardcoded ID (guid) to each of these web parts at this point — does anyone know how to do this?
I know that the format of a webpart ID is g_00000000_0000_0000_0000_000000000000, but if I add an ID property (see below) and then activate my feature, the guids of the webparts are all different.
<AllUsersWebPart WebPartZoneID="TopZone" WebPartOrder="1">
<![CDATA[
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="..." />
<importErrorMessage>...</importErrorMessage>
</metaData>
<data>
<properties>
<property name="ID" type="string">g_FB777184_F9AB_4747_AA71_1BF0C96E535A</property>
</properties>
</data>
</webPart>
</webParts>
]]>
</AllUsersWebPart>
FYI: I need to hardcode an ID for each of my web parts because I have a seperate feature receiver which uses the ID to locate each web part on the page (I have 6x identical parts on the page with the same title) and then it assigns an audience to each part (so that only 1 is visible at any given time to a user).
Good and bad news…
The approach I was using is in fact valid, however it will only work if your web parts are not placed in a web part zone. If placed in a zone, the web part infrastructure automatically sets the WebPart.ID value (confirmed on MSDN).
In order to hard-code the web part IDs, you need to place your web parts directly in the page layout (so that you can place them in directly within the page’s markup).
Personally, I have opted to leave the web parts in the zones and instead use unique titles which I can use later in my feature reciever to identify the parts.