How to use LINQ in SharePoint

Source: Internet
Author: User
Tags command line prepare reference tostring

LINQ is an efficient and efficient way to query data. With Spmetal you can prepare a SharePoint list for LINQ. Here's how to prepare your list and create an application that uses LINQ.

1. Open the command line (run as Administrator).

2. Navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin and enter the following command (make sure to replace it with your own server name)

Spmetal.exe/web:http://<servername>/code:spentitymodel.cs/language:csharp

3. The Spmetal command tool creates a C # file SPEntityModel.cs, which you can use to perform a LINQ query on the SharePoint list model in your application (Spmetal primarily translates all the lists in the site into an entity model).

4. Admin Identity Open vs.

5. New blank SharePoint Project Myfirstsplinqproject, click OK, deploy as farm solution, complete.

6. Right-click the project to add an existing item to find the SPEntityModel.cs created.

7. Right-click the item to add a new item, select Web Part, name Mysplinqwebpart. Click Add.

8. Right-click the reference and select Add Reference. Click Browse to navigate to the folder C:\Program Files\Common Files\Microsoft Shared\Web server Extensions\14\ ISAPI. Select Microsoft.SharePoint.Linq.dll, click OK.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/web/sharepoint/

9. Right-click MySPLinqWebPart.cs to view the code.

10. Modify Code:

Using System;
Using System.ComponentModel;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using Microsoft.SharePoint;
Using Microsoft.SharePoint.WebControls;
Using Microsoft.SharePoint.Linq;
Using System.Linq;
namespace Myfirstsplinqproject.mysplinqwebpart {[ToolboxItemAttribute (false)] public class Mysplinqwebpart:webpart {
Label MyLabel = new label ();
ListBox listtitles = new ListBox ();
Button MyButton = New button ();
protected override void CreateChildControls () {mylabel.text = "Lists:";
Mybutton.text = "Get Lists"; This.
Controls.Add (MyLabel); This.
Controls.Add (Listtitles); This.
Controls.Add (New LiteralControl ("<br/>")); This.
Controls.Add (MyButton);
Mybutton.click + = new EventHandler (MyButton_Click); } void MyButton_Click (object sender, EventArgs e) {//be sure to update the server reference//below to point to your serv
Er. using (spentitymodeldatacontext DataContext = new Spentitymodeldatacontext ("http:smallville-pc:1528 ")) {var salesinfo = from data in datacontext.customers select data; foreach (Var salesitem in Salesi
NFO) {listTitles.Items.Add (salesItem.Title.ToString () + "|" + salesItem.CustomerType.ToString ());} }
}
}

11. Modify the. WebPart File code:

<?xml version= "1.0" encoding= "Utf-8"?>
<webParts>
<webpart xmlns= "http:// Schemas.microsoft.com/webpart/v3 ">
<metaData>
<type name=" MyFirstSPLinqProject.MySPLinqWebPart.MySPLinqWebPart, $SharePoint. project.assemblyfullname$ "  />
< importerrormessage> $Resources:core,importerrormessage;</importerrormessage>
</metaData>
<data>
<properties>
<property name= "Title" type= "string" >customer types</ property>
<property name= "Description" type= "string" > Web part that lists
customers and customer Types (using SP LINQ).
</property>
</properties>
</data>
</webPart>
</webParts>

12. When finished, deploy the solution.

13. Add this Web Part.

14. Click Get lists to obtain a list of corresponding customers and types.

Screenshot of this machine:

PS: This article is related to the previous blog http://blog.csdn.net/crazygolf/article/details/30710779, please complete this exercise first.

Author: csdn Blog Zhang Shihui

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.