1

I am currently getting all events from a calendar one calendar at a time. These are all sharepoint online calendars. This is working fine but a little slow. The basic of what I am doing is:

ClientContext clientContext = new ClientContext(url);
clientContext.Credentials = onlineCredentials;
List oList = clientContext.Web.Lists.GetByTitle(title);

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><And><Leq><FieldRef Name='EventDate'/><Value Type='DateTime'>" + endDate + "</Value></Leq><Geq><FieldRef Name='EndDate' /><Value Type='DateTime'>" + startDate + "</Value></Geq></And></Where></Query></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);

clientContext.Load(collListItem);

clientContext.ExecuteQuery();

Since I am calling the same code above 5 or more times to get all my calendars, is there a way to pass all the urls together and get all events at once?

1 Answer 1

1

A more efficient option may be to use the SharePoint Search API. You could query across all sites with a single query. Here's an example:

using (ClientContext clientContext = new ClientContext(url))
{
    KeywordQuery keywordQuery = new KeywordQuery(clientContext);
    keywordQuery.QueryText = "ContentClass:STS_ListItem_Event (path:\"http://site1\" OR path:\"http://site2\" OR path:\"http://site3\")";
    SearchExecutor searchExecutor = new SearchExecutor(clientContext);
    ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
    clientContext.ExecuteQuery();
}

The only problem is that you would need to setup managed properties for Start Date and End Date before you could use them in your query.

3
  • Thank you for the suggestion. That is quite faster. I am using the: foreach (var oResultRow in oResultTables.Value[0].ResultRows) and those results are missing most of the data I need. I am taking the data and putting it into a calendar. So I am missing EndData, StartDate, fAllDayEvent, Category, ... Is there another way to get that information? Also, for ClientContext, does the url have to be that I have permission on the calendar and site? Thanks again.
    – pomo
    Commented Oct 27, 2014 at 20:34
  • It will only retrieve managed properties. You can create managed properties in your site collection settings by following this guide: technet.microsoft.com/en-us/library/… As far as permissions, yes the user would have to have permissions to the item for them to see it in search results. They won't get an 'access denied' error, the result just wouldn't show up. Commented Oct 27, 2014 at 21:04
  • Then I most likely can not use the approach. I am basically a user that has been granted read access to the Calendar page. I will not be able to request the admin of those calendars to alter managed properties. So I need a way to get calendar events as read access to only that calendar page.
    – pomo
    Commented Oct 27, 2014 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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