Monday, August 10, 2009

Adding Web User Control with Ajax in SharePoint

you have create an user control from an web application right. Fine,

First of all you need to make some necessary changes in your site web.config for AJAX.
http://zieglers.wordpress.com/2009/02/02/webconfig-entries-for-enabling-ajax-for-sharepoint-2007

above link will help you for configuring your web.config. (Remember you need to make a copy of your web.config before you change do any modifications on your web.config)

Also remember you if you try to copy the web.config tags from the above Article which i suggest you need to put (double quotes) " symbol where you see " because, on this above site it shows you images only like ”.

Also remember one more thing, if you need to add AJAX in one page or for one web part means you don't need to add

<asp:scriptmanager runat="”server”" id="”ScriptManager1"">

in the master page, just add the above tag on the your user control, its enough. If you want to make AJAX Functionality across your site means you need to add the above tag on the master page as which the above article mentioned.

Then copy AjaxControlToolKit.dll and past in the Bin folder of your site
C:\Inetpub\wwwroot\wss\VirtualDirectories\111\bin

You need to follow my following steps,

1) Create a web application project. then create an web user control and design your user control page as you need with AJAX controls.

2) After User Control creation is over you need to include the following method in your user control code window and call this method in the page load method.

#region Page Load
protected void Page_Load(object sender, EventArgs e)
{ //Ajax Script Initialisation
EnsureUpdatePanelFixups();
}
#endregion

#region Ajax Update Pannel FixUps
private void EnsureUpdatePanelFixups()
{
this.Page.Title = "Here Type your page title name";
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(UpdatePanel), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;", true);
}
#endregion


(Remember here am using the Update Panel)

3) After creation of your web control is over you need to create a folder as UserControls in the site as

C:\Inetpub\wwwroot\wss\VirtualDirectories\111\UserControls
and copy paste your webusercontrol on this Folder.

4) Build your web application project. If there is no error the Copy your web application dll from your web application's Bin folder to your site Bin folder C:\Inetpub\wwwroot\wss\VirtualDirectories\111\bin

5) Also you need to make one more change in your web.config,
you need to register your web application dll in the site as follows


<safecontrol assembly="" Namespace="AjaxControlToolKit dll name" TypeName="*" Safe="True">

<safecontrol assembly="Your Dll name" Namespace="Your Dll name" TypeName="*" Safe="True">


6) Now you need to build your web application to the site Bin folder, for this you need to do following steps,
a) Right Click on your web application Solution Explorer then you can see Properties (Menu Item), Click on it.
b) On the left side you can see Build, Click on Build on the Properties window.
c) On the Output Path you need to set the output path as your web site bin folder. (here my site port number as 111)
C:\Inetpub\wwwroot\wss\VirtualDirectories\111\bin\
d) After setting the output path, make Build your web application.
e) Remember if you make any change in your web application code or design you need to make a build and also copy paste your web user control to your site User control folder which you have created before.

7) now you need to install a smart part ( which is a web part which is user to load user controls to your site) which is just like other web parts install in the site.

Try it,
I think its enough for you, if you have any doubts on this be feel free to ask me at any time.