Example #1
0
 private bool CheckConnection()
 {
     return(SPUtility.IsConnected());
 }
Example #2
0
        protected void CreateItems <U>(U reportObject) where U : class
        {
            try
            {
                ToggleBusy(true);
                Device.BeginInvokeOnMainThread(async() =>
                {
                    // StringContent contents = null;
                    var client = await OAuthHelper.GetHTTPClientAsync();
                    var data   = reportObject;// _viewobject;

                    var body = JsonConvert.SerializeObject(data, Formatting.None,
                                                           new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });
                    var contents = new StringContent(body);
                    contents.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");
                    if (CheckConnection())
                    {
                        string url     = SPUtility.GetListURL(ReportType.SationInfo);
                        var postResult = await client.PostAsync(url, contents);

                        if (postResult.IsSuccessStatusCode)
                        {
                            App.DAUtil.Delete(_StationInformation);

                            lblLoading.Text = "Item created successfully." + Environment.NewLine;

                            var spData = JsonConvert.DeserializeObject <SPData>(await postResult.Content.ReadAsStringAsync(),
                                                                                new JsonSerializerSettings {
                                DateParseHandling = DateParseHandling.None
                            });
                            int itemId = spData.d.Id;

                            await Task.Delay(500);
                            await SendAttachments(itemId);

                            MessagingCenter.Send(this, "home");
                        }
                        else
                        {
                            var ex = await postResult.Content.ReadAsStringAsync();
                            await DisplayAlert("Sharepoint error", ex, "Ok");
                        }
                        ToggleBusy(false);
                    }
                    else
                    {
                        SaveOfflineItem(body, ReportType.SationInfo, _attachementView.GetAttachmentInfoAsString());

                        DependencyService.Get <IMessage>().LongAlert("Item stored in local storage");
                        ToggleBusy(false);
                    }
                });
            }
            catch (HttpRequestException ex)
            {
                DependencyService.Get <IMessage>().ShortAlert("Upload Error");
            }
            catch (Exception ex)
            {
                DependencyService.Get <IMessage>().ShortAlert("Upload Error" + ex.Message);
            }
        }