Wednesday, 2 July 2014

Getting selected columns from a sharepoint list using SPQuery.ViewFields


Let's say I have a list "Employee" with columns - Name, ID, Age, Sex, Address, SSN etc. and I just want to retrieve data corresponding to columns Name & SSN for Employee with ID = 9.
Syntactically, it can be accomplished as below -
// Code starts here
SPSite site = new SPSite("site url");
SPWeb web = site.OpenWeb();
SPList objList = web.Lists["listName"];
SPQuery qry = new SPQuery();
qry.Query = ""................"";
qry.ViewFields =SPListItemCollection itemCol = objList.GetItems(qry);


IMP:
qry.ViewFields=