How do access the tab details in C# from Umbraco 7? - c#

Please see following image.
Umbraco7 screen
I am using Umbraco 7. You can see in the image that I have 'General Messages' tab.
I have saved all error messages in that and i need to access these error messages from the code, can I do that in Csharp ?

I'm going to assume that you have a template assigned to the sign up page, and that you want to get at the messages on that View.
That being the case, you can use either:
#Umbraco.Field("yourPropertyAliasHere")
or
#Model.Content.GetPropertyValue("yourPropertyAliasHere")
The main difference is that Umbraco.Field has a bunch of useful additional parameters for things like recursive lookup.
If you want get at the properties from within some random C# in your Umbraco site that isn't related t the actual signup page, assuming you have an Umbraco Helper, you can do the following:
var page = Umbraco.TypedContent(1234); //replace '1234' with the id of your signup page!
var message = page.GetPropertyValue<string>("yourPropertyAliasHere");

Related

Blazor Server download file with parameters

I have a Blazor Server app where the users can use filters to see a list of movies. For example, they can specify the year, the country and etc. and movies that match the criteria will be displayed on the page (a list of Movie objects).
I want to give users the ability to save the displayed list as a text file. I know how to make an HTTP Get request to another Razor page or an MVC or API controller to return a predefined file but am struggling with passing any arguments to the endpoint.
Initially I thought of using HttpClient.PostAsJsonAsync to send a POST request to an API controller, passing the list as the request body, which works fine in the sense that if I put a breakpoint in the controller method it is hit but nothing is returned to the user.
If I use an <a> element or a button onClick method with NavigationManager.NavigateTo it works fine for a predetermined file HTTP Get request but how can I serve the users with a file that consists of the list of movies/objects they are seeing on the browser?
I.e. How can I either pass arguments with NavigationManager.NavigateTo or using an <a> element or any other way in order to send data to the endpoint/server which it then can use to generate a file and return that file to the user for saving?
Mock code to give an idea of what I'd like. This is just a design/idea reference, doesn't have to be WebApi, MVC or whatever. I just need something that would work with Blazor:
[HttpPost]
File WebApiMethodThatDoesTheWork(CustomObject data)
{
StringBuilder stringBuilder = new();
foreach (var item in data.Items)
{
stringBuilder.Append(item);
}
File.WriteAllText("test.txt", stringBuilder.ToString());
return File("test.txt");
}
The only two requirements I have about how the file will be downloaded are: it should be user-friendly, meaning the usual "Do you want to open or save this file?" dialog box would be perfect AND it shouldn't use the GET request to a Razor page to retrieve the file UNLESS a single Razor page can be used to download many different file types. I.e. if a user wants to download a list of movies, while another a list of songs. With my current knowledge I think for such a case I'd have to have 2 different Razor pages, which is suboptimal.
I don't have a complete packaged up answer because mine is old and Blazor works differently now, but the underlying principal is the same.
Step 1 - JS Interop call to URL.createObjectURL(new Blob([{your data}]))
Step 2 - Render an anchor tag with the resulting object url as the href and a download attribute.
<a id="download" href=#BlobUrl download=#FileName>Download File</a>
Step 3 - Optionally JS Interop call to click the rendered anchor tag so the user doesn't have to.
Whether the user sees a Save As.. dialog or not depends on their browser/operating system - you can't control that (There is a downloads api that has saveas functionality, but it's not fully supported)
You could try re-filtering the list using that file and upon start-up you can check if they have that file... there's lots of options you can chose from

Install ReCaptcha to Umbraco V7

I would like to install ReCaptcha to our Umbraco Version 7. But I can't seem to find the correct approach. Basically I want to add the captcha element inside my custom form (not the Umbraco Form). Is there a way to do it? It seems the approach of adding the Recaptcha is not the same as how you add it in PHP application. How should I do this in Umbraco?
Update:
Recaptcha version can either be version 1, 2 or 3
reCaptcha V2 only requires a few simple lines of HTML to be inserted, AFAIK. You should be able to insert both a script tag and an HTML element anywhere inside your form element (inside Html.BeginForm if that's what you're doing) in your custom form, as long as you have an API key. I did this the other day and it just worked.
https://developers.google.com/recaptcha/docs/display
It doesn't need "installation", but it requires some fiddling with code.
I wrote a post about it here a while back, https://www.jondjones.com/learn-umbraco-cms/umbraco-7-tutorials/umbraco-security/how-to-add-a-recapture-within-your-umbraco-website/
The quick option is to install recaptha mvc via Nuget and then decorate your controller with CaptchaValidator and use the Recaptcha in your HTML
In your controller just check the form values and get the captcha value from it like this below, if that value is null then the person hasn't filled it otherwise it will have a value in it.
var formData = Request.Form;
var captchaRequest = formData["g-recaptcha-response"];
if (string.IsNullOrWhiteSpace(captchaRequest))
{
TempData["formError"] = "Fill in the Captcha box.";
return CurrentUmbracoPage();
}
Ok I found a solution with detailed explanation in this Blog

Moving data from one web form to another ASP.NET C#

I am trying to move the content of a textbox on the from StudentRegistration to the form MyProfile by following a tutorial on YouTube. However when I try to reference the StudentRegitration Page in my code, I get the error that the type or namespace cannot be found.
In the tutorial I can see that in their code they have a namespace, however my website does not. Could anyone tell me what to do in order to be able to reference StudentRegistration without getting an error?
I should have stated that I have a website not a web app. I have found that websites do not have a default namespace. How would I go about accessing the StudentRegistration without referencing a namespace?
public partial class MyProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
StudentRegistration LastPage = (StudentRegistration)Context.Handler;
lblEmail.Text = StudentRegistration.STextBoxEm;
}
}
}
Rather than answer your question directly, I'd like to point out another issue with your code that will probably prevent it from working. You should refer to the documentation on the PreviousPage property at: http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage%28v=vs.110%29.aspx
It does NOT work like this:
user visits /StudentRegistration.aspx
user does stuff
user submits the form on /StudentRegistration.aspx
server redirects the user to /MyProfile.aspx
MyProfile class knows that PreviousPage = the class from /StudentRegistration.aspx
Instead, the description from the msdn reference page linked above stipulates that the PreviousPage property only works on this scenario:
user visits /StudentRegistration.aspx
user does some stuff
user submits form on /StudentRegistration.aspx
server transfers request to the MyProfile class
this does not mean that the url has changed to /MyProfile.aspx for the user, this means that the server is going to treat the current request to /StudentRegistration.aspx as if it were actually a request to /MyProfile.aspx
the user ends up seeing the result of what would normally be /MyProfile.aspx on /StudentRegistration.aspx
Now, your code may actually want that, but the fact that you have:
if (PreviousPage != null)
{
StudentRegistration LastPage = (StudentRegistration)Context.Handler;
// this should be
// StudentRegistration LastPage = (StudentRegistration)PreviousPage;
}
makes me think that you have misinterpreted the somewhat misleadingly named PreviousPage property. For a sample of how to persist state across multiple page loads in .NET, I would recommend reading up on SessionState. It has a somewhat complicated name, but does more of what you would want in this scenario:
http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.100%29.aspx
An added bonus is that you do not need to reference one class from another, so you fix your current bug later on. Additionally, even if you did resolve your potential namespace error, the issue that I outlined earlier will cause the value of the text field to be blank if your code is working as I suspect.
You are sending data from a source to a target - e.g. StudentRegistration -> MyProfile
You have options because at the end of the day, it is HTTP. Aside from "persistence" (Session), and the tutorial you are following, a "simpler" way is to use ButtonPostBackUrl.
All it means is that you are POSTing data to the target page. The target page (MyProfile) will have to validate and parse the posted data (Request.Form). This way you don't have to manage things like Session state.

Pass values from User Control (MVC 2, Json)

these are the following technologies i am utilizing in my current project:
- WCF
- MVC 2
- Json
I'm just new with MVC 2 and Json. My question is, how do you pass values from a User Control back to the page where it was called?
Please answer my question or give me any reference links wherein i can extract ideas.
Thanks.
I suggest, Keep the user details in session when your user control finds a user. then access that session variable from your controller or any place of your application.
//After your user controller find the User
Session.add("userName",UserNameProperty);
from your controller//
if(Session("userName") != null){
string _uName = Session("userName").ToString()
// Do your logic
}

How to use Skin.AddPageMessage() method?

I am developing a DNN module and I want to display an info message at the top of my ContentPane, not above the actual module. I have found that DotNetNuke.UI.Skins.Skin.AddPageMessage() should just do the thing. I am not getting the behavior I want though, the message just won't display at all.
There are few overloads of this method, one group accepting a Page object, the other one taking a Skin object.
public static void AddPageMessage(Page page, string heading, string message, ModuleMessage.ModuleMessageType moduleMessageType)
public static void AddPageMessage(Skin skin, string heading, string message, ModuleMessage.ModuleMessageType moduleMessageType)
I did take a look into the DNN source and found out that in the end they're actually using the same private static AddPageMessage(...) method, which just looks for a ContentPane within the provided control and adds a new ModuleMessage to the collection of its controls.
What should I pass as a Page or Skin parameter to get this correclty working?
Thanks ...
The private AddPageMessage method takes a fairly ambiguous "Control" as the first parameter. I believe that needs to be the current Skin, as it does a FindControl for ContentPane.
Doing something like this should get you a reference to the current skin:
var skin = Skin.GetSkin((PageBase)this.Page);
Skin.AddPageMessage(skin, "Header", "Message", ModuleMessageType.GreenSuccess);
the reason why the messages are not showing up is that you turned on "enable partial rendering" in the controlssetting of the modulecontrols.
If you are using AJAX (this is happening if you set the partial rendering to true) the DNN modulemessages are turned off from DNN itselfe.
Its enough if you have turned the partial rendering on just 1 control (dont have to be your control where you are acting from) on your page. DNN will wrap the whole page into ajax script manager and messages are not working anymore.
*EDIT 26.04.2012 10:45:
You can get the current ScriptManager by executing the following Code for example in you Page_Load(). If the manager is null, you dont have ajax enabled and the modulemessages should work. If bIsAjaxEnabled is true the modulemessages are disabled.
ScriptManager manager = AJAX.GetScriptManager(Page);
if (manager != null)
{
bool bIsAjaxEnabled = manager.SupportsPartialRendering;
}

Categories

Resources