SharePoint 2010 Makes the Link to LINQ : Page 2

For writing the LINQ queries, add the reference to "Microsoft.SharePoint.Linq.dll." This assembly is located inside this directory:




"<Drive-Letter>:\Program Files\Common Files\Microsoft Shared\

Web Service Extensions\14\ISAPI", where "<Drive-Letter>"
is the drive where SharePoint is installed. See the following image.

5. Use LINQ to query the "Customers" List, see the following code fragment.

And the call from "Main" method looks like this; the only parameter passed is the web URL.

Let's take a look at the "DisplayCustomerListData" method; first it created an object of type "SharePointDataContext". This is the class generated by "SPMETAL", the constructor takes the URL, and this is the same URL that is used in "SPMETAL" while generating the source file.

Once the object is created, LINQ queries can be constructed. The sample query selects all the customer list items, provided that "Name" field is not empty. Also, the ordering of the customers is done based on the "Name" column.

The "results" variable contains the "Customers" resulted from the query, the "foreach loop" is used to display the "Customers" list items as a result of the LINQ query. Compile and run the application, it should list the "customers" name and address. Following image shows the output, and as there is only one customer item in the list, it's the only item displayed.

6. Next use LINQ to SharePoint to insert a new list item inside the "Customers" list. Create a method called "InsertItemInCustomerList" as shown in figure below.

Also update the Main method as follows; this is to make sure that item is inserted first and then, all the customers are displayed, including the newly added customer list item.

The method "InsertItemInCustomerList" takes the URL of the web, the same as the previous method "DisplayCustomerListData". It then creates an object of type "SharePointDataContext", which is also the same as the previous method. In order to create a new list item of type "Customer", create a new object of type "CustomersItem"; this object will be the list item along with columns like Name, Address etc. One of the benefits of using LINQ is that fields are all strong types, so compile-type checking along with Visual Studio itellisense is present throughout the development.

After setting the values of the fields, make a call to "I;sertOnSumbit" method, and passes the newly created "customer" object of type "CusomterItems" to it. Lastly call the method "SubmitChanges" which will actually update the SharePoint 2010 content database with the newly created list item.

Compile and run the application. First the "InsertItemInCustomerList" is called and then "DisplayCustomerListData", which will now display two items as shown below.

The newly created item in the Customers list is shown below.

12
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Networking Solutions

Partners

More Networking