private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    var request = ItemWebApiRequestBuilder.RenderingHtmlRequestWithSourceAndRenderingId(sourceIdTextField.Text, renderingIdTextField.Text)
                                  .Build();

                    this.ShowLoader();

                    Stream response = await session.ReadRenderingHtmlAsync(request);

                    response.Position = 0;
                    string htmlText = "";
                    using (StreamReader reader = new StreamReader(response))
                    {
                        htmlText = await reader.ReadToEndAsync();
                    }

                    this.resultWebView.LoadHtmlString(htmlText, null);
                }
            }
            catch (Exception e)
            {
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.HideLoader();
                });
            }
        }
        public async Task <ScItemsResponse> GetItemByPath(string itemPath, PayloadType itemLoadType, List <ScopeType> itemScopeTypes, string itemLanguage = "en")
        {
            try {
                using (ISitecoreWebApiSession session = await SitecoreSession) {
                    IReadItemsByPathRequest request = ItemWebApiRequestBuilder.ReadItemsRequestWithPath(itemPath)
                                                      .Payload(itemLoadType)
                                                      .AddScope(itemScopeTypes)
                                                      .Language(itemLanguage)
                                                      .Build();


                    return(await session.ReadItemAsync(request));
                }
            }
            catch (SitecoreMobileSdkException ex)
            {
                this._loggingService.Log("Error in GetItemByPath,  id {0} . Error: {1}", itemPath, ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                this._loggingService.Log("Error in GetItemByPath,  id {0} . Error: {1}", itemPath, ex.Message);
                throw ex;
            }
        }
Beispiel #3
0
        public async Task <ScItemsResponse> GetItemById(string itemId, PayloadType itemLoadType, List <ScopeType> itemScopeTypes, string itemLanguage = "en")
        {
            try
            {
                using (ISitecoreWebApiSession session = GetSession())
                {
                    IReadItemsByIdRequest request = ItemWebApiRequestBuilder.ReadItemsRequestWithId(itemId)
                                                    .Payload(itemLoadType)
                                                    .AddScope(itemScopeTypes)
                                                    .Language(itemLanguage)
                                                    .Build();

                    return(await session.ReadItemAsync(request));
                }
            }
            catch (SitecoreMobileSdkException ex)
            {
                Log.Error("Error in GetItemById,  id {0} . Error: {1}", itemId, ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                Log.Error("Error in GetItemById,  id {0} . Error: {1}", itemId, ex.Message);
                throw ex;
            }
        }
Beispiel #4
0
 public async Task <bool> Authenticate(string login, string password)
 {
     using (ISitecoreWebApiSession session = GetSession(login, password))
     {
         return(await session.AuthenticateAsync());
     }
 }
        private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    var request = ItemWebApiRequestBuilder.ReadItemsRequestWithSitecoreQuery(queryTextField.Text)
                                  .Build();

                    this.ShowLoader();

                    ScItemsResponse response = await session.ReadItemAsync(request);

                    this.HideLoader();
                    if (response.ResultCount > 0)
                    {
                        this.ShowItemsList(response);
                    }
                    else
                    {
                        AlertHelper.ShowLocalizedAlertWithOkOption("Message", "Item is not exist");
                    }
                }
            }
            catch (Exception e)
            {
                this.HideLoader();
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
        }
Beispiel #6
0
        private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    var builder = ItemWebApiRequestBuilder.ReadItemsRequestWithId(itemIdTextField.Text)
                                  .Payload(this.currentPayloadType)
                                  .AddFieldsToRead(this.fieldNameTextField.Text);

                    if (this.parentScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Parent);
                    }
                    if (this.selfScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Self);
                    }
                    if (this.childrenScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Children);
                    }

                    var request = builder.Build();

                    this.ShowLoader();

                    ScItemsResponse response = await session.ReadItemAsync(request);

                    if (response.Any())
                    {
                        this.ShowItemsList(response);

                        //items serialization test
                        ScItem item         = response[0] as ScItem;
                        string json         = JsonConvert.SerializeObject(item);
                        ScItem restoredItem = JsonConvert.DeserializeObject <ScItem>(json);
                        Console.WriteLine(restoredItem.DisplayName);
                    }
                    else
                    {
                        AlertHelper.ShowLocalizedAlertWithOkOption("Message", "Item is not exist");
                    }
                }
            }
            catch (Exception e)
            {
                this.CleanupTableViewBindings();
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.HideLoader();
                    this.FieldsTableView.ReloadData();
                });
            }
        }
 public void Setup()
 {
     this.testData = TestEnvironment.DefaultTestEnvironment();
     this.session  =
         SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost(this.testData.InstanceUrl)
         .Credentials(this.testData.Users.Admin)
         .BuildSession();
 }
 public void Setup()
 {
   this.testData = TestEnvironment.DefaultTestEnvironment();
   this.session =
     SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost(this.testData.InstanceUrl)
       .Credentials(this.testData.Users.Admin)
       .BuildSession();
 }
 public void Dispose()
 {
     if (null != this.workerSession)
     {
         this.workerSession.Dispose();
         this.workerSession = null;
     }
 }
        private async void UploadImage(Android.Net.Uri data)
        {
            var imageNameField = this.FindViewById <EditText>(Resource.Id.field_media_item_name);
            var imagePathField = this.FindViewById <EditText>(Resource.Id.field_media_item_path);

            var imageName = imageNameField.Text;
            var imagePath = imagePathField.Text;

            if (string.IsNullOrWhiteSpace(imageName))
            {
                Toast.MakeText(this, "Please select image name before upload", ToastLength.Long).Show();
                return;
            }

            if (data == null)
            {
                Toast.MakeText(this, "Please select image before upload", ToastLength.Long).Show();
                return;
            }

            try
            {
                this.SetProgressBarIndeterminateVisibility(true);

                using (ISitecoreWebApiSession session = Prefs.From(this).Session)
                {
                    using (Stream stream = ContentResolver.OpenInputStream(this.imageUri))
                    {
                        var builder = ItemWebApiRequestBuilder.UploadResourceRequestWithParentPath(imagePath)
                                      .ItemDataStream(stream)
                                      .ContentType("image/jpg")
                                      .ItemName(imageName)
                                      .FileName("bugaga.jpg");

                        var response = await session.UploadMediaResourceAsync(builder.Build());

                        if (response != null && response.ResultCount > 0)
                        {
                            DialogHelper.ShowSimpleDialog(this, "Image uploaded", "Image path : " + response[0].Path);
                        }
                        else
                        {
                            var title = this.GetString(Resource.String.text_error);
                            DialogHelper.ShowSimpleDialog(this, title, "Failed to upload image");
                        }
                    }
                }
                this.SetProgressBarIndeterminateVisibility(false);
            }
            catch (System.Exception exception)
            {
                this.SetProgressBarIndeterminateVisibility(false);

                var title = this.GetString(Resource.String.text_error);
                DialogHelper.ShowSimpleDialog(this, title, exception.Message);
            }
        }
    public void SetupSession()
    {
      this.testData = TestEnvironment.DefaultTestEnvironment();
      this.session = this.CreateSession();

      // Same as this.session
      var cleanupSession = this.CreateSession();
      this.noThrowCleanupSession = new NoThrowWebApiSession(cleanupSession);
    }
        private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    IDownloadMediaOptions options = new MediaOptionsBuilder()
                                                    .Set
                                                    .Width(this.width)
                                                    .Height(this.height)
                                                    .BackgroundColor("white")
                                                    .Build();

                    string path = this.MediaPathTextField.Text;

                    var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(path)
                                  .DownloadOptions(options)
                                  .Build();


                    byte[] data = null;
                    using (Stream response = await session.DownloadMediaResourceAsync(request))
                        using (MemoryStream responseInMemory = new MemoryStream())
                        {
                            await response.CopyToAsync(responseInMemory);

                            data = responseInMemory.ToArray();
                        }

                    BeginInvokeOnMainThread(delegate
                    {
                        using (UIImage image = new UIImage(NSData.FromArray(data)))
                        {
                            // no need disposing
                            // since this.ImageView.Image creates a
                            // new C# object on each call
                            this.ImageView.Image = image;

                            // Update Overlay
                            this.HideLoader();
                        }
                    });
                }
            }
            catch (Exception e)
            {
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.HideLoader();
                });
            }
        }
    public void TearDown()
    {
      this.testData = null;

      this.session.Dispose();
      this.session = null;

      this.noThrowCleanupSession.Dispose();
      this.noThrowCleanupSession = null;
    }
        public void TearDown()
        {
            this.testData = null;

            this.session.Dispose();
            this.session = null;

            this.noThrowCleanupSession.Dispose();
            this.noThrowCleanupSession = null;
        }
Beispiel #15
0
        public void SetupSession()
        {
            this.testData = TestEnvironment.DefaultTestEnvironment();
            this.session  = this.CreateSession();

            // Same as this.session
            var cleanupSession = this.CreateSession();

            this.noThrowCleanupSession = new NoThrowWebApiSession(cleanupSession);
        }
        private async void GetRenderingHtml()
        {
            var sourceIdField    = this.FindViewById <EditText>(Resource.Id.field_source_id);
            var renderingIdField = this.FindViewById <EditText>(Resource.Id.field_rendering_id);

            var sourceId    = sourceIdField.Text;
            var renderingId = renderingIdField.Text;

            if (string.IsNullOrWhiteSpace(sourceId))
            {
                Toast.MakeText(this, "Please enter source ID", ToastLength.Long).Show();
                return;
            }

            if (string.IsNullOrWhiteSpace(renderingId))
            {
                Toast.MakeText(this, "Please enter rendering ID", ToastLength.Long).Show();
                return;
            }

            try
            {
                this.SetProgressBarIndeterminateVisibility(true);

                using (ISitecoreWebApiSession session = Prefs.From(this).Session)
                {
                    var request = ItemWebApiRequestBuilder.RenderingHtmlRequestWithSourceAndRenderingId(sourceId, renderingId)
                                  .Build();

                    var response = await session.ReadRenderingHtmlAsync(request);

                    if (response != null)
                    {
                        var    reader = new StreamReader(response);
                        string html   = await reader.ReadToEndAsync();

                        this.webview.LoadDataWithBaseURL(null, html, null, null, null);
                    }
                    else
                    {
                        var title = this.GetString(Resource.String.text_error);
                        DialogHelper.ShowSimpleDialog(this, title, "Failed load rendering html");
                    }
                }
                this.SetProgressBarIndeterminateVisibility(false);
            }
            catch (System.Exception exception)
            {
                this.SetProgressBarIndeterminateVisibility(false);

                var title = this.GetString(Resource.String.text_error);
                DialogHelper.ShowSimpleDialog(this, title, exception.Message);
            }
        }
        private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    var builder = ItemWebApiRequestBuilder.ReadItemsRequestWithPath(this.ItemPathField.Text)
                                  .Payload(this.currentPayloadType)
                                  .AddFieldsToRead(this.fieldNameTextField.Text);

                    if (this.parentScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Parent);
                    }
                    if (this.selfScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Self);
                    }
                    if (this.childrenScopeButton.Selected)
                    {
                        builder = builder.AddScope(ScopeType.Children);
                    }

                    var request = builder.Build();

                    this.ShowLoader();

                    ScItemsResponse response = await session.ReadItemAsync(request);

                    if (response.Any())
                    {
                        this.ShowItemsList(response);
                    }
                    else
                    {
                        AlertHelper.ShowLocalizedAlertWithOkOption("Message", "Item is not exist");
                    }
                }
            }
            catch (Exception e)
            {
                this.CleanupTableViewBindings();
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.FieldsTableView.ReloadData();
                    this.HideLoader();
                });
            }
        }
        private async Task <ISitecoreWebApiSession> GetSession()
        {
            Models.Settings settings = await _settingsRepository.GetWithFallback();

            using (IWebApiCredentials credentials = DependencyService.Get <ICustomSecureStringPasswordProvider> ().Login(settings.SitecoreUserName, settings.SitecorePassword)) {
                {
                    ISitecoreWebApiSession session = SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost(settings.RestBaseUrl)
                                                     .Credentials(credentials)
                                                     .Site(settings.SitecoreShellSite)
                                                     .DefaultDatabase(settings.SitecoreDefaultDatabase)
                                                     .DefaultLanguage(settings.SitecoreDefaultLanguage)
                                                     .MediaLibraryRoot(settings.SitecoreMediaLibraryRoot)
                                                     .MediaPrefix(settings.SitecoreMediaPrefix)
                                                     .DefaultMediaResourceExtension(settings.SitecoreDefaultMediaResourceExtension)
                                                     .BuildSession();

                    return(session);
                }
            }
        }
        private async void SendRequest()
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    var request = ItemWebApiRequestBuilder.CreateItemRequestWithParentId(this.pathField.Text)
                                  .ItemTemplatePath("Sample/Sample Item")
                                  .ItemName(this.nameField.Text)
                                  .AddFieldsRawValuesByNameToSet("Title", titleField.Text)
                                  .AddFieldsRawValuesByNameToSet("Text", textField.Text)
                                  .Build();

                    this.ShowLoader();

                    ScItemsResponse response = await session.CreateItemAsync(request);

                    if (response.Any())
                    {
                        ISitecoreItem item = response[0];
                        this.CreatedItemId = item.Id;
                        AlertHelper.ShowLocalizedAlertWithOkOption("The item created successfully", "Item path: " + item.Path);
                    }
                    else
                    {
                        AlertHelper.ShowLocalizedAlertWithOkOption("Message", "Item is not exist");
                    }
                }
            }
            catch (Exception e)
            {
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.HideLoader();
                });
            }
        }
        private async void SendImage(UIImage image)
        {
            try
            {
                using (ISitecoreWebApiSession session = this.instanceSettings.GetSession())
                {
                    Stream stream = image.AsJPEG().AsStream();

                    var request = ItemWebApiRequestBuilder.UploadResourceRequestWithParentPath(itemPathTextField.Text)
                                  .ItemDataStream(stream)
                                  .ContentType("image/jpg")
                                  .ItemName(this.itemNameTextField.Text)
                                  .FileName("imageFile.jpg")
                                  .Build();

                    this.ShowLoader();

                    var response = await session.UploadMediaResourceAsync(request);

                    if (response != null)
                    {
                        AlertHelper.ShowAlertWithOkOption("upload image result", "The image uploaded successfuly");
                    }
                    else
                    {
                        AlertHelper.ShowAlertWithOkOption("upload image result", "something wrong");
                    }
                }
            }
            catch (Exception e)
            {
                AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message);
            }
            finally
            {
                BeginInvokeOnMainThread(delegate
                {
                    this.HideLoader();
                });
            }
        }
Beispiel #21
0
        private ISitecoreWebApiSession GetSession(string userName = "", string password = "")
        {
            if (string.IsNullOrEmpty(userName))
            {
                userName = Constants.Sitecore.SitecoreUserName;
                password = Constants.Sitecore.SitecorePassword;
            }

            using (IWebApiCredentials credentials = new SecureStringPasswordProvider(userName, password))
            {
                ISitecoreWebApiSession session = SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost(Constants.Sitecore.RestBaseUrl)
                                                 .Credentials(credentials)
                                                 .Site(Constants.Sitecore.SitecoreShellSite)
                                                 .DefaultDatabase(Constants.Sitecore.SitecoreDefaultDatabase)
                                                 .DefaultLanguage(Constants.Sitecore.SitecoreDefaultLanguage)
                                                 .MediaLibraryRoot(Constants.Sitecore.SitecoreMediaLibraryRoot)
                                                 .MediaPrefix(Constants.Sitecore.SitecoreMediaPrefix)
                                                 .DefaultMediaResourceExtension(Constants.Sitecore.SitecoreDefaultMediaResourceExtension)
                                                 .BuildSession();
                return(session);
            }
        }
Beispiel #22
0
        //gets the item or child item on specifying the query
        public async Task <ScItemsResponse> GetItemByQuery(string query, string itemLanguage = "en")
        {
            try
            {
                using (ISitecoreWebApiSession session = GetSession())

                {
                    IReadItemsByQueryRequest request = ItemWebApiRequestBuilder.ReadItemsRequestWithSitecoreQuery(query).Language(itemLanguage).Build();
                    return(await session.ReadItemAsync(request));
                }
            }
            catch (SitecoreMobileSdkException ex)
            {
                Log.Error("Error in GetItemByQuery, id{0}. Error{1}", query, ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                Log.Error("Error in GetItemByQuery, id{0}. Error{1}", query, ex.Message);
                throw ex;
            }
        }
        public void TestAuthenticatedSessionShouldBeCreatedByTheBuilder()
        {
            IWebApiCredentials credentials = this.adminCredentials;

            var builder = SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost("sitecore.net")
                          .Credentials(credentials)
                          .WebApiVersion("v1")
                          .DefaultDatabase("web")
                          .DefaultLanguage("en")
                          .Site("/sitecore/shell")
                          .MediaLibraryRoot("/sitecore/media library")
                          .DefaultMediaResourceExtension("ashx");


            ISitecoreWebApiSession session = builder.BuildSession();

            Assert.IsNotNull(session);

            var roSession = builder.BuildReadonlySession();

            Assert.IsNotNull(roSession);
        }
        public async Task <Byte[]> GetMediaByUrl(string mediaUrl)
        {
            try {
                mediaUrl = CleanUpMediaUrlByReplacingWeirdTildeSignWithCorrect(mediaUrl);

                using (ISitecoreWebApiSession session = await SitecoreSession)
                {
                    IMediaResourceDownloadRequest request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(mediaUrl)
                                                            .Language("en")
                                                            .Build();

                    byte[] data = null;

                    using (Stream response = await session.DownloadMediaResourceAsync(request))

                        using (MemoryStream responseInMemory = new MemoryStream())
                        {
                            await response.CopyToAsync(responseInMemory);

                            data = responseInMemory.ToArray();

                            return(data);
                        }
                }
            }
            catch (SitecoreMobileSdkException ex)
            {
                this._loggingService.Log("Error in GetMediaByUrl,  url {0} . Error: {1}", mediaUrl, ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                this._loggingService.Log("Error in GetMediaByUrl,  url {0} . Error: {1}", mediaUrl, ex.Message);
                throw ex;
            }
        }
 public NoThrowWebApiSession(ISitecoreWebApiSession workerSession)
 {
     this.workerSession = workerSession;
 }
    private async Task<ISitecoreItem> CreateItem(string itemName, ISitecoreItem parentItem = null, ISitecoreWebApiSession itemSession = null)
    {
      if (itemSession == null)
      {
        itemSession = session;
      }
      string parentPath = parentItem == null ? this.testData.Items.CreateItemsHere.Path : parentItem.Path;
      var request = ItemWebApiRequestBuilder.CreateItemRequestWithParentPath(parentPath)
        .ItemTemplatePath(testData.Items.Home.Template)
        .ItemName(itemName)
        .Build();
      var createResponse = await itemSession.CreateItemAsync(request);

      Assert.AreEqual(1, createResponse.ResultCount);
      return createResponse[0];
    }
 public void TearDown()
 {
     this.testData = null;
     this.session.Dispose();
     this.session = null;
 }
Beispiel #28
0
        private async Task <ISitecoreItem> CreateItem(string itemName, ISitecoreItem parentItem = null, ISitecoreWebApiSession itemSession = null)
        {
            if (itemSession == null)
            {
                itemSession = session;
            }
            string parentPath = parentItem == null ? this.testData.Items.CreateItemsHere.Path : parentItem.Path;
            var    request    = ItemWebApiRequestBuilder.CreateItemRequestWithParentPath(parentPath)
                                .ItemTemplatePath(testData.Items.Home.Template)
                                .ItemName(itemName)
                                .Build();
            var createResponse = await itemSession.CreateItemAsync(request);

            Assert.AreEqual(1, createResponse.ResultCount);
            return(createResponse[0]);
        }
 public void TearDown()
 {
   this.testData = null;
   this.session.Dispose();
   this.session = null;
 }