Skip to content

Commit e799c20

Browse files
committed
Merge pull request pnp#1303 from PaoloPia/book-samples
Microsoft Graph API - book samples
2 parents 563d317 + d34ba53 commit e799c20

File tree

146 files changed

+33866
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+33866
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OfficeDevPnP.MSGraphAPIDemo", "OfficeDevPnP.MSGraphAPIDemo\OfficeDevPnP.MSGraphAPIDemo.csproj", "{9A074D64-DB6A-4029-BF56-054A95250B58}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9A074D64-DB6A-4029-BF56-054A95250B58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9A074D64-DB6A-4029-BF56-054A95250B58}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9A074D64-DB6A-4029-BF56-054A95250B58}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9A074D64-DB6A-4029-BF56-054A95250B58}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Web;
2+
using System.Web.Optimization;
3+
4+
namespace OfficeDevPnP.MSGraphAPIDemo
5+
{
6+
public class BundleConfig
7+
{
8+
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
9+
public static void RegisterBundles(BundleCollection bundles)
10+
{
11+
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
12+
"~/Scripts/jquery-{version}.js"));
13+
14+
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
15+
"~/Scripts/jquery.validate*"));
16+
17+
// Use the development version of Modernizr to develop with and learn from. Then, when you're
18+
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
19+
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
20+
"~/Scripts/modernizr-*"));
21+
22+
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
23+
"~/Scripts/bootstrap.js",
24+
"~/Scripts/respond.js"));
25+
26+
bundles.Add(new StyleBundle("~/Content/css").Include(
27+
"~/Content/bootstrap.css",
28+
"~/Content/Office365SuiteBar.css",
29+
"~/Content/site.css"));
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Web;
2+
using System.Web.Mvc;
3+
4+
namespace OfficeDevPnP.MSGraphAPIDemo
5+
{
6+
public class FilterConfig
7+
{
8+
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
9+
{
10+
filters.Add(new HandleErrorAttribute());
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
using System.Web.Routing;
7+
8+
namespace OfficeDevPnP.MSGraphAPIDemo
9+
{
10+
public class RouteConfig
11+
{
12+
public static void RegisterRoutes(RouteCollection routes)
13+
{
14+
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15+
16+
routes.MapRoute(
17+
name: "Default",
18+
url: "{controller}/{action}/{id}",
19+
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
20+
);
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Web;
7+
using Owin;
8+
using Microsoft.Owin.Security;
9+
using Microsoft.Owin.Security.Cookies;
10+
using Microsoft.Owin.Security.OpenIdConnect;
11+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
12+
using System.Threading.Tasks;
13+
using OfficeDevPnP.MSGraphAPIDemo.Components;
14+
using System.Security.Claims;
15+
16+
namespace OfficeDevPnP.MSGraphAPIDemo
17+
{
18+
public partial class Startup
19+
{
20+
public void ConfigureAuth(IAppBuilder app)
21+
{
22+
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
23+
24+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
25+
26+
app.UseOpenIdConnectAuthentication(
27+
new OpenIdConnectAuthenticationOptions
28+
{
29+
ClientId = MSGraphAPIDemoSettings.ClientId,
30+
Authority = MSGraphAPIDemoSettings.Authority,
31+
PostLogoutRedirectUri = MSGraphAPIDemoSettings.PostLogoutRedirectUri,
32+
Notifications = new OpenIdConnectAuthenticationNotifications()
33+
{
34+
SecurityTokenValidated = (context) =>
35+
{
36+
return Task.FromResult(0);
37+
},
38+
AuthorizationCodeReceived = (context) =>
39+
{
40+
var code = context.Code;
41+
42+
ClientCredential credential = new ClientCredential(
43+
MSGraphAPIDemoSettings.ClientId,
44+
MSGraphAPIDemoSettings.ClientSecret);
45+
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(
46+
ClaimTypes.NameIdentifier).Value;
47+
48+
AuthenticationContext authContext = new AuthenticationContext(
49+
MSGraphAPIDemoSettings.Authority,
50+
new SessionADALCache(signedInUserID));
51+
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
52+
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)),
53+
credential, MSGraphAPIDemoSettings.MicrosoftGraphResourceId);
54+
55+
return Task.FromResult(0);
56+
},
57+
AuthenticationFailed = (context) =>
58+
{
59+
context.OwinContext.Response.Redirect("/Home/Error");
60+
context.HandleResponse(); // Suppress the exception
61+
return Task.FromResult(0);
62+
}
63+
}
64+
});
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
using Newtonsoft.Json;
2+
using OfficeDevPnP.MSGraphAPIDemo.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Web;
7+
8+
namespace OfficeDevPnP.MSGraphAPIDemo.Components
9+
{
10+
public static class CalendarHelper
11+
{
12+
/// <summary>
13+
/// This method retrieves the calendars of the current user
14+
/// </summary>
15+
/// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
16+
/// <returns>A page of up to 10 calendars</returns>
17+
public static List<Calendar> ListCalendars(Int32 startIndex = 0)
18+
{
19+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
20+
String.Format("{0}me/calendars?$skip={1}",
21+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
22+
startIndex));
23+
24+
var calendarList = JsonConvert.DeserializeObject<CalendarList>(jsonResponse);
25+
return (calendarList.Calendars);
26+
}
27+
28+
/// <summary>
29+
/// This method retrieves the calendars of the current user
30+
/// </summary>
31+
/// <param name="id">The ID of the calendar</param>
32+
/// <returns>The calendar</returns>
33+
public static Calendar GetCalendar(String id)
34+
{
35+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
36+
String.Format("{0}me/calendars/{1}",
37+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
38+
id));
39+
40+
var calendar = JsonConvert.DeserializeObject<Calendar>(jsonResponse);
41+
return (calendar);
42+
}
43+
44+
/// <summary>
45+
/// This method retrieves the events of the current user's calendar
46+
/// </summary>
47+
/// <param name="calendarId">The ID of the calendar</param>
48+
/// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
49+
/// <returns>A page of up to 10 events</returns>
50+
public static List<Event> ListEvents(String calendarId, Int32 startIndex = 0)
51+
{
52+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
53+
String.Format("{0}me/calendars/{1}/events?$skip={2}",
54+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
55+
calendarId,
56+
startIndex));
57+
58+
var eventList = JsonConvert.DeserializeObject<EventList>(jsonResponse);
59+
return (eventList.Events);
60+
}
61+
62+
/// <summary>
63+
/// This method retrieves the events of the current user's calendar within a specific date range
64+
/// </summary>
65+
/// <param name="calendarId">The ID of the calendar</param>
66+
/// <param name="startDate">The start date of the range</param>
67+
/// <param name="endDate">The end date of the range</param>
68+
/// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
69+
/// <returns>A page of up to 10 events</returns>
70+
public static List<Event> ListEvents(String calendarId, DateTime startDate,
71+
DateTime endDate, Int32 startIndex = 0)
72+
{
73+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
74+
String.Format("{0}me/calendars/{1}/calendarView?startDateTime={2:o}&endDateTime={3:o}&$skip={4}",
75+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
76+
calendarId,
77+
startDate.ToUniversalTime(),
78+
endDate.ToUniversalTime(),
79+
startIndex));
80+
81+
var eventList = JsonConvert.DeserializeObject<EventList>(jsonResponse);
82+
return (eventList.Events);
83+
}
84+
85+
/// <summary>
86+
/// This method retrieves the events of a series within a specific date range
87+
/// </summary>
88+
/// <param name="calendarId">The ID of the calendar</param>
89+
/// <param name="masterSeriesId">The ID of the master event of the series</param>
90+
/// <param name="startDate">The start date of the range</param>
91+
/// <param name="endDate">The end date of the range</param>
92+
/// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
93+
/// <returns>A page of up to 10 events</returns>
94+
public static List<Event> ListSeriesInstances(String calendarId,
95+
String masterSeriesId,
96+
DateTime startDate,
97+
DateTime endDate,
98+
Int32 startIndex = 0)
99+
{
100+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
101+
String.Format("{0}me/calendars/{1}/events/{2}/instances?startDateTime={3:o}&endDateTime={4:o}&$skip={5}",
102+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
103+
calendarId,
104+
masterSeriesId,
105+
startDate.ToUniversalTime(),
106+
endDate.ToUniversalTime(),
107+
startIndex));
108+
109+
var eventList = JsonConvert.DeserializeObject<EventList>(jsonResponse);
110+
return (eventList.Events);
111+
}
112+
113+
/// <summary>
114+
/// This method creates an event in a target calendar
115+
/// </summary>
116+
/// <param name="calendarId">The ID of the target calendar</param>
117+
/// <param name="calendarEvent">The event to add</param>
118+
/// <returns>The added event</returns>
119+
public static Event CreateEvent(String calendarId, Event calendarEvent)
120+
{
121+
String jsonResponse = MicrosoftGraphHelper.MakePostRequestForString(
122+
String.Format("{0}me/calendars/{1}/events",
123+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
124+
calendarId),
125+
calendarEvent, "application/json");
126+
127+
var createdEvent = JsonConvert.DeserializeObject<Event>(jsonResponse);
128+
return (createdEvent);
129+
}
130+
131+
/// <summary>
132+
/// This method retrieves an event from a calendar
133+
/// </summary>
134+
/// <param name="calendarId">The ID of the calendar</param>
135+
/// <param name="eventId">The ID of the event</param>
136+
/// <returns>The retrieved event</returns>
137+
public static Event GetEvent(String calendarId, String eventId)
138+
{
139+
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
140+
String.Format("{0}me/calendars/{1}/events/{2}",
141+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
142+
calendarId, eventId));
143+
144+
var calendarEvent = JsonConvert.DeserializeObject<Event>(jsonResponse);
145+
return (calendarEvent);
146+
}
147+
148+
/// <summary>
149+
/// This method updates an event in a calendar
150+
/// </summary>
151+
/// <param name="calendarId">The ID of the calendar</param>
152+
/// <param name="eventId">The event to update</param>
153+
/// <returns>The updated event</returns>
154+
public static Event UpdateEvent(String calendarId, Event eventToUpdate)
155+
{
156+
String jsonResponse = MicrosoftGraphHelper.MakePatchRequestForString(
157+
String.Format("{0}me/calendars/{1}/events/{2}",
158+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
159+
calendarId, eventToUpdate.Id),
160+
eventToUpdate, "application/json");
161+
162+
var updatedEvent = JsonConvert.DeserializeObject<Event>(jsonResponse);
163+
return (updatedEvent);
164+
}
165+
166+
/// <summary>
167+
/// This method deletes an event from a calendar
168+
/// </summary>
169+
/// <param name="calendarId">The ID of the calendar</param>
170+
/// <param name="eventId">The ID of the event to delete</param>
171+
public static void DeleteEvent(String calendarId, String eventId)
172+
{
173+
MicrosoftGraphHelper.MakeDeleteRequest(
174+
String.Format("{0}me/calendars/{1}/events/{2}",
175+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
176+
calendarId, eventId));
177+
}
178+
179+
/// <summary>
180+
/// This method provides a feedback for a received meeting request
181+
/// </summary>
182+
/// <param name="calendarId">The ID of the calendar</param>
183+
/// <param name="eventId">The ID of the meeting request</param>
184+
/// <param name="feedback">The feedback for the meeting request</param>
185+
/// <param name="comment">Any comment to include in the feedback, optional</param>
186+
public static void SendFeedbackForMeetingRequest(String calendarId,
187+
String eventId,
188+
MeetingRequestFeedback feedback,
189+
String comment = null)
190+
{
191+
MicrosoftGraphHelper.MakePostRequest(
192+
String.Format("{0}me/calendars/{1}/events/{2}/{3}",
193+
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
194+
calendarId, eventId, feedback),
195+
content: !String.IsNullOrEmpty(comment) ? new { Comment = comment } : null,
196+
contentType: "application/json");
197+
}
198+
}
199+
200+
/// <summary>
201+
/// Defines the possible feedbacks for a meeting request
202+
/// </summary>
203+
public enum MeetingRequestFeedback
204+
{
205+
Accept,
206+
Decline,
207+
TentativelyAccept,
208+
}
209+
}

0 commit comments

Comments
 (0)