Objective In this article, we will see how to work with LINQ to SharePoint. I have tried to address all the common errors we encounter when we start using LINQ against SharePoint.We will see:
namespace SPLinqTest1{ class Program { static void Main(string[] args) { ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1"); //EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product"); var res = from r in context.Test1_Product select r; foreach (var r in res) { Console.WriteLine(r.ProductId + ":" + r.ProductName + ":" + r.ProductPrice); } Console.ReadKey(true); } }}OutputInsert to SharePoint list using SPLinq.
Create the instance of Data Context
Get the entity list where item would get inserted
Create instance of List item to be inserted
Call the InsertOnsubmit on the instance of Entity list.
Call sumitchange on the context
Code for inserting a list item :ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1"); EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product"); Test1_ProductItem itemToInsert = new Test1_ProductItem() { ProductId="9", ProductName ="Soccer Ball", ProductPrice =600}; products.InsertOnSubmit(itemToInsert); context.SubmitChanges();Update a Particular list item in SharePoint list
Create instance of Data Context
Fetch the list item to be updated using SPLInq
Modify the list item property wished to be updated. I am updating Product name to Dhananjay of Product Id 1
Call the submit change on the context Code for updating a list item ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1"); var itemToUpdate = (from r in context.Test1_Product where r.ProductId == "1" select r).First(); itemToUpdate.ProductName = "Dhananjay"; context.SubmitChanges();
Delete a Particular item from the list
Fetch the list item to be deleted using SPLInq
Call deleteonsubmit to delete a particular item from SharePoint list
Call the submit change on the context Code for deleting a list item ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1"); EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product"); var itemToDelete = (from r in context.Test1_Product where r.ProductId == "1" select r).First(); products.DeleteOnSubmit(itemToDelete); context.SubmitChanges();
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: