Sharepoint list Dynamic Linq query
-
19-06-2021 - |
Question
I need to query a list in SharePoint where the columns may be added in the future. For instance at the moment I have the following columns Name, Job, interests, address I want to be able to query this string dynamically using a parameter from the browser so if columns are added in the future I don’t have to change the code but just the parameter. The address may look like this www.contoso.com/sites/mypage.aspx?property=Interests And the code something on the line of this:
var SiteParameter = Request.QueryString["property"];
var ItemsFromList = from item in ListItems where item[try to put the parameter in here] select item;
I use SPmetal to get the list details, so if I press item. Visual Studio2010 will return the columns within the list.
Solution
This may be easier without SPMetal.
var qy = new SPQuery();
qy.Query =
"<Where><Eq>" +
"<FieldRef Name=`" + siteParameter + "'/>" +
// You may have to worry about the type of the field here, too.
"<Value Type='Text'>" + desiredValue + "</Value>" +
"</Eq></Where>";
var itemCollection = myList.GetItems(qy);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow