Friday, March 12, 2010

Creating Ajax webparts

1)Download ASP.NET AJAX 1.0.

2)First of all you need to add

· System.Web
· System.Web.Extensions (v1.06)
· System.Web.Extensions.Design (v1.06)
. Syste.Data

3)Code:
using System;
using System.Web;

using System.Data;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace AjaxWebpart
{
[Guid("0eebf43e-a235-480b-a6df-54269c30892b")]
public class AjaxWebpart : System.Web.UI.WebControls.WebParts.WebPart
{
public AjaxWebpart()
{
this.ExportMode = WebPartExportMode.All;
}
private GridView grdSearch;
private TextBox txtSearch;
private Button btnSearch;
private Label lblMsg;
private UpdatePanel updatePanel;
private UpdateProgress updateProgress;
private static readonly object EventSubmitKey = new object();

//protected override void Render(HtmlTextWriter writer)
//{
// // TODO: add custom rendering code here.
// // writer.Write("Output HTML");
//}
[WebBrowsable(true), WebDisplayName("Image URL"), WebDescription("Image URL"), Personalizable(PersonalizationScope.Shared)]
public string LoadingImageURL
{
get
{
object _obj = ViewState["LoadingImageURL"];
if (_obj == null)
{
return string.Empty;
}
else
{
return (string)ViewState["LoadingImageURL"];
}

}
set { ViewState["LoadingImageURL"] = value; }
}
public event EventHandler Submit
{
add { Events.AddHandler(EventSubmitKey, value); }
remove { Events.RemoveHandler(EventSubmitKey, value); }
}
private void btnSearch_Click(object source, EventArgs e)
{

grdSearch.Visible = false;

System.Threading.Thread.Sleep(1500);

////Create DataTable Structure
DataTable Dtable = new DataTable("tmpTable");
////User_ID Col
Dtable.Columns.Add("User_ID", typeof(int));
Dtable.Columns["User_ID"].AutoIncrement = true;
Dtable.Columns["User_ID"].AutoIncrementSeed = 1;
////User_Name Col
Dtable.Columns.Add("User_Name", typeof(string));
////User_Department Col
Dtable.Columns.Add("User_Department", typeof(string));

DataRow dr = default(DataRow);

dr = Dtable.NewRow();
dr["User_Name"] = "Madhu Nomula";
dr["User_Department"] = "Sharepoint";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "Swathi Nomula";
dr["User_Department"] = "Technical";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "chinna Nomula";
dr["User_Department"] = "Production";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "Chinni Nomula";
dr["User_Department"] = "Technical";
Dtable.Rows.Add(dr);

string searchFor = "%";

if (txtSearch.Text != string.Empty)
{
searchFor = txtSearch.Text;
}

DataView dtView = new DataView(Dtable);
dtView.RowFilter = "User_Name LIKE '%'+'" + searchFor + "'+'%'";

if (dtView.Count > 0)
{
grdSearch.Visible = true;
grdSearch.AutoGenerateColumns = true;
grdSearch.DataSource = dtView;
grdSearch.DataBind();
lblMsg.Text = string.Empty;
}
else
{
lblMsg.Text = "Sorry, No Data Found!.";
grdSearch.Visible = false;
}


}



protected override void CreateChildControls()
{
Controls.Clear();

updatePanel = new UpdatePanel();
{
updatePanel.ID = "UpdatePanel1";
updatePanel.ChildrenAsTriggers = true;
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
}

updateProgress = new UpdateProgress();
updateProgress.ID = "UpdateProgress1";

string templateHTML = null;
if (LoadingImageURL == string.Empty)
{
templateHTML = "Loading ...";
}
else
{
templateHTML = "
'Loading...'
";
}

updateProgress.ProgressTemplate = new ProgressTemplate(templateHTML);

updateProgress.AssociatedUpdatePanelID = updatePanel.ClientID;

this.Controls.Add(updatePanel);

grdSearch = new GridView();
grdSearch.ID = "GrdSearch";

txtSearch = new TextBox();
txtSearch.ID = "txtSearch";

btnSearch = new Button();
btnSearch.Text = "Search";
btnSearch.ID = "BtnSearch";

lblMsg = new Label();
lblMsg.ID = "lblMsgError";

btnSearch.Click += btnSearch_Click;

updatePanel.ContentTemplateContainer.Controls.Add(txtSearch);
updatePanel.ContentTemplateContainer.Controls.Add(btnSearch);
updatePanel.ContentTemplateContainer.Controls.Add(grdSearch);
updatePanel.ContentTemplateContainer.Controls.Add(lblMsg);
updatePanel.ContentTemplateContainer.Controls.Add(updateProgress);

}


protected override void OnInit(EventArgs e)
{

base.OnInit(e);

//get the existing ScriptManager if it exists on the page
ScriptManager AjaxManager = ScriptManager.GetCurrent(this.Page);
if (AjaxManager == null)
{

//create new ScriptManager and EnablePartialRendering
AjaxManager = new ScriptManager();
AjaxManager.EnablePartialRendering = true;

//Fix problem with postbacks and form actions (DevDiv 55525)
Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID, "_spOriginalFormAction = document.forms[0].action;", true);

//tag:"form" att:"onsubmit" val:"return _spFormOnSubmitWrapper()"
//blocks async postbacks after the first one
//not calling "_spFormOnSubmitWrapper()" breaks all postbacks
//returning true all the time, somewhat defeats the purpose of the
//_spFormOnSubmitWrapper() which is to block repetitive postbacks,
//but it allows MS AJAX Extensions to work properly

if ((this.Page.Form != null))
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) & formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
//add the ScriptManager as the first control in the Page.Form
this.Page.Form.Controls.AddAt(0, AjaxManager);

}
}
}

protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
updatePanel.RenderControl(writer);
}

//Class for Building progress tempales
public class ProgressTemplate : ITemplate
{
private string template;

public ProgressTemplate(string temp)
{
template = temp;
}

public void InstantiateIn(Control container)
{
LiteralControl ltr = new LiteralControl(this.template);
container.Controls.Add(ltr);
}


}
}

}


--------------

4)Now Extend the SharePoint web.config file, which is typically found in a directory with the following structure: drive\inetpub\wwwroot\VirtualDirectories\port number.

Modify the web.config as per the settings in this url

http://msdn.microsoft.com/en-us/library/bb861898.aspx

Thursday, March 11, 2010

MOSS 2010( Sharepoint 2010) Key features

Introduction

Sharepoint is a product that is truly what you make of it, and has always been more platform than simple product. Companies who invested in proper customization and integration have been able to benefit from it much more than organizations who simply deployed Sharepoint and made it available out of the box.

Sharepoint 2010 is much more usable out of the box. The new ribbon interface is intuitive and familiar to Office users, and the product is much tighter, but the need for customization and integration remains. Luckily, Sharepoint 2010 is more focused than ever on being a platform for developers.

Microsoft announced about the SharePoint 2010 technical preview.

Key Features

  • New User Interface including new Ribbon
  • Web Edit
  • Silverlight Web Part
  • Rich Theming
  • Multiple Browser Support
  • Visio Services
  • SharePoint Designer
  • Business Connectivity Services (the evolution of the Business Data Catalog)
  • SharePoint Workspace
  • Rich Media Support
  • Central Administration

SharePoint 2010 is packed with exciting new features. Content authors, administrators and developers can all expect an improved experience.

Content Authoring Improvements

  • Improved WYSIWYG Editor (Web Edit with Live Preview)
  • Improved Theming
  • Silverlight Web Part
  • Ribbon Toolbar
Administration Improvements
  • Streamlined Central Administration
  • Best Practices Analyzer: analyzes farm health and can automatically fix common configuration errors out-of-the-box. Extensible and rules-based.
  • Unified Logging Database
  • Resource throttling for large lists and libraries

Development Improvements

  • Visual Studio 2010 Tools including a Package Designer and Web Part Editor
  • LINQ for SharePoint
  • Developer Dashboard: Page-level debugging/trace output
  • Business Connectivity Services (BCS) replaces Business Data Catalog (BDC) with SharePoint Designer 2010 and Visual Studio 2010 will provide BCS-specific tooling.
  • Client Object Model (OM) is a new SharePoint API that runs on the client and can be called from JavaScript, .NET, or Silverlight

Limitations

SharePoint 2010 has hardware and other limitations:

  1. SharePoint 2010 will only be available in a 64-bit version. This change is not limited to servers. Developers running on 32-bit hardware will need new machines.
  2. SharePoint 2010 will require a 64-bit version of SQL Server 2005 or 2008 (2000 is no longer supported).
  3. Not all applications written for SharePoint 2007 will work in 2010. Certain APIs will be no longer be available. Others will be deprecated.
  4. The basic flow for many tasks as changed. That, coupled with the laundry list of productivity-enhancing features means that any 2010 deployment should be accompanied by training for users, administrators, and developers.

Monday, March 8, 2010

Silverlight Integration with Moss 2007

What is Silver light?
Silver light is a new technology form Microsoft that enables developer to build rich, interactive user experience for web based applications.

It is a cross –browser, cross-platform plug-in that Microsoft developed to enable more compelling and interactive media based application experience on the web
It supports .Net 3.5(C#, LINQ, VB and XML serialization are also supported.), Ajax also more dynamic technologies like Phython, Java script and Ruby.

It also provides extensive library controls to customize , binding, change style templates…etc.

XAML—Extensible Application Markup Language
Silver light XAML is the subset of WPF(Windows Presentation Foundation ) XAML.
Using XAML you can layout your UI and then associate with your controls
XAML provides seleveral “structural” containers to position your controls

Ex: Canvas and add other controls to build complete UI or small part of UI.

When you are building your XAML in visual studio shows you the view that we can expect to work with it.
The view split in to Designer and XAML view.
You can Drag drop controls in to the XAML view and add properties in XAML view.
To use full-drag and drop you need to use Expression blend.—Which also provides rich set of tools to manipulate your silver light control.
Because Expression blend and Visual studios are integrated you can right click on Page.XAML to open in “Open in Expression Blend”.

Silver light integration with Moss:

Pre-requisites:
Windows server 2008/2003 Entp.
Windows sharepoint services 3.0 +wss sp1
Moss 2007 Enpt edition+Moss sp1
Visualstudios 2008 Professional edition above with SP1
Silverlight 2 tools for visual studios 2008sp1
Silverlight2 client runtime
Visual studio extensions for windows sharepoint services 3.0 versions 1.2
Expression Blend 2 SP1

After you have installed above you have to make sure that your development environment configured for Sharepoint and silver light integration.

1) Change MIME type in IIS to support silverlight application.
2) Change web.config file in your root sharepoint direction to support silver light application
3) Ensure that syste.web.silverlight .dll is added in your GAC.

After that

Follow the below basic steps:

1) Create silverlight web application Project VS 2008.
2) Create .xap file
3) Create new-->sharepoint webpart
4) Add reference of System.web.silverlight.DLL to this webpart
4) In child controls

System.web.UI.silverlightcontrols.silverlight test= new System.web.UI.silverlightcontrols.silverlight();

test.ID="testID";
test.source="http://xyz/xaps/silver.xap"; // URL of silver light application
test.width= new system.web.UI.webcontrols.unit(900);
test.Height= new system.web.UI.webcontrols.unit(650);

this.contols.ADD(test);

5) Deploy the webpart.

Wednesday, March 3, 2010

Creating a Web Part with Client-side Script (Java Script etc)

Calling Java script functions in webparts:



We can call Java script functions in sharepoint webparts in 2 ways
1) Embed a script in a Web Part
2) Linking a script file to webpart

1) Embed a script in a Web Part :
Method 1:
1) You can explicitly specify your script in a Web Part and have the script loaded only once for all instances of the same Web Part on a Web Part Page. The IsClientScriptBlockRegistered method of the System.Web.UI.Page class can check if the script has been loaded for the same Web Part Page.

private const string HelloIncludeScriptKey = "HelloIncludeScript";
private const string EmbeddedScriptFormat = "< script language=javascript >function Hello(){alert('Hello world');} </ script> ";

public Web_Part2()
{

this.PreRender += new EventHandler(Web_Part2_PreRender);

}

void Web_Part2_PreRender(object sender, EventArgs e)
{

if (!Page.IsClientScriptBlockRegistered(HelloIncludeScriptKey))
Page.RegisterClientScriptBlock(HelloIncludeScriptKey, EmbeddedScriptFormat);
//syntax: Page .RegisterClientScriptBlock(key of the script, ScriptFormat string);
}
Button btn;
protected override void Render(HtmlTextWriter writer)
{
btn.RenderControl(writer);
}
protected override void CreateChildControls()
{
btn = new Button();
btn.ID = "btn";
btn.Text = "Click";
btn.OnClientClick = "Hello()";//call the JS function
this.Controls.Add(btn);
}



Method 2:
Using StringBuilder:

protected override void Render(HtmlTextWriter writer)
{
btnclk.RenderControl(writer);
}
Button btnclk;
protected override void CreateChildControls()
{
//base.CreateChildControls();
btnclk = new Button();
btnclk.ID = "btnclk";
StringBuilder strscript = new StringBuilder();
strscript.Append("alert('hello world'); return false;");
btnclk .Attributes .Add ("onclick",strscript .ToString ());
}

Method 3:
Including “ < script> ” tags in writer.write in Render Method:
protected override void Render(HtmlTextWriter writer)
{
writer.Write("< script >");
writer.Write("function helloworld(){ alert('hello world'); return false;}");
writer.Write("</script >");
btnclk1.RenderControl(writer);

}
Button btnclk1;
protected override void CreateChildControls()
{
btnclk1 = new Button();
btnclk1.ID = "btnclk1";
btnclk1.Attributes.Add("onclick", "helloworld();");
}


2) Linking a script file to webpart:
//JS file name : hello.js

function hello()
{
alert('hello this is external script alert');
}

We need to place this js file in this path :

C:\Program Files\Common Files\Microsoft Shared\web server extensions\wpresources\EmployeeWebpart\1.0.0.0_91f29f0c96ff744c

EmployeeWebpart-----folder with webpart name
1.0.0.0_91f29f0c96ff744c---folder with version_public key token



// Referring External Javascript in Createchild controls method

ClientScriptManager cs = Page.ClientScript;
// Include the required javascript file.
if (!cs.IsClientScriptIncludeRegistered("hello"))
cs.RegisterClientScriptInclude(this.GetType(), "hello", "/_wpresources/EmployeeWebpart/1.0.0.0_91f29f0c96ff744c/hello.js");

//Test:
Testbutton = new Button();
Testbutton.Text = "Click me";
Testbutton.OnClientClick = "hello()"; // specify function name here