Reputation: 1179
I want to delete an item from sharepoint calendar. Here is part of my code:
Microsoft.SharePoint.Client.CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
query.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='Title'/><Value Type='Text'>"
+ requestTitle +
"</Value></Eq><Eq><FieldRef Name='EventDate'/><Value Type='Text'>"
+ lFrom +
"</Value></Eq></And><And><Eq><FieldRef Name='EndDate'/><Value Type='Text'>"
+ lTo +
"</Value></Eq><Eq><FieldRef Name='fAllDayEvent'/><Value Type='Text'>"
+ Boolean.TrueString +
"</Value></Eq></And></Where></Query></View>";
Microsoft.SharePoint.Client.ListItemCollection listItem = calendar.GetItems(query);
context.Load(listItem);
if (listItem.Count == 1)
{
listItem[0].DeleteObject();
//context.ExecuteQuery();
}
It throws an exception in line
if (listItem.Count == 1)
it says that listItem is not initialized. It even throws the exception when I pass empty query like this:
Microsoft.SharePoint.Client.ListItemCollection listItem = calendar.GetItems(new Microsoft.SharePoint.Client.CamlQuery());
Why? On this link https://msdn.microsoft.com/en-us/library/office/ee534956(v=office.14).aspx it is said that it should retrieve all items when empty query is passed.
Evertyhing works perfectly for adding new item to calendar, here is code I used for that:
//adding new calendar item
Microsoft.SharePoint.Client.ListItemCreationInformation item = new Microsoft.SharePoint.Client.ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem newItem = calendar.AddItem(item);
newItem["Title"] = requestTitle;
newItem["EventDate"] = lFrom;
newItem["EndDate"] = lTo;
newItem["fAllDayEvent"] = Boolean.TrueString;
newItem.Update();
Upvotes: 3
Views: 183
Reputation: 826
Since, you have used,
Microsoft.SharePoint.Client
library, I am assuming its a client-side application. In client-side app, sending a request to the server is a costly affair. Hence, what actually happens is that whenever you load any component, only a request is created in the background. This way, you can bundle multiple request into 1 using the load() as many times. To actually send the request to the server, you need to call the ExecuteQuery() before checking the length of the listItemCollection.
Microsoft.SharePoint.Client.ListItemCollection listItem = calendar.GetItems(query);
context.Load(listItem);
context.ExecuteQuery(); // This will take a while
if (listItem.Count == 1)
{
listItem[0].DeleteObject();
context.ExecuteQuery();
}
Upvotes: 2