SP Query and LINQ Operations on Sharepoint List

// Row Limit in SP Query

public void SPRowLimit()
{
using (SPWeb oWebsite = siteCollection.OpenWeb())
{
SPList oList = oWebsite.Lists[“Country List”];

SPQuery oQuery = new SPQuery();

// Select the needed fields
oQuery.ViewFields = “”;

// Limit the number of rows in the selection
oQuery.RowLimit = 2;

SPListItemCollection collListItems = oList.GetItems(oQuery);

foreach (SPListItem oListItem in collListItems)
{
Response.Write(Convert.ToString(oListItem[oList.Fields.GetField(“Title”).InternalName]) + “”);
}
}
}

// Query files inside folder in a document library

public void SPView()
{
using (SPWeb oWebsite = siteCollection.OpenWeb())
{
SPList oList = oWebsite.Lists[“Yearly Report”];
SPView oView = oList.Views[“All Documents”];

SPQuery oQuery = new SPQuery(oView);

// To explore files inside a folder
oQuery.ViewAttributes = “Scope=\”Recursive\””;

SPListItemCollection collListItemsAvailable =
oList.GetItems(oQuery);

foreach (SPListItem oListItemAvailable in collListItemsAvailable)
{
Response.Write(oListItemAvailable[“Name”] + “”);
}
}

}

// Get Custom List Data using LINQ

public void ListusingLinQ()
{
SPSite site = new SPSite(“http://praveenkumar:5555”);
SPWeb web = site.OpenWeb();

SPList lstCustomer = web.Lists[“Country List”];

var customers = from SPListItem customer in lstCustomer.Items
select customer;

foreach (SPListItem item in customers)
{
Response.Write(item.ID.ToString() + “-” + item[“Title”].ToString());
}

}

// Get Multiple Custom List Data using LINQ

public void MultipleListValueusingLinq()
{
SPSite site = new SPSite(“http://praveenkumar:5555”);
SPWeb web = site.OpenWeb();

SPList lstCustomer = web.Lists[“Customers”];
SPList lstorders = web.Lists[“Order”];

var customerorders =

from SPListItem customer in lstCustomer.Items
join SPListItem order in lstorders.Items on customer.ID.ToString() equals order[“CustomerID”].ToString().Split(new string[] { “;#” }, StringSplitOptions.None)[0]

select new { Title = customer[“Title”].ToString(), OrderDate = order[“OrderDate”].ToString() };

foreach (var item in customerorders)
{
Response.Write(item.Title + “-” + item.OrderDate);
}
}

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: