Thursday, April 2, 2009

MOSS with ATLAS (ASP.Net AJAX)


Introduction - MOSS with ASP.Net AJAX:

ASP.NET AJAX is the newest addition to ASP.NET 2.0 and is a supported Microsoft framework for next-generation Web development using AJAX technologies. AJAX is shorthand for "Asynchronous JavaScript + XML," an approach to Web applications that utilizes asynchronous data requests from the page with a client-side JavaScript engine. It is not one technology, but rather a combination of technologies that enables rich-client responsiveness in the browser when used together. It's also not a new technology, but rather a new trend that combines old technologies in a new way. The technologies include object-oriented JavaScript, asynchronous data requests utilizing the XMLHttpRequest object, XML data streams, Extensible Style sheet Language transformation (XSLT) transforms, and dynamic manipulation of the HTML Document Object Model (DOM). It more closely resembles a recipe for Web applications than a single architecture-it is an approach to Web applications based on rendering and manipulating data in smaller components during the page's life cycle in response to user actions on the client rather than the processing of the entire page as a whole. Because of this design pattern, Web Parts make an excellent deployment mechanism for AJAX components in Microsoft Windows SharePoint Services (WSS), and many of the built-in WSS APIs can be leveraged in JavaScript code from the browser.

The main benefit of using AJAX techniques in Web applications is an increased level of responsiveness once possible only in rich-client applications. Instead of waiting for a page reload or form post to update the user interface, the browser can send a discrete call to the server by using the XmlHttpRequest object while enabling the user to continue working.

Windows SharePoint Services version 3 builds much more directly on top of ASP.NET 2.0; therefore, many of the capabilities of ASP.NET AJAX work directly with SharePoint.
In a few cases there are some compatibility issues between ASP.NET AJAX and SharePoint (anticipated to be addressed in the first service pack of Windows SharePoint Services). Specifically, there are some limitations on usages of the UpdatePanel in web parts and controls. I personally experienced this issue in TreeView control inside the Update Panel.


Adding ASP.NET AJAX to SharePoint Pages :

  1. Download and install ASP.NET AJAX on servers in your farm.
  2. Extend web.config with some settings to enable ASP.NET AJAX technology.
  3. Add the ASP.NET AJAX Script Manager into your master page to enable scenarios such as Extenders or UpdatePanels. (Install the full "ASP.NET 2.0 AJAX Extensions 1.0" from ajax.asp.net. )

The AJAX WSS Web page life cycle:

The figure below (Figure – 1.0) illustrates the dynamic nature of the AJAX approach. Instead of one fat data stream being processed in the main request/response, individual components are responsible for their processing and can be individually refreshed and updated in response to user actions, events, or timer components.



(Figure – 1.0)

The user interface and data streams could also evolve over the client life cycle of the page-new components can be spawned from additional data sources and loaded into new components, all without reloading or posting the Web page. The Web page life cycle is much longer in an AJAX application because the page is not refreshed after the initial page load.

AJAX Web Parts:

AJAX components within Web Parts require the Web Part Client Control Registration Pattern. With this design pattern, a placeholder is registered with the page and processed through a convention in the JavaScript application load event.

The other most important thing is Extending SharePoint web.config files with ASP.NET AJAX. It requires some Ajax registration entries in-line with WSS registration entries.

Sample Code:

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using System.Data;
namespace AJAXWebPart
{
public class AJAXWebPart:WebPart
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
SPTreeView sptv; // SharePoint display TreeView
TreeView tv; // Dummy Tree view asp.net control
string strSPWebApplication = "";
private DataTable _table;

protected override void CreateChildControls()
{
base.CreateChildControls();
this.EnsureUpdatePanelFixups();
UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel1";
up.ChildrenAsTriggers = true;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
this.Controls.Add(up);
tv = new TreeView();
sptv = new SPTreeView();
GetSiteCollection(); //Function to Fill all Site Collection Only..
sptv.CollapseAll();
sptv.TreeNodeExpanded += new TreeNodeEventHandler(sptv_TreeNodeExpanded);
sptv.TreeNodeCollapsed += new TreeNodeEventHandler(sptv_TreeNodeCollapsed);
up.ContentTemplateContainer.Controls.Add(tv);
up.ContentTemplateContainer.Controls.Add(sptv);
}
void sptv_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
{
TreeNode CurNode = new TreeNode();
CurNode = e.Node;
CurNode.CollapseAll();
}

void sptv_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
TreeNode CurNode = new TreeNode();
string strWebNode = e.Node.Text;
CurNode = e.Node;
CurNode.ChildNodes.Clear();
CreateTreeOnExpandNode(strWebNode, CurNode);
}
public void CreateTreeOnExpandNode(string URL,TreeNode nodeExpanded)
{
SPSite site = new SPSite(URL);
SPWeb web = site.RootWeb;
//rootnode = new TreeNode(web.Url);
foreach (SPWeb web1 in web.Webs)
{
TreeNode childnode = new TreeNode(web1.Url);
GetChild(web1, childnode);
nodeExpanded.ChildNodes.Add(childnode);
}
}
private void GetChild(SPWeb web1, TreeNode refnode)
{
foreach (SPWeb web2 in web1.Webs)
{
TreeNode subsitenode = new TreeNode(web2.Url);
refnode.ChildNodes.Add(subsitenode);
GetChild(web2, subsitenode);
}
}
public void GetSiteCollection()
{
SPWebApplication wa;
wa = SPControl.GetContextWebApplication(Context);//Get Url from HTTP context
SPSiteCollection wasites = wa.Sites;
TreeNode rootnode;
sptv.RootNodeStyle.ImageUrl = "/_layouts/1033/IMAGES/USM.gif";
foreach (SPSite site in wasites)
{
SPWeb web = site.RootWeb;
rootnode = new TreeNode(web.Url);
if (web.Webs.Count > 0)
{
TreeNode emptyNode = new TreeNode();
rootnode.Collapse();
rootnode.ChildNodes.Add(emptyNode);
}
sptv.Nodes.Add(rootnode);
}
}

private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
ScriptManager.RegisterStartupScript(this, typeof(AJAXWebPart), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
}


}
}


No comments:

Post a Comment