コード例 #1
0
        private async Task AddNewCeo()
        {
            StringBuilder requestUri = new StringBuilder(_spContext.SPAppWebUrl.ToString())
                                       .Append("_api/web/lists/getbytitle('CeoList')/items");

            // updated ceo
            var newCeoJson = new SpChiefExecutiveJson {
                Metadata = new JsonMetadata {
                    Type = "SP.Data.CeoListListItem"
                },
                Title           = "Satya Nadella",
                TenureStartYear = "2014",
                TenureEndYear   = "Present"
            };

            StringContent requestContent = new StringContent(JsonConvert.SerializeObject(
                                                                 newCeoJson,
                                                                 Formatting.None,
                                                                 new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

            HttpClient         client  = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString());

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _spContext.UserAccessTokenForSPAppWeb);
            request.Headers.Add("Accept", "application/json;odata=verbose");
            request.Headers.Add("If-Match", "*");
            request.Content = requestContent;

            await client.SendAsync(request);
        }
コード例 #2
0
        private async Task UpdateCurrentCeo()
        {
            // get list of all current CEO's
            var results = await GetChiefExecutives();

            // get CEO with no tenure end date
            var currentCeo = results.FirstOrDefault(ceo => ceo.TenureEndYear == "Present");

            StringBuilder requestUri = new StringBuilder(_spContext.SPAppWebUrl.ToString())
                                       .Append("_api/web/lists/getbytitle('CEO List')/items")
                                       .Append("(" + currentCeo.Id + ")");

            // updated ceo
            var existingCeoJson = new SpChiefExecutiveJson
            {
                Metadata = new JsonMetadata {
                    Type = "SP.Data.CEO_x0020_ListListItem"
                },
                TenureEndYear = "2014"
            };

            StringContent requestContent = new StringContent(JsonConvert.SerializeObject(
                                                                 existingCeoJson,
                                                                 Formatting.None,
                                                                 new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }));

            requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

            HttpClient         client  = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString());

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _spContext.UserAccessTokenForSPAppWeb);
            request.Headers.Add("Accept", "application/json;odata=verbose");
            request.Headers.Add("If-Match", "*");
            request.Headers.Add("X-Http-Method", "Merge");
            request.Content = requestContent;

            await client.SendAsync(request);
        }