如何编辑和读取SharePoint列表中的数据

时间:2009-09-01 15:50:05

标签: sharepoint sharepoint-2007

我想查询SharePoint列表,并希望将数据显示在我创建的用户控件中。

之前我的数据来自数据库。

现在我需要修改它以使用SharePoint列表。

你能指导我吗?

格雷斯。

3 个答案:

答案 0 :(得分: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;
}

我不确定我是否遗漏了任何东西,但这应该让你接近。

希望它有所帮助!

答案 1 :(得分:0)

我用它来查询SharePoint列表

            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;
            }

答案 2 :(得分:0)

获取列的不同值单击Here

要编辑列表中的值,有一个很好的帖子here