Thursday, September 29, 2011

search SharePoint list and return matching column using LINQ

I Have writen this reusable code, which will open a Sharepoint List, and search that list and return the first matching row's specific column:
This function need 04 parameters: ListName, Search FieldName, Search Value, and return Column name.


public string GetAColumnValueFromList(string listName, string Field, string value, string returnField)
{
string outData = string.Empty;
DataTable dtResult = new DataTable();

try
{
if (oList == null)
{
using (SPSite oSiteCollection = new SPSite(siteUrl))
{
using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb("/"))
{
oList = oWebsiteRoot.Lists[listName];

}
}
}

List items = (from li in oList.Items.OfType()
where li["GUID1"].ToString() == value
select li).ToList();

foreach (SPListItem item in items)
{
outData = item[returnField].ToString();
}

return outData;
}
catch (Exception ex)
{
EnterpriseLibrary.HandleException(ex, Policies.BLL);
throw ex;
}
}


If you like this solution, then care for a tree which need some water, so give water to a plant and make that green.
Start using http://www.code4green.com as your free code generation tool for Sharepoint, PowerShell, C#, ASP.net, SQL, and many more languages.
let other know about this tool, because this tool is helping community.

No comments:

Post a Comment