Get SharePoint list item by List Unique Field using CAML Query

  1. // Function to get value  
  2. public SPListItem GetRKListItemByRefNo(SPWeb objSPWeb, string refNo)  
  3.     {  
  4.         SPList objSPInvoiceList = objSPWeb.Lists["RKList"];  
  5.         string camlQuery = string.Empty;  
  6.         if (!String.IsNullOrEmpty(refNo))  
  7.         {  
  8.             camlQuery = "<View><Query><Where><Eq><FieldRef Name='RefNo' /><Value Type='Text'>" + refNo + "</Value></Eq></Where></Query></View>";  
  9.         }  
  10.         SPQuery qryInv = new SPQuery();  
  11.         qryInv.ViewXml = camlQuery;  
  12.         SPListItemCollection incAnaCol = objSPInvoiceList.GetItems(qryInv);  
  13.         return incAnaCol[0];  
  14.     }  
  15.     // Calling the function  
  16. SPListItem item = GetRKListItemByRefNo(web, itemRefNo);