tags:

views:

43

answers:

2

Hi, I am writing the SPQuery for getting the data in contact list of sharepoint site.but how to write that? Means I want to retrieve data as :

Name:aaa
Cell No: 13123131
Address : something address here..
so on...

of given LAst Name in search text box (build by me). how to do that? Means what query i have to write? (Syntax please).

+1  A: 

Hi Lalit,

You can construct a caml query with a filter on LastName. Please check the below msdn link which has an example of using SPQuery with task list. Similarly you can use it for contact list as well.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx

You can use Caml query builder to construct your caml queries. YOu can download it from here:

http://www.u2u.be/Res/downloads/u2ucamlquerybuildersolution.zip

Hope this helps.

-Faiz

Faiz
+1  A: 
string siteUrl = "http://sharepointserver/";
string webUrl = "MySubSite";
using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb(webUrl))
    {
        SPList list = web.Lists["Contacts"];

        string lastName = "Smith";

        SPQuery q = new SPQuery();
        q.Query = string.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", lastName);

        SPListItemCollection items = list.GetItems(q);

        foreach (SPListItem item in items)
        {
            Console.WriteLine(item["Title"]);
        }
    }
}
Kit Menke