Thursday 30 May 2013

Accessing SharePoint 2010 List Data by Using LINQ to SharePoint

Overview
Using the LINQ to SharePoint provider is a way to add and read items from a Microsoft SharePoint 2010 list. The technique described in this Visual How To requires running the SPMetal utility to create a custom data-context class and entity classes. By using these classes, you can access SharePoint list data in a strongly typed manner.
Code It
To use the LINQ to SharePoint provider to access data in a SharePoint 2010 list, you must generate entity classes by using a command-line tool named SPMetal (SPMetal.exe). When running the SPMetal utility from the command prompt, you must pass three parameters: the URL to the site that contains the list, a namespace that is used to create the entity classes, and the name of an output file. (Enter the entire command as a single line. Line breaks in the following example are for readability only.)
SPMETAL.EXE /web:http://intranet.wingtip.com 
            /namespace:WingtipIntranet 
            /code:WingtipIntranet.cs
Displaying List Items by Using an ASP.NET DataGrid
You can use a Microsoft ASP.NET DataGrid control to display the data that is returned from a query. If you set the AutoGenerateColumns attribute to true, the DataGrid control can automatically create a grid column for each field in the result set.
<asp:DataGrid 
  ID="grdMain" 
  runat="server" 
  AutoGenerateColumns="true" />
The data-context class represents a connection and, consequently, must be initialized by using the URL of a target site. After you create an instance of the data-context class, each list in the site is exposed as a property, which enables you to write a LINQ statement with strongly typed access to list colums. With this capability, you can easily execute a query against a SharePoint list and bind the output result set to the ASP.NET DataGrid control.
WingtipIntranetDataContext dc =
  new WingtipIntranetDataContext(SPContext.Current.Web.Url);

protected override void OnPreRender(EventArgs e) {

  var query = from DevelopersItem dev in dc.Developers
              where dev.SharePointDeveloper.Equals(true)
              orderby dev.Title, dev.FirstName
              select new {
                FirstName=dev.FirstName,
                LastName=dev.Title,
                SPDev=dev.SharePointDeveloper.ToString()
              };

  grdMain.DataSource = query;
  grdMain.DataBind();
}
Adding New List Items
The data-context and entity classes created by SPMetal provide a simple approach to adding a new list item. SPMetal generates an entity class for each list in a target site. If a site has a list named "Developers", SPMetal generates an entity class named DevelopersItem that has a set of properties that map to the list columns. You simply create and initialize an entity class instance. Then call the InsertOnSubmit method followed by the SubmitChanges method.
protected void cmdAddDeveloper_Click(object sender, EventArgs e) {
  DevelopersItem newDev =
    new DevelopersItem {
      FirstName = txtFirstName.Text,
      Title = txtLastName.Text,
      SharePointDeveloper = chkSharePointDeveloper.Checked
  };

  dc.Developers.InsertOnSubmit(newDev);
  dc.SubmitChanges();
}
Read It
SharePoint 2010 introduces a new LINQ provider named LINQ to SharePoint, which enables you to access SharePoint list data by using LINQ programming techniques. Using the LINQ to SharePoint provider is optional, but this approach has several benefits when compared to older techniques for querying and updating list items, such as using the SharePoint object model or the CAML programming language:
  • LINQ to SharePoint eliminates the need to deal with CAML.
  • Your code has strongly typed access to the columns of a list item.
  • Microsoft Visual Studio provides IntelliSense information based on list columns as you write LINQ statements in C# or Microsoft Visual Basic.
  • Queries in LINQ are significantly easier to write and understand than queries in CAML.
To use the LINQ to SharePoint provider, follow these general steps:
  1. Run SPMetal against the target site to generate entity classes.
  2. In Visual Studio, add the source file that was generated by SPMetal to your project.
  3. Add a project reference to Microsoft.SharePoint.Linq.
  4. Program against the generated data-context and entity classes

No comments:

Post a Comment