[SharePoint 2010 – Developpement] Create a Content Query Webpart in Site Definition (onet.xml), with dynamic site Url

I wanted to create a webtemplate with content query webpart in pages. Those webpart had to have the current site url. So I used the hack “~Site” in the properties of the webpart
Here are the step for it :

Step 1 : Create the Content query WebPArt Graphically

Create your webpart, and edit its properties (filter on content type, on column etc)

STEP 2:  Export the WebPart

From the page, export the webpart

STEP 3 : Modify the generated file exported .webpart

Modification 1 :
<property name=”ListGuid” type=”string”>d2d…
replace it by =>
<property name=”ListGuid” type=”string”/>

Modification 2 :
replace the property Weburl by
<property name=”WebUrl” type=”string”>~Site</property>

=> ~Site generates the website url dynamically

Step 4 : Integrate the CQWP in your onet.xml

In the AllUsersWebpart Tag, put the code of your webpart as that :
   1: <AllUsersWebPart
   2:           WebPartZoneID="Left"
   3:           WebPartOrder="1">
   4:           [CDATA[<webParts><webPart xmlns="http://schemas.microsoft.com/WebPart/v3%22>
   5:     <metaData>
   6:       <type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
   7:       <importErrorMessage>Cannot import this Web Part.importErrorMessage>
   8:     metaData>
   9:     <data>………..data>
  10:   webPart>
  11: webParts>]]>
  12:         AllUsersWebPart>

Deploy the solution and it will work Sourire

Advertisements

2 thoughts on “[SharePoint 2010 – Developpement] Create a Content Query Webpart in Site Definition (onet.xml), with dynamic site Url

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: