Prexens team dev' corner

How-to: insert a Data View Web Part in a WCM publication page

Goal of this post
This post guides you through the steps for inserting the famous DVWP, which is an exclusive part of SharePoint Designer, in a publishing page of a WCM site.

Problematic overview
As a reminder, when trying to open a publishing page with SharePoint Designer, the following message box appears:
 

Edit in browser

Because we can't open the page in SharePoint Designer, we cannot configure and add the DVWP…

Solution
SharePoint Designer contains a very handy feature for WCM scenarios: " Detach from Page Layout ". This will break the inheritance between the publishing content page and its page layout.

First, start by duplicating the publishing page:
 

Duplicate page


Right click on the copied page and select " Detach from Page Layout ": this will embed the layout in the content page.
We can now open the content page as a standard " collaborative " page, configure and insert our Data View Web Part:
 

Configure Data View Web Part


Once you've configured the Web Part, you can export it from the browser interface…
 

Export Web Part


… then re-import it in the SharePoint Web Part Gallery:
 

Upload to Web Part Gallery


You can now open the target content page again, and insert your custom DVWP directly in a Web Part zone.

Tip: we recommend you to keep the duplicated page in the library for further modifications!

Best practices and common errors when adding a ScriptManager programmatically from a WebPart

Goal of this post
 
This post aims at providing the most flexible and convenient way to make sure that a valid ScriptManager is available on page when you develop WebParts dealing with AJAX for ASP.net or Silverlight controls.
 
Problematic overview
 
ScriptManager is the key object making Silverlight and AJAX for ASP.net controls run on your ASP.net page. This non-visual control basically aims at loading JS scripts relevant for those technologies. Referring to MS documentation, it must appear as the first control in page controls tree in order to be available for any control requiring AJAX support. A ScriptManager can be embedded either from markup code of the hosting page, or programmatically during page lifecycle.
 
So let’s imagine you are developing a WebPart embedding a Silverlight application. You want to redistribute this WebPart but you don’t want users to modify anything in their SharePoint portal to make it run. Basically, in your code, you will try to see if a ScriptManager is present on the page and if no you will add it on the fly.
 
Then you have to face several issues:
  1. Where can I place my ScriptManager in the controls tree to make it available to relevant controls?
  2. Which event do I need to choose to attach my code?
  3. Will it be compliant with postback events, databinding cycles and WebParts connection management?
Solution
 
First, you need to add a new ScriptManager as the first control in the page form. Before adding anything, you have to ensure that no ScriptManager is already present on the page.
if (Page != null && ScriptManager.GetCurrent(Page) == null)
{
    Page.Form.Controls.AddAt(0, new ScriptManager());
}
Then you need to determine where to place this code to be compliant with requirements evoked at question #3. The most reliable answer is to override the OnInit event of your WebPart and paste your code there
protected override void OnInit(EventArgs e)
{
    if (Page != null && ScriptManager.GetCurrent(Page) == null)
    {
        Page.Form.Controls.AddAt(0, new ScriptManager());
    }

    base.OnInit(e);
}
An alternative approach could be to attach a handler to the page LoadComplete event, but it gives no gain to the previous solution.
 
Common errors
 
I’ve listed below some common mistakes and the corresponding errors when trying to answer to questions above.
Do not add control directly to Page’s controls collection; add it to its Form property.
Page.Controls.AddAt(0, new ScriptManager());
Page.Form.Controls.AddAt(0, new ScriptManager());
Striked code above will produce the following error
 
Control 'ctl01' of type 'ScriptManager' must be placed inside a form tag with runat=server
 
Do not try to override the WebPart OnLoad event to place your code.
If so, you will receive the error below
 
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
 
Do not try to add any handler to page events.
Web parts lifecycle is too restrictive to use this approach.
    • If you try to attach a handler to the Page Init event, your code will never be executed because Web parts are added after the page InitComplete event.

    • If you try to attach a handler to the Page Load or PreLoad events, your code will never be executed when your Web part is being added to the page and you will receive the error below (next attempts will work). The fact is that when a Web part is added to a page, any page event prior to LoadComplete is ignored.
The control with ID 'XXX' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
    • If you try to attach a handler to the Page PreRender event, you will receive the error below
Script controls may not be registered before PreRender.

Nothing happens when I click on the verbs menu of my custom web part

Symptoms
 
When several instances of your custom Web part are living on a page, the verbs menu is not displayed when you click on the top right arrow of the Web parts chrome. You can also notice that a JavaScript error is raised when you try to click on this menu.
 
Cause
 
The OnInit event has been overridden into your code, but the base method is not called. So, in the JS code produced by WebPartManager, no ID is given to objects controlling the client-side script of your Web parts.
 
Resolution
 
In your code, make sure that base.OnInit(e) is present in the OnInit method.
protected override void OnInit(EventArgs e)
{
        // Your code …

        base.OnInit(e);
}

Categories
Links
  Prexens Web Site
  Charting for SharePoint
  Charting for SharePoint (Codeplex)
  Charting for SharePoint Starter kit

Microsoft Certified Partner