Sunday 17 February 2013

calling REST with Jquery from sharepoint 2010

I will show how to call REST service with JQUERY to fetch data from sharepoint list.

I have one sharepoint list called Employee1 which has 2 columns(Ename and Contact).

Include these 2 scripts:

<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
Next write following code- 

<script type="text/jscript" language="jscript">$(document).ready(function () {


$.getJSON('http://URL/_vti_bin/ListData.svc/Employee1', function (data) {
//alert(data);
var count = 0;
$.each(data.d.results, function (i, result) {
//alert(result);
var Ename= data.d.results[i].Ename
var Contact= data.d.results[i].Contact;
html = "<table border=’1′ style=’float: left’><tr><td style=’color:blue’>" + Ename+ "</td><td style=’color:blue’>" + Contact+ "</td></tr></table>";
//html1 = "<table border=’1′ style=’float: left’><tr><td style=’color:blue’>" + title + "</td></tr></table>";
$('#resultarea').append($(html));
});
});
});

</script>

Tuesday 5 February 2013

Working with Nested Repeater in Asp.net

Hi today I am writing on how to implement nested repeater.

I have taken 2 tables.
One is Emp{Eid(PK),Ename,Econt} and Book{Bid(PK),Bname,Eid(FK to EMP)}

So make these 2 tables in Sql Server database.

I want to show which employee has how many books under him/her.

Now I have taken 2 repeaters.below is the HTML

<form id="form1" runat="server">
<div><asp:Repeater ID="repParent" runat="server" OnItemDataBound="repParent_ItemDataBound">
<ItemTemplate ><table>
<tr>
<td>
<asp:HiddenField ID="hdnID" runat="server" Value=' <%# Eval("Eid") %>' /></td>
</tr><tr><td><b>Name :</b><asp:Label ID="lblEname" runat="server" Text=' <%# Eval("Ename") %>' /><br /></td><tr><td>------------------</td></tr></tr><tr><td><asp:Repeater ID="repChild" runat="server"><ItemTemplate><asp:Label ID="lblBname" runat="server" Text=' <%# Eval("Bname") %>' /><br /></ItemTemplate></asp:Repeater></td></tr>
</table></ItemTemplate><SeparatorTemplate>
</SeparatorTemplate></asp:Repeater></div></form>
Now on code behind paste following code.

namespace nestedRepeater{

  public partial class WebForm1 : System.Web.UI.Page  {  protected void Page_Load(object sender, EventArgs e)    {

    if (!IsPostBack)  
      {
      BindParentRep();
       }
    }

private void BindParentRep(){

string connStr = @"Data Source=your server name; initial catalog=Sales;integrated security=true";
SqlConnection conn = new SqlConnection(connStr);
string query = "select * from dbo.Emp";
SqlDataAdapter ad = new SqlDataAdapter(query,conn);
DataSet ds = new DataSet();ad.Fill(ds);
repParent.DataSource = ds;
repParent.DataBind();

}



protected void repParent_ItemDataBound(object sender, RepeaterItemEventArgs e){

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){

Repeater child = (Repeater)e.Item.FindControl("repChild");
HiddenField hdnId = (HiddenField)e.Item.FindControl("hdnID"); //Based on this Emp id I will //find all corresponding books.
if (hdnId.Value != null){

string connStr = @"Data Source=your server name; initial catalog=Sales;integrated security=true";
SqlConnection conn = new SqlConnection(connStr);
string query = "select Bname from dbo.Book where Eid=@Eid";
SqlDataAdapter ad = new SqlDataAdapter(query, conn);
SqlCommand cmd = new SqlCommand(query, conn);conn.Open();
cmd.Parameters.AddWithValue(
"@Eid", hdnId.Value);
SqlDataReader dr = cmd.ExecuteReader();child.DataSource = dr;
child.DataBind();
conn.Close();
}
}
}
}
}

After this press F5 to run.