public CalendarItem GetCalendarItemById(string siteUrl, string listName, int itemId) { string realm = ConfigurationManager.AppSettings["ida:Audience"]; string appId = ConfigurationManager.AppSettings["ida:ClientId"]; string appSecret = ConfigurationManager.AppSettings["ida:ClientSecret"]; CalendarItem calItem = new CalendarItem(); OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager(); using (ClientContext context = authManager.GetAppOnlyAuthenticatedContext(siteUrl, appId, appSecret)) { try { List oList = context.Web.Lists.GetByTitle(listName); ListItem oItem = oList.GetItemById(itemId); context.Load(oItem); context.ExecuteQuery(); calItem.Title = oItem["Title"].ToString(); calItem.ID = oItem.Id; calItem.Description = oItem["Description"].ToString(); calItem.Created = DateTime.Parse(oItem["Created"].ToString()); calItem.EndDate = DateTime.Parse(oItem["EndDate"].ToString()); calItem.EventDate = DateTime.Parse(oItem["EventDate"].ToString()); calItem.FileDirRef = oItem["FileDirRef"].ToString(); calItem.FileRef = oItem["FileRef"].ToString(); calItem.Location = oItem["Location"].ToString(); calItem.Modified = DateTime.Parse(oItem["Modified"].ToString()); FieldUserValue itemAuthor = oItem["Author"] as FieldUserValue; var author = new Models.UserModel(); author.Email = itemAuthor.Email; author.LookupId = itemAuthor.LookupId; author.LookupValue = itemAuthor.LookupValue; calItem.Author = author; FieldUserValue itemEditor = oItem["Editor"] as FieldUserValue; var editor = new Models.UserModel(); editor.Email = itemEditor.Email; editor.LookupId = itemEditor.LookupId; editor.LookupValue = itemEditor.LookupValue; calItem.Editor = editor; calItem.SiteUrl = siteUrl; calItem.ListName = listName; //calItem.Category = oItem["Category"]; return(calItem); } catch (Exception ex) { return(null); } } }
public IEnumerable <CalendarItem> GetCalendarItems(string siteUrl, string listName) { List <CalendarItem> calItems = new List <CalendarItem>(); DateTime todayDate = DateTime.Now; string todayString = todayDate.ToString("yyyy-MM-ddTHH:mm:ssZ"); DateTime maxDate = DateTime.Now.AddDays(30); string maxString = maxDate.ToString("yyyy-MM-ddTHH:mm:ssZ"); OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager(); try { using (ClientContext context = authManager.GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret)) { try { List oList = context.Web.Lists.GetByTitle(listName); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View><Query><Where><And><Geq><FieldRef Name='EventDate'/><Value IncludeTimeValue='False' Type='DateTime'>" + todayString + "</Value></Geq><Leq><FieldRef Name='EventDate'/><Value IncludeTimeValue='False' Type='DateTime'>" + maxString + "</Value></Leq></And></Where></Query></View>"; ListItemCollection listItems = oList.GetItems(camlQuery); context.Load(listItems); context.ExecuteQuery(); foreach (ListItem oListItem in listItems) { var fields = oListItem.FieldValues; var calItem = new CalendarItem(); foreach (var field in fields) { switch (field.Key) { case "ID": calItem.ID = Convert.ToInt32(field.Value); break; case "Title": calItem.Title = field.Value.ToString(); break; case "Description": calItem.Description = WebUtility.HtmlDecode(field.Value.ToString()); break; case "FileDirRef": calItem.FileDirRef = field.Value.ToString(); break; case "FileRef": calItem.FileRef = field.Value.ToString(); break; case "Location": calItem.Location = field.Value.ToString(); break; case "Created": calItem.Created = DateTime.Parse(field.Value.ToString()); break; case "Modified": calItem.Modified = DateTime.Parse(field.Value.ToString()); break; case "EndDate": calItem.EndDate = DateTime.Parse(field.Value.ToString()); break; case "EventDate": calItem.EventDate = DateTime.Parse(field.Value.ToString()); break; case "Author": FieldUserValue itemAuthor = field.Value as FieldUserValue; var author = new Models.UserModel(); author.Email = itemAuthor.Email; author.LookupId = itemAuthor.LookupId; author.LookupValue = itemAuthor.LookupValue; calItem.Author = author; break; case "Editor": FieldUserValue itemEditor = field.Value as FieldUserValue; var editor = new Models.UserModel(); editor.Email = itemEditor.Email; editor.LookupId = itemEditor.LookupId; editor.LookupValue = itemEditor.LookupValue; calItem.Editor = editor; break; } } calItem.ListName = listName; calItem.SiteUrl = siteUrl; calItems.Add(calItem); } return(calItems); } catch (Exception ex) { return(null); } } } catch (Exception ex1) { return(null); } }