|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
namespace ClientObjectModelAndDate
{
public partial class DemoForm : System.Windows.Forms.Form
{
public DemoForm()
{
InitializeComponent();
}
private string ListTitle = "Client Object Model - CAML Tasks";
private void CreateDataButton_Click(object sender, EventArgs e)
{
// Open context
ClientContext clientContext = new ClientContext(UrlText.Text);
List list = clientContext.Web.Lists.GetByTitle(ListTitle);
clientContext.Load(list);
try
{
// Try get task list
clientContext.ExecuteQuery();
}
catch (ServerException)
{
// Create task list
ListCreationInformation listCreationInformation = new ListCreationInformation();
listCreationInformation.Title = ListTitle;
listCreationInformation.TemplateType = (int)ListTemplateType.Tasks;
list = clientContext.Web.Lists.Add(listCreationInformation);
clientContext.ExecuteQuery();
}
// insert data
DateTime date = DateTime.Today.AddMonths(2);
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 3; j++)
{
ListItemCreationInformation info = new ListItemCreationInformation();
ListItem item = list.AddItem(info);
item["Title"] = "A task item";
item["DueDate"] = date;
item.Update();
clientContext.ExecuteQuery();
}
date = date.AddMonths(-1); // Decrement Month on each outer loop
}
}
private void ShowAllDataButton_Click(object sender, EventArgs e)
{
// Load Data
ClientContext context = new ClientContext(UrlText.Text);
List list = context.Web.Lists.GetByTitle(ListTitle);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
context.Load(list);
context.Load(items);
context.ExecuteQuery();
// Show Data
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
table.Columns.Add("DueDate");
foreach (ListItem item in items)
table.Rows.Add(item.Id, item["Title"], item["DueDate"]);
grid1.DataSource = table;
}
private void ShowThisMonthDataButton_Click(object sender, EventArgs e)
{
// Prepare start and end dates
DateTime startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
DateTime endDate = startDate.AddMonths(1).AddMinutes(-1);
// Format dates
string startDateFx = startDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
string endDatFx = endDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
// Load Data for this Month
ClientContext clientContext = new ClientContext(UrlText.Text);
List list = clientContext.Web.Lists.GetByTitle(ListTitle);
CamlQuery query = new CamlQuery();
query.ViewXml = @"<View>" +
"<Query>" +
"<Where>" +
"<And>" +
"<Geq>" +
"<FieldRef Name='DueDate'/>" +
"<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
"</Geq>" +
"<Leq>" +
"<FieldRef Name='DueDate'/>" +
"<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
"</Leq>" +
"</And>" +
"</Where>" +
"</Query>" +
"</View>";
ListItemCollection listItems = list.GetItems(query);
clientContext.Load(listItems, items => items.Include(
item => item["Id"],
item => item["Title"],
item => item["DueDate"]
));
clientContext.ExecuteQuery();
// Show Data
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
table.Columns.Add("DueDate");
foreach (ListItem item in listItems)
table.Rows.Add(item.Id, item["Title"], item["DueDate"]);
grid2.DataSource = table;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.
In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.
Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.
You can find some of his work over here. He blogs at http://jeanpaulva.com