1

I want to query a SharePoint list and want to use the data to be displayed in a usercontrol that I have created.

Earlier I had the data coming from a database.

Now I need to modify it to work with a SharePoint list.

Can you please guide me.

Grace.

4
  • In what way do you need guidance? Is it related to how to query a list in SharePoint with the SPQuery object? Please tell us what you need help with... Sep 1, 2009 at 16:07
  • In addition: are you attempting to query the list from within a SharePoint application, or are you attempting to access it from somewhere else via SharePoint's Lists.asmx endpoint? I'm guessing the latter, but figured it would be a good idea to check. Sep 1, 2009 at 16:50
  • Johan You got me right. I am trying to query a SharePoint List and am not using it for external application. I have a web part with a user control mostly text boxes. I need to populate the list data to the text boxes.
    – Pradeep007
    Sep 1, 2009 at 16:57
  • <%#DataBinder.Eval(Container.DataItem, "myfieldName")%>. Earlier I have this in the .ascx. Now I need to have the data "myfieldName" coming from a SharePoint List
    – Pradeep007
    Sep 1, 2009 at 17:03

3 Answers 3

1
SPList list = null;
SPListItemCollection LIC = null;
SPListItem listItem = null;

using (SPSite mainSite = new SPSite("http://sitewhereyourlistis"))
{
    using (SPWeb mainWeb = mainSite.OpenWeb())
    {
        list = mainWeb.Lists["ListNameHere"];

        //**You will most likely want to limit your return to a single record so I have created the caml to do so with the List Item ID
        String caml = String.Format("<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">{0}</Value></Eq></Where>", ListItemID);

        SPQuery qry = new SPQuery();
        qry.Query = caml;
        LIC = list.GetItems(qry);
        listItem = LIC[0];


        //**Here is where you will fill your textboxes
        txtTextBoxName.Text = listItem["ColumnNameHere"].toString();


        //**The above statement likes to error out if the value is null so I like to use a custom Application Helper Function to prevent things like this.
        //**My actual call to get the data would look like this.

        txtTextBoxName.Text = EnsureTextValue(listItem, "ColumnNameHere");
        // I added the Function Definition Below 

    }
}

public String EnsureTextValue(SPListItem item, String param)
{
    String tmp = String.Empty;
    try
    {
        tmp = item[param].ToString();
    }
    catch { "Run Debug Method or Whatever Here" }
    return tmp;
}

I am not sure if I missed anything but this should get you close.

Hope it helps!

1
  • Hi 20Seconds Thanks for your help. Can you please tell me how to edit the content in SharePoint list.
    – Pradeep007
    Sep 2, 2009 at 3:37
0

To get Distinct Values for the column Click Here

To edit the values in the list there is a nice post here

0

I have used this to Query the SharePoint List

            using (SPWeb web = SPContext.Current.Site.RootWeb)
            {
                SPList mylist = web.Lists["listName"];
                SPQuery query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='columnName' /><Value Type='Text'>" + strXYZ+ "</Value></Eq></Where>";
                SPListItemCollection items = mylist.GetItems(query);
                tempDT = items.GetDataTable();
                return tempDT;
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.