Sunday, June 22, 2014

SPQuery to get Highest salary from Custom List in SharePoint

Hi All,

Here is very simple example to get highest salary of employee from Custom List using SPQuery...

1- Create a list as given below...



using (SPSite site = new SPSite("http://localhost"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["EmployeeList"];
                        SPQuery query = new SPQuery();
                        query.Query = "<OrderBy><FieldRef Name='Salary' Ascending='False' /></OrderBy>";
                        query.RowLimit = 1;
                        SPListItemCollection items = list.GetItems(query);

                        if (items.Count > 0)
                        {
                            SPListItem _item = items[0];

                            Console.WriteLine("Name: " + _item["Name"].ToString() + "\t" + "Salary: " + _item["Salary"].ToString());

                        }
                    }
                }

               
Output given below:

















Have Fun !!


No comments:

Post a Comment

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...