Remove an item from your Sharepoint calendar

I want to remove an item from my sharepoint calendar. Here is a portion 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 at line

               if (listItem.Count == 1)

      

it says the listItem is not initialized. It even throws an exception when I pass an empty request like this:

               Microsoft.SharePoint.Client.ListItemCollection listItem = calendar.GetItems(new Microsoft.SharePoint.Client.CamlQuery());

      

Why? This link https://msdn.microsoft.com/en-us/library/office/ee534956(v=office.14).aspx says that it should fetch all items when submitting an empty request.

Evertyhing works great for adding a new item to the calendar, here is the code I used to do it:

                //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();

      

+3


source to share


1 answer


Since you used

Microsoft.SharePoint.Client



I accept his client application. In a client application, sending a request to the server is costly. Hence what actually happens is that whenever you load any component, only a request is generated in the background. This way you can combine multiple queries into 1 using load () as many times. To send a request to the server, you need to call 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();
}

      

+2


source







All Articles