Ejemplo n.º 1
0
        /// <summary>
        /// Create a new extension object on a user.
        /// </summary>
        /// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
        /// <param name="userId">The user to access.</param>
        /// <param name="extensionId">The id of the new extension.</param>
        /// <returns>The newly created extension.</returns>
        public static async Task <Extension> CreateExtension(this GraphServiceClient graph, string userId, string extensionId)
        {
            if (string.IsNullOrWhiteSpace(extensionId))
            {
                throw new ArgumentNullException(nameof(extensionId));
            }

            if (string.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }

            try
            {
                // Try to see if the extension already exists.
                return(await graph.GetExtension(userId, extensionId));
            }
            catch
            {
            }

            string requestUrl = graph.Users[userId].Extensions.Request().RequestUrl;

            string json = "{" +
                          "\"@odata.type\": \"microsoft.graph.openTypeExtension\"," +
                          "\"extensionName\": \"" + extensionId + "\"," +
                          "}";

            HttpRequestMessage hrm = new (HttpMethod.Post, requestUrl);

            hrm.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
            await graph.AuthenticationProvider.AuthenticateRequestAsync(hrm);

            HttpResponseMessage response = await graph.HttpProvider.SendAsync(hrm);

            if (response.IsSuccessStatusCode)
            {
                // Deserialize into Extension object.
                var content = await response.Content.ReadAsStringAsync();

                var extension = graph.HttpProvider.Serializer.DeserializeObject <Extension>(content);
                return(extension);
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Delete a user extension by id.
        /// </summary>
        /// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
        /// <param name="userId">The user to access.</param>
        /// <param name="extensionId">The id of the extension to delete.</param>
        /// <returns>A task.</returns>
        public static async Task DeleteExtension(this GraphServiceClient graph, string userId, string extensionId)
        {
            if (string.IsNullOrWhiteSpace(extensionId))
            {
                throw new ArgumentNullException(nameof(extensionId));
            }

            if (string.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }

            try
            {
                await graph.GetExtension(userId, extensionId);
            }
            catch
            {
                // If we can't retrieve the extension, it must not exist.
                return;
            }

            await graph.Users[userId].Extensions[extensionId].Request().DeleteAsync();
        }