SharePoint - query in sharepoint using vs2010 - Asked By Bookworm on 04-Nov-11 09:59 AM

Hi,

i need to create a simple query from a list

thnx
Kirtan Patel replied to Bookworm on 04-Nov-11 01:39 PM
Untitled document
you can use LINQ to query the Sharepoint lists
 
first you need to create LINQ->SQL classes for that you can use SQLMetal.exe tool
 
SPMetal.exe /web:http://localhost/ /code:C:\Projects.cs
 
public partial class ProjectsDataContext : Microsoft.SharePoint.Linq.DataContext {
 
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
 
public ProjectsDataContext(string requestUrl) : this(requestUrl, true) {
 
}
 
public ProjectsDataContext(string requestUrl, Boolean tracking) : base(requestUrl)
{
  this.OnCreated();
  ObjectTrackingEnabled = tracking;
}
 
[Microsoft.SharePoint.Linq.ListAttribute(Name="departments")]
public Microsoft.SharePoint.Linq.EntityList<Item> Departments {
    get {
      return this.GetList<Item>("departments");
    }
}
 
Riley K replied to Bookworm on 04-Nov-11 10:24 PM


Here is the example to query on a list

I am populating a dropdownlist with items from custom list

private void bindPersonTitles()
{
  // the name of the DropDownList control we are populating is ddlPersonTitles..
  using (SPWeb configWeb = SPContext.Current.Site.AllWebs["configuration"])
  {
    SPList titlesList = configWeb.Lists["PersonTitles"];
 
    foreach (SPListItem titleItem in titlesList.Items)
    {
      ddlPersonTitles.Items.Add(new ListItem(titleItem.Title, titleItem.ID.ToString()));
    }
  }
}


Regards
Anoop S replied to Bookworm on 05-Nov-11 02:40 AM
Retrieving List Items with CAML using the SharePoint Object Model

If you need to retrieve items from a list when developing web parts, application pages or custom field types you can best use the SPQueryobject from the SharePoint object model. This object is located in the Microsoft.SharePoint namespace of the Microsoft.SharePoint.dll located in the Global Assembly Cache.

refer this for more details
http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list