Click here to Skip to main content
Click here to Skip to main content

AjaxConnectedPageViewer

, 17 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
With Microsoft ASP.NET AJAX 1.0, you can build more dynamic applications that come closer to the rich style of interruption-free interaction. This web part will give a full insight of site collections as a tree view and the respective attributes as a data grid, which are connected to each other.

Introduction

This is my second release as a development cycle of an AJAX Connectable webpart. While doing some R&D, I found that the RowConsumer and Provider interfaces, ICellConsumer and ICellProvider are more effective when we are writing a connectable web part in the AJAX framework.

Background

This is my second article, so for a more deep insight of AJAXConnectableWebPart, check out my previous article: AjaxConnectableWebPart_V1.0.0.0.

Using the code

First, we need to derive our web part from the ICellProvider interface:

/// <span class="code-SummaryComment"><summary></span>
/// This is an ajax-enabled webpart that sends
/// URL to a connectable PageViewer webpart
/// <span class="code-SummaryComment"></summary></span>
public class AjaxUrlListWP : WebPart, ICellProvider
{
}

There are some events required by ICellProvider:

//Events required by ICellProvider
public event CellProviderInitEventHandler CellProviderInit;
public event CellReadyEventHandler CellReady;
/// <span class="code-SummaryComment"><summary></span>
/// This method is called by the Web Part infrastructure to notify the Web Part 
/// that it has been connected.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="interfaceName">Friendly name of the interface</span>
///              that is being connected.<span class="code-SummaryComment"></param></span>
/// <span class="code-SummaryComment"><param name="connectedPart">Reference to the other Web Part</span>
///              that is being connected to<span class="code-SummaryComment"></param></span>
/// <span class="code-SummaryComment"><param name="connectedInterfaceName">Friendly name of the interface</span>
/// on the other Web Part through which they are connected<span class="code-SummaryComment"></param></span>
/// <span class="code-SummaryComment"><param name="runAt">Where the interface can be executed</param></span>
public override void PartCommunicationConnect(string interfaceName,
    WebPart connectedPart, string connectedInterfaceName, ConnectionRunAt runAt)
{
    //Receive connection from "Cell_Provider_Interface_WPQ_" interface only.
    if (interfaceName == "Cell_Provider_Interface_WPQ_")
    {
        //the connection is accepted, and the web part is connected now
        isConnected = true;                
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// This method is called by the Web Part infrastructure to allow the Web Part
/// to fire any initialization events
/// <span class="code-SummaryComment"></summary></span>
public override void PartCommunicationInit()
{
    //if the web part is connected and the CellProviderInit listener is created,
    //create the args for CellProviderInit event and fire it to tell the Consumer
    //Web Part what type of cell it will be receiving when CellReady is fired later.
    if (isConnected && CellProviderInit != null)
    {
        CellProviderInitEventArgs cellProviderInitArgs = new CellProviderInitEventArgs();
        //set the field name.
        cellProviderInitArgs.FieldName = "URL";
        CellProviderInit(this, cellProviderInitArgs);
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// This is called by the Web Part infrastructure to allow the Web Part to fire any
/// of the other events from the interface (for example, CellReady). During the
/// execution of the PartCommunicationMain method, the actual communication of data
/// values takes place between Web Parts. 
/// <span class="code-SummaryComment"></summary></span>
public override void PartCommunicationMain()
{
    //if the web part is connected and CellReady event is created,
    //create the args for this event and fire it.
    if (isConnected && CellReady != null)
    {
        CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
        if (send_data && !string.IsNullOrEmpty(tv.SelectedValue))
        {
            //set the cell field with the selected URL
            //this field will be sent to the consumer.
            cellReadyArgs.Cell = tv.SelectedValue;
        }
        else
        {
            //nothing was selected
            cellReadyArgs.Cell = "";
        }
        CellReady(this, cellReadyArgs);
    }
}

/// <span class="code-SummaryComment"><summary></span>
/// Register a client startup script to fixup the update panel, we need this
/// because Windows SharePoint Services JavaScript has a "form onSubmit wrapper"
/// which is used to override the default form action.
/// <span class="code-SummaryComment"></summary></span>
private void EnsureUpdatePanelFixups()
{
    if (this.Page.Form != null)
    {
        //modify the form onSubmit wrapper
        if (this.Page.Form.Attributes["onsubmit"] == "return _spFormOnSubmitWrapper();")
        {
            this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
        }
    }
    //register the script
    ScriptManager.RegisterStartupScript(this, typeof(AjaxUrlListWP), "UpdatePanelFixup", 
      "_spOriginalFormAction = document.forms[0].action;" + 
      " _spSuppressFormOnSubmitWrapper=true;", true);
}

We are fetching the sub-sites at runtime, when the user expands the tree node. So, I have written a recursive method which will take care of filling the treeview at runtime.

public void CreateTreeOnExpandNode(string URL, TreeNode nodeExpanded)
{
    try
    {
        SPSite site = new SPSite(URL);
        SPWeb web = null;
        if (site.Url == URL)
        {
            web = site.OpenWeb();
        }
        else
        {
            URL = URL.Replace(site.Url, "");
            if (site.ServerRelativeUrl != "/") URL = site.ServerRelativeUrl + URL;
            web = site.OpenWeb(URL);
        }
        foreach (SPWeb web1 in web.Webs)
        {
            TreeNode childnode = new TreeNode(web1.Url);
            //GetChild(web1, childnode);
            if (web1.Webs.Count > 0)
            {
                TreeNode emptyNode = new TreeNode();
                childnode.ChildNodes.Add(emptyNode);
            }
            nodeExpanded.ChildNodes.Add(childnode);
        }
    }
    catch (Exception ex)
    {
        Page.Response.Write(ex.Message);
    }
}

Consumer part: For creating the consumer part, I have derived my control class from the ICellConsumer interface, and the rest of the things are pretty similar to the provider part.

For getting data from the provider part:

/// <span class="code-SummaryComment"><summary></span>
/// Implements CellReady Event Handler. Receives data from provider
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="sender">provider web part</param></span>
/// <span class="code-SummaryComment"><param name="cellReadyArgs">arguments sent by the provider</param></span>
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
{
    if (cellReadyArgs.Cell != null)
    {
        //gets the URL from the provider and save it in ContentLink
        ContentLink = cellReadyArgs.Cell.ToString();
    }
}

#endregion

Happy SharePointing! Cheers!!!

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bh@nu
Team Leader
India India
"In the attitude of silence the soul finds the path in an clearer light, and what is elusive and deceptive resolves itself into crystal clearness. My life is a long and arduous quest after knowledge & Truth."
I'm playing with all ancient and new Microsoft Technologies since last two and half years.

Comments and Discussions

 
GeneralPlease improve formatting PinmemberMadhur Ahuja18-Mar-08 8:31 
GeneralRe: Please improve formatting PinmemberAdityaBhanu16-Jun-08 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 17 Mar 2008
Article Copyright 2008 by Bh@nu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid