1

** I have a Sharepoint List called "Resources" and have various Fields as shown below and same returns as DataTable:

ID    Title     Role       LoginName    Enable[Yes/No] 
1    Pranav    Developer    BW\PPB       Yes
2    Rohit     Tester       BW\RW        No
3    Sarang    Account      BW\SJ        No
4    Amit      Soft.Eng     BW\AI        Yes
4    Kunal     Soft.Eng     BW\KT        Yes

**My code:

 public static DataTable GetListData()
        {
            DataTable returnTable = null;

            SPSite thisSite = null;
            SPWeb thisWeb = null;
            SPList thisList = null;

            try
            {
                thisSite = new SPSite(siteLink);
                thisWeb = thisSite.OpenWeb();
                thisList = thisWeb.Lists["Resources"];
                if (thisList.Items.Count > 0)
                   {
                filterQuery = new SPQuery();
                filterQuery.ViewFields = PopulateFieldsParameter("Title,Role,AddtionalRoles,Designation,LoginName,ClientBillable,NonBillable, EnableChatting,LoggedIn,EnableTimesheet,DesignationTitle,pmPODEnable");
                returnTable = thisList.GetItems(filterQuery).GetDataTable();
                   }
            returnTable = thisList.Items.GetDataTable();
            }
             return returnTable;
         }

With this above code i am able to retrieve whole list data but i want to get table(rows) whose Enable is Yes.

Actual Output

   ID    Title     Role       LoginName    Enable[Yes/No] 
    1    Pranav    Developer    BW\PPB       Yes
    2    Rohit     Tester       BW\RW        No
    3    Sarang    Account      BW\SJ        No
    4    Amit      Soft.Eng     BW\AI        Yes
    5    Kunal     Soft.Eng     BW\KT        Yes

Expected Output -Condition= Enable == Yes

ID    Title     Role       LoginName    Enable[Yes/No] 

1    Pranav    Developer    BW\PPB       Yes
4    Amit      Soft.Eng     BW\AI        Yes
5    Kunal     Soft.Eng     BW\KT        Yes

Help..!!

thanks in advance.

1
public static DataTable GetListData()
    {
        DataTable returnTable = null;

        SPSite thisSite = null;
        SPWeb thisWeb = null;
        SPList thisList = null;

        try
        {
            thisSite = new SPSite(siteLink);
            thisWeb = thisSite.OpenWeb();
            thisList = thisWeb.Lists["Resources"];

            SPQuery query = new SPQuery();
            string camlquery = "<Where><Eq><FieldRef Name='Enable' /><Value Type='Text'>" + true + "</Value></Eq></Where>";
            query.Query = camlquery;
            SPListItemCollection spcol = thisList.GetItems(query);  

            returnTable = spcol.GetDataTable();
        }
     }
3
  • verify caml query.. – Rajesh Joshi Oct 6 '14 at 11:01
  • i have edited my code above...plz check it – Pranav Bilurkar Oct 6 '14 at 12:50
  • where is your filter query to filer results based on Enable filds – Rajesh Joshi Oct 7 '14 at 4:23
0

Instead of

if (thisList.Items.Count > 0)
                returnTable = thisList.Items.GetDataTable();

Use below code:

SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name=\"Enable\" /><Value Type=\"Boolean\">1</Value></Eq></Where>";
SPListItemCollection itemCol = list.GetItems(query);
var dataTbl = itemCol .GetDataTable();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.