Sunday, 3 June 2012

Display Sharepoint list in 4.0

Step 1)   Create an ASP.NET Empty WEB Site
STEP 2)  Add aspx page called default.aspx
Step 3)  Add Service Reference
Enter WCF REST Service as   http://sp2010:22913/sites/sc2/_vti_bin/ListData.svc
             bold faced text shows Website URL, Change according to ur requirement,

Click OK,.  ServiceReference1 will be added as shown below

Step 4) Just for Reference This site in this case http://sp2010:22913/sites/sc2/  has "sales data" list, as shown in the below screen.

Step 5)   Add datagrid to  default.aspx page

    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server">

Step 6)  in default.aspx Page_Load method add following code
    protected void Page_Load(object sender, EventArgs e)
       //Create SiteTitle Datacontext Object
       //in this case Bizone is site title, so datacontext is BixoneDataContext

        ServiceReference1.BizoneDataContext dc = new ServiceReference1.BizoneDataContext(
new Uri("http://sp2010:22913/sites/sc2/_vti_bin/ListData.svc/"));
        //Assuming  user running in the same machine/network where sharepoint hosted.
        //otherwise provide username & password.
        dc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

        //Shown in Step 4, sharepoint site has sales details list
        var expr = from sd in dc.SalesDetails
                   select sd;

        //Bind sharepoint list to DataGrid
        GridView1.DataSource = expr;