Provisioning XsltListViewWebPart in Site Definition / Issue With Web Part

Scenario:

We have our custom site definition. We are provisioning an instance of a Link List in a <Lists> element in the site definition.

We need to add a XsltListViewer web part (XsltListViewWebPart) on the front page for the preceding Link List. So we have a module element to provision the front page and added a XSLT Viewer web part having order 2. This module is added into the web level feature and not in Onet.xml.

But the problem is whenever we were creating site this XsltListViewer web part is appearing over the top, in other words having order 0 for it. It's not coming over the order specified for it, in other words 2.

After doing much research it's determined that this is related to order of execution in Onet.xml. In the following order the parsing of Onet.xml happens:

  1. Site Features
  2. Stapled Site Features
  3. Web Features
  4. Stapled Web Features
  5. <Lists> element
  6. <Modules> element
Now, we are provisioning a page and web parts on it using the web level feature so whenever the page is provisioned, SharePoint is trying to add a XsltListViewer web part for the list that is not provisioned yet. The <Lists> element executes after the <WebFeatures>.

But surprisingly SharePoint doesn't throw an exception there or doesn't fail the site creation process but later adds it as a top web part in the respective zone, in other words having order 0.

There are the following two possible approaches for this problem:
  1. We provision a list instance in the web level feature and set the dependency of this feature over the feature that provision the pages. We used this approach and the web part is placed in the correct order.
  2. Use the <Module> element from Onet.xml itself for provisioning the pages since the <module> element executes last. Until the time <Lists> are provisioned. 
References:
http://www.martinhatch.com/2008/10/onetxml-order-of-execution-shown-in-8.html

http://social.msdn.microsoft.com/Forums/office/en-US/9387c512-032d-4db6-b893-e0e91662c144/caml-execution-sequence?forum=sharepointdevelopmentlegacy

http://stefan-stanev-sharepoint-blog.blogspot.fi/2010/12/feature-stapling-in-sandbox-solutions.html

Feel free if you have some thoughts on this or please share your experience in case you have encountered a similar kind of error and solved it.
Thanks! Feel free to comment and/or provide feedback if any or if you have a query.

Up Next
    Ebook Download
    View all
    Learn
    View all