Question

I am trying to get latest 10 posts from discussion board in sharepoint by using SPQuery. I need read [PostUrl] [Subject] [Body] [LastModifyDate]?

Please some one help me out?

Was it helpful?

Solution

Try with below sample code, its working on my environment.

        SPSite oSite = new SPSite("http://localhost/");
        SPWeb oWeb = oSite.OpenWeb();
        SPList oList = oWeb.Lists["DiscussionBoardList"];

        SPQuery qry = new SPQuery();
        qry.RowLimit = 10;
        qry.Query = "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
        qry.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Body' /><FieldRef Name='Modified' />";
        DataTable dt = oList.GetItems(qry).GetDataTable();

OTHER TIPS

Try with below sample code, its working on my environment.

     SPSite oSite = new SPSite("http://localhost/");
     SPWeb oWeb = oSite.OpenWeb();
     SPList oList = oWeb.Lists["DiscussionBoardList"];
     SPQuery qry = new SPQuery();
     qry.RowLimit = 10;
     qry.Query = "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
     qry.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Body' /><FieldRef Name='Modified' />";
     DataTable dt = oList.GetItems(qry).GetDataTable(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top