Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

AJAXConnectableWebPart

0.00/5 (No votes)
16 Mar 2008 1  
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 full insight of site collection as tree view and respective attributes in as a data grid, which are connected to each other.

Introduction

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 full insight of site collection as tree view and respective attributes in as a data grid, which are connected to each other.

AJAX.JPG

Background

Microsoft ASP. NET AJAX 1.0 lets developers build Web 2.0 sites using the latest Ajax techniques. ASP.NET AJAX extends ASP.NET 2.0 and makes several new tools and techniques available to help you build applications more quickly:

Extensions to JavaScript: ASP.NET AJAX extends the JavaScript library to bring standard object oriented concepts to JavaScript. It brings a formal type declaration system, with support for inheritance. It also provides a significant number of out of the box types, including types such as Sys.Net.WebRequest for working with web services. Finally, it helps to abstract some cross-browser issues such as XML element traversal. This makes it much easier to create robust JavaScript libraries and frameworks which are commonly needed by rich internet applications.

ASP.NET Control Extenders: Extenders are additional ASP.NET controls which can extend the functionality of existing controls with additional Ajax capabilities. A common example is an extender which allows existing textbox controls to have autocomplete functionality with no modification to the extended control. (The autocomplete extender is included with the ASP.NET AJAX Control Toolkit.)

UpdatePanels: UpdatePanels allow your existing ASP.NET controls and web parts to achieve the fluid, no-postback updates of Ajax-based applications with minimal re-coding of your control or part. Quite simply, controls within the UpdatePanel control which ordinarily would post back to update their data will now be routed through an Ajax-style callback, resulting in a silent update back to the server. This makes your application “postback” much less, making interaction with your control more seamless. With Microsoft ASP.NET AJAX 1.0, you can build more dynamic applications that come closer to the rich style of interruption-free interaction you may see in standard client applications.

Microsoft ASP.NET AJAX 1.0 and SharePoint

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. However, in a few cases there are some compatibility issues between ASP.NET AJAX and SharePoint which are anticipated to be addressed in the first service pack of Windows SharePoint Services. For this reason, until that first Service Pack of Windows SharePoint Services, we are unable to offer formal support through Microsoft Product Support Services for usage of ASP.NET AJAX within a SharePoint Web site. This post is intended to provide developers with some guidelines for evaluating ASP.NET AJAX and WSS3.0. Specifically, there are some limitations on usages of the UpdatePanel in your web parts and controls. Some approaches are described below to address these limitations, but these are workarounds and as such may cause other issues in your application. Here are some common scenarios in SharePoint you should be able to achieve with Microsoft ASP.NET AJAX 1.0: Building a more powerful, re-usable JavaScript libraries you can use in your web controls and parts Enabling your web services to render via JSON, resulting in easier usage in JavaScript/Ajax Applications Building a web part that takes advantage of Extender technology to provide richer interaction styles, such as autocomplete on a textbox. Using an UpdatePanel in your web part or control for more fluid, no postback interaction. (this will require some workarounds, however.)

Adding Microsoft ASP.NET AJAX Technology to SharePoint Pages

To extend your SharePoint site with Microsoft ASP.NET AJAX 1.0, you’ll need to perform a few steps.

First, you will need to download and install ASP.NET AJAX on servers in your farm.

Second, you need to extend web.config with some settings to enable ASP.NET AJAX technology.

Third, you will need to add the ASP.NET AJAX Script Manager into your master page to enable scenarios such as Extenders or UpdatePanels.

Installing ASP.NET AJAX on servers in your farm You will want to install the full "ASP.NET 2.0 AJAX Extensions 1.0" from Download AJAX Framework.

Extending SharePoint web.config files with Microsoft ASP.NET AJAX 1.0 Extending SharePoint web.config files with ASP.NET AJAX requires that you interleave some Ajax registration entries in-line with WSS registration entries. To do this you will need to edit your SharePoint web.config file, typically in a directory like c:\inetpub\wwwroot\wss\virtualdirectories\80.

1. Add a element to the tag:

<configsections />
    <sectiongroup name="""system.web.extensions"""
       type="""System.Web.Configuration.SystemWebExtensionsSectionGroup,""
        version=""1.0.61025.0,"" culture=""neutral,""
        publickeytoken=""31bf3856ad364e35""" />
      <sectiongroup name="""scripting"""
       type="""System.Web.Configuration.ScriptingSectionGroup,""
       version=""1.0.61025.0,"" culture=""neutral,""
       publickeytoken=""31bf3856ad364e35""" />
          <section name="""scriptResourceHandler""" 
           type="""System.Web.Configuration.ScriptingScriptResourceHandlerSection,"" 
           version=""1.0.61025.0,"" culture=""neutral,""
           publickeytoken=""31bf3856ad364e35""" requirepermission="""false"""
           allowdefinition="""MachineToApplication"/"" />
        <sectiongroup name="""webServices"""
           type="""System.Web.Configuration.ScriptingWebServicesSectionGroup,""
           version=""1.0.61025.0,"" culture=""neutral,""
           publickeytoken=""31bf3856ad364e35""" />
          <section name="""jsonSerialization"""
           type="""System.Web.Configuration.ScriptingJsonSerializationSection,""
           version=""1.0.61025.0,"" culture=""neutral,""
           publickeytoken=""31bf3856ad364e35""" requirepermission="""false"""
           allowdefinition="""Everywhere""" />
          <section name="""profileService"""
           type="""System.Web.Configuration.ScriptingProfileServiceSection,""
           version=""1.0.61025.0,"" culture=""neutral,""
           publickeytoken=""31bf3856ad364e35""" requirepermission="""false"""
           allowdefinition="""MachineToApplication""" />
          <section name="""authenticationService"""
           type="""System.Web.Configuration.ScriptingAuthenticationServiceSection,""
           version=""1.0.61025.0,"" culture=""neutral,""
           publickeytoken=""31bf3856ad364e35""" requirepermission="""false"""
           allowdefinition="""MachineToApplication""" />
        </sectiongroup />
      </sectiongroup />
    </sectiongroup />
</configsections />
2. Add a section as a child of the / tag:
<pages />
      <controls />
        <add assembly="""System.Web.Extensions,"" namespace="""System.Web.UI"""
         tagprefix="""asp""" version=""1.0.61025.0,"" culture=""neutral,""
         publickeytoken=""31bf3856ad364e35"/"" />
      </controls />
</pages />
3. Add the following tag to the tag, within :
<assemblies /> 
       <add assembly="""System.Web.Extensions,"" version=""1.0.61025.0,""
        culture=""neutral,"" publickeytoken=""31bf3856ad364e35"/"" />
</assemblies />
4. Add some new registrations to the end of the httphandlers section:
<httphandlers />
      <add type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,
       Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       validate="false" path="*.asmx" verb="*" />
      <add type="System.Web.Script.Services.ScriptHandlerFactory,
       System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
       PublicKeyToken=31bf3856ad364e35" validate="false" path="*_AppService.axd"
       verb="*" />
      <add type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions,
       Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       validate="false" path="ScriptResource.axd" verb="GET,HEAD" />
</httphandlers />
5. Add a new registration to the HttpModules section, beneath any existing registrations:
<httpmodules /> 
      <add name="""ScriptModule""" type="""System.Web.Handlers.ScriptModule,""
       version=""1.0.61025.0,"" culture=""neutral,""
       publickeytoken=""31bf3856ad364e35"/"" />
</httpmodules />
6. Add a SafeControl entry for the System.Web.UI namespace from Microsoft Ajax Extensions, within the /section:
<safecontrols /> 
      <safecontrol assembly="""System.Web.Extensions,"" namespace="""System.Web.UI"""
       version=""1.0.61025.0,"" culture=""neutral,""
       publickeytoken=""31bf3856ad364e35""" typename="""*""" safe="""True""" />
</safecontrols />
7. Finally, add the following configuration tags at the bottom of web.config, near the bottom before the end tag:
  system.webServer>
    validation validateIntegratedModeConfiguration="false"/>
    modules>
      <add name="""ScriptModule""" type="""System.Web.Handlers.ScriptModule,""
       version=""1.0.61025.0,"" culture=""neutral,""
       publickeytoken=""31bf3856ad364e35"/"" precondition="""integratedMode""" />
    </modules />
    <handlers />
      <remove name="""WebServiceHandlerFactory-Integrated""" />
      <add name="""ScriptHandlerFactory"""
       type="""System.Web.Script.Services.ScriptHandlerFactory,""
       path="""*.asmx""" verb="""*""" version=""1.0.61025.0,"" culture=""neutral,""
       publickeytoken=""31bf3856ad364e35"/"" precondition="""integratedMode""" />
      <add name="""ScriptHandlerFactoryAppServices"""
       type="""System.Web.Script.Services.ScriptHandlerFactory,""
       path="""*_AppService.axd""" verb="""*""" version=""1.0.61025.0,""
       culture=""neutral,"" publickeytoken=""31bf3856ad364e35"/""
       precondition="""integratedMode""" />
      <add name="""ScriptResource"""
       type="""System.Web.Handlers.ScriptResourceHandler,""
       path="""ScriptResource.axd""" verb="""GET,HEAD""" version=""1.0.61025.0,""
       culture=""neutral,"" publickeytoken=""31bf3856ad364e35"""
       precondition="""integratedMode""" />
    </handlers />
</system.webserver />

Refrence:You can also check this blog @msdn for exact entries -BLOG

Adding a ScriptManager into a SharePoint MasterPage

Many components of ASP.NET AJAX require the inclusion of a .NET ScriptManager control within a page.

Although it may be possible in some cases to dynamically insert a script manager from within a control, in many cases the control may not be able to insert the script manager early enough in the page lifecycle depending on how the control is used, making this tricky to get right. Also, the control implementer will need to ensure that multiple instances of their control (or other controls) do not result in the addition of multiple script managers within the page. For these reasons, dynamic insertion of a ScriptManager control from another control is not recommended.

To statically embed a script manager into a page, it is recommended that you add the ScriptManager into the master page of a site.

To do this, open up the master page for your site. Typically, this will be located at /_catalogs/masterpage. You can edit this file by opening it in an editor such as Microsoft SharePoint Designer, or directly in Notepad by opening your master page library via DAV (typically \\server\\_catalogs\masterpage.)

Add the following into the markup of your page. A recommended location is right beneath the WebPartManager registration (search for

  (WebPartPages:SPWebPartManager id="m" runat="Server" ): 

    (asp:ScriptManager runat="server" ID="ScriptManager1")(/asp:ScriptManager)

Using the code

The web parts uses the “SharePoint Object Model” to query the information. The web parts is bundled as “Site Tree” and “Site Dashboard” components. Site Tree webpart implements the IProvider Interface and provide the data to the Site Dashboard webpart which implements the IConsumer. Both the webparts query SharePoint using the object model.

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            this.EnsureUpdatePanelFixups();
            UpdatePanel up = new UpdatePanel();
            ScriptManager scriptManager = new ScriptManager();
            scriptManager.ID = "scriptManager";
            scriptManager.AsyncPostBackTimeout = 180;
            up.ID = "UpdatePanel1";
            up.ChildrenAsTriggers = true;
            up.UpdateMode = UpdatePanelUpdateMode.Conditional;
            
            sptv = new SPTreeView();
            bool check = this.Page.IsPostBack;
            GetSiteCollection(); //Function to Fill all Site Collection Only..

            sptv.SelectedNodeChanged += new EventHandler(sptv_SelectedNodeChanged);
            sptv.TreeNodeExpanded += new TreeNodeEventHandler(sptv_TreeNodeExpanded); 
            up.ContentTemplateContainer.Controls.Add(sptv);
            this.Controls.Add(scriptManager);
            this.Controls.Add(up);
        }

        

        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.OpenWeb();
                rootnode = new TreeNode(web.Url);
                if (web.Webs.Count > 0)
                {
                    TreeNode emptyNode = new TreeNode();
                    rootnode.ChildNodes.Add(emptyNode);
                }
                sptv.Nodes.Add(rootnode);
            }
        }

To ensure not refreshing the page (PostBack), we have to register our startup Script:

        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);
        }

Now we want to set the URL which we are sending from consumer to provider.For this we will set this URL in the constructor of the web part.

        public AJAXWebPart()
        {
            _table = new DataTable();

            DataColumn col = new DataColumn();
            col.DataType = typeof(string);
            col.ColumnName = "Url";
            _table.Columns.Add(col);

            DataRow row = _table.NewRow();
            row["Url"] = string.Empty;
            _table.Rows.Add(row); 
           
        }
        [ConnectionProvider("Row")]
        public IWebPartRow GetConnectionInterface()
        {
            return this;
        }
        public PropertyDescriptorCollection Schema
        {
            get
            {
                return TypeDescriptor.GetProperties(_table.DefaultView[0]);
            }
        }
        public void GetRowData(RowCallback callback)
        {
            callback(_table.Rows);
        }

Points of Interest

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. However, in a few cases there are some compatibility issues between ASP.NET AJAX and SharePoint which are anticipated to be addressed in the first service pack of Windows SharePoint Services.

History

AJAXConnectableWebPart v1.0.0.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here