Monday, June 20, 2016

LINQ to SharePoint - SPMetal

In SharePoint, We can query list data using LINQ or CAML. Below example I had created windows application that uses CAML to SharePoint for quering SharePoint list data.  I had created empty SharePoint project with farm solution with button and Datagrid to display the data.

[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string siteUrl = "http://stapvsp9007.gcm.com/sites/testsite";
        private void LINQSP_Click(object sender, EventArgs e)
        {          
            var ctx = new SPTestList.SPTestListDataContext(siteUrl);
            var query = from p in ctx.Testcalendar
                        where p.Title == "Test567$"
                        select p;
            dataGridView1.DataSource = query.ToList();           
        }
    }
}
[/code]
Using SPMetal.exe I had created LINQ to SharePoint Model class file and then queried the data



Project Structure


Result:

No comments:

Post a Comment