更有效的方式以编程方式从Sharepoint列表中获取值?

时间:2016-08-30 09:03:12

标签: c# sharepoint

我正在使用SharePoint 2010并使用以下代码从列表中获取值。

它可能被称为......

getValueFromList("addresses", "secondName", "city", "manhattan");

它将返回" secondName"第一个条目是" city = manhattan"

有没有办法让它更有效率?

private string getValueFromList(string theList, string returnColumnName,   string matchColumnName, string valueToMatch)
    {
        try
        {
            SPSite siteh2 = SPContext.Current.Site;

            using (SPWeb oWeb = siteh2.OpenWeb())
            {

                //SPWeb oWeb = SPControl.GetContextSite(Context).OpenWeb();
                SPList oList = oWeb.Lists[theList];

                SPQuery oQuery = new SPQuery();
                oQuery.Query = "<Where><Eq><FieldRef Name='" + matchColumnName + "' />" +
                    "<Value Type='Text'>" + valueToMatch + "</Value></Eq></Where>";

                //oQuery.Query = "<Query><Where><Eq><FieldRef Name='Username' /><Value Type='Text'>whelanp</Value></Eq></Where></Query>";

                SPListItemCollection results = oList.GetItems(oQuery);

                if (results.Count == 0)
                    return "";

                foreach (SPListItem oListItem in results)
                {
                    return cleanValue(oListItem[returnColumnName].ToString());
                }
                return "";
            }
        }
        catch (Exception ex)
        {
            return "";
        }
    }

0 个答案:

没有答案