3

I have created the SharePoint list and a custom view. I need to get the list items from the specific custom view instead of all items view. How to do this using c# with caml query?

I have used below code

   SPUser user = site.CurrentUser;
   AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
   SPList upcomingEventsList = site.Lists["Upcoming Events"];

   SPView view = upcomingEventsList.Views["Calendar"];

   SPQuery upcomingEventsQuery = new SPQuery();
   upcomingEventsQuery.CalendarDate = DateTime.Now;
   upcomingEventsQuery.ExpandRecurrence = true;
   string date = XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local);
   upcomingEventsQuery.ViewFields = "<FieldRef Name='LinkTitle'/><FieldRef Name='ID'/><FieldRef Name='Location'/><FieldRef Name='EventDate'/><FieldRef Name='EndDate'/>";
   upcomingEventsQuery.Query = "<View><Query>"+view.Views+"<Where><Geq><FieldRef Name='EventDate'/><Value IncludeTimeValue='TRUE' Type='DateTime'>" + date + "</Value></Geq></Where><OrderBy><FieldRef Name='EventDate' Ascending='True'/></Now></OrderBy></Query></View>";
   SPListItemCollection SPListItemCollection = upcomingEventsList.GetItems(upcomingEventsQuery);
   DataTable upcomingEventsDT = SPListItemCollection.GetDataTable();

This is not getting the calendar items properly.

7

Try below code. It fetches all items from My Tasks view.

using (SPSite site = new SPSite("https://sitecollectionurl"))
{
    using (SPWeb web = site.OpenWeb())
    {   
       SPList list = web.Lists.TryGetList("Tasks";
       if(list!=null){
           SPView view = list.Views["My Tasks"];   //custom view name

           SPListItemCollection items = list.GetItems(view);

           // Get a collection of view field names.
           StringCollection viewFields = view.ViewFields.ToStringCollection();

           // Print data for each item in the view.
           foreach (SPListItem item in items)
           {
              // Print the value of each view field.
              foreach (string fieldName in viewFields)
              {
                 Console.WriteLine("{0} = {1}", fieldName, item[fieldName]);
              }
              Console.WriteLine();
           }
       }
    }
}

Never include Query in CAML

Try it as below:

SPUser user = site.CurrentUser;
AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
SPList upcomingEventsList = site.Lists["Upcoming Events"];

SPView view = upcomingEventsList.Views["Calendar"];

SPQuery upcomingEventsQuery = new SPQuery();
upcomingEventsQuery.CalendarDate = DateTime.Now;
upcomingEventsQuery.ExpandRecurrence = true;
string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now); 
upcomingEventsQuery.ViewFields = "<FieldRef Name='LinkTitle'/><FieldRef Name='ID'/><FieldRef Name='Location'/><FieldRef Name='EventDate'/><FieldRef Name='EndDate'/>";
upcomingEventsQuery.Query = "<Where><Geq><FieldRef Name='EventDate'/><Value IncludeTimeValue='TRUE' Type='DateTime'>" + date + "</Value></Geq></Where><OrderBy><FieldRef Name='EventDate' Ascending='True'/></Now></OrderBy>";
upcomingEventsQuery.ViewFieldsOnly=true;
SPListItemCollection SPListItemCollection = upcomingEventsList.GetItems(upcomingEventsQuery);
DataTable upcomingEventsDT = SPListItemCollection.GetDataTable();
  • I have added the reference code – PGP Feb 7 '17 at 6:44
  • @GokulPgp Check updated answer :) – Gautam Sheth Feb 7 '17 at 6:48
  • we are not used the line SPView view = upcomingEventsList.Views["Calendar"]; – PGP Feb 7 '17 at 6:51
  • Can you check now ? Have added full code. – Gautam Sheth Feb 7 '17 at 7:13
  • 1
    @GokulPgp are you sure that your custom view name is Calendar? Double check your view name. I am not sure but there can be also a case where display and internal name of view are different, and code expects internal name. – Asad Refai Feb 7 '17 at 8:49
5

Get sharepoint list items by view name:

using (SPSite site = new SPSite("http://siteURL"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks"; // List URL
               SPList list = web.GetList(listUrl);
               SPView view = list.Views["My Tasks"]; // Get view by name
               SPListItemCollection items = list.GetItems(view);

               foreach (SPListItem item in items)
               {                  
                  Console.WriteLine("{0}", item["Title"]);

                  Console.WriteLine();
               }
            }
         }
  • This is retrieves all items from list. not based on view. – PGP Feb 7 '17 at 8:05
4

Here is the c# Client Object Model code to retrieve items from a custom view.

static void Main(string[] args)
{
    // Connect to your SharePoint site and load object of your custom view
    //
    ClientContext context = new ClientContext("http://sp2013sps/sites/test");
    List list = context.Web.Lists.GetByTitle("TestListName");
    context.Load(list);
    context.ExecuteQuery();
    View view = list.Views.GetByTitle("Test View");

    context.Load(view);
    context.ExecuteQuery();

    // Create a new CAML Query object and store the query from the custom view
    //
    CamlQuery query = new CamlQuery();
    query.ViewXml = view.ViewQuery;

    // Based on the query load items
    //
    ListItemCollection items = list.GetItems(query);
    context.Load(items);
    context.ExecuteQuery();
    Console.Write(items.Count);
}

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.