WFFM form POST and redirect - sitecore

I've roughly followed this example but it doesnt solve my Sitecore-related redirect problem.
sitecore web form for marketers form post to external url
I've confirmed that my form POST works properly by using a 3rd party POST test tool. The problem I'm having is that in Sitecore they use a successMode to determine what the user wants todo if the submit is a success. If the user selects successmode/message, the form redirects back to a thank you message. If the user selects successmode/redirect, the success method pipeline looks for the success page value in the form and then a redirect happens to that URL. The problem with the redirect is that it loses my POST data.
Can anyone provide a Sitecore example of how they executed a form POST, and then redirect to the target external URL without losing POST values?
Did you use the successmode settings in the form?
I'm debating whether to overrride the successmode redirect pipeline, add conditions and test but I'm open to a solution that could include jquery.
Here's is my code:
using Sitecore.Data;
using Sitecore.Form.Core.Client.Data.Submit;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Form.Submit;
using System.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Text;
using Sitecore.Forms.Core.Data;
using Sitecore.Form.Core.Configuration;
using Sitecore.Forms.Core.Crm;
using System;
using System.IO;
using System.Net;
using Sitecore.Diagnostics;
using System.Text;
namespace XXXWffmExternals
{
public class Redirect : ISaveAction
{
UrlString url = new UrlString("https://XXX.XXX/default.asp");
public virtual void Execute(ID formid, AdaptedResultList fields, params object[] data)
{
String strResult = "";
strResult = setPost(url.ToString(), fields);
}
public String setPost(string url, AdaptedResultList fieldListForPOST)
{
String resultReturn = "";
AdaptedControlResult firstname = fieldListForPOST.GetEntry(this.First_Name, "First_Name");
AdaptedControlResult lastname = fieldListForPOST.GetEntry(this.Last_Name, "Last_Name");
AdaptedControlResult billingaddress = fieldListForPOST.GetEntry(this.Billing_Address, "Billing_Address");
AdaptedControlResult billingcity = fieldListForPOST.GetEntry(this.Billing_City, "Billing_City");
AdaptedControlResult billingstate = fieldListForPOST.GetEntry(this.Billing_State, "Billing_State");
AdaptedControlResult billingzip = fieldListForPOST.GetEntry(this.Billing_Zip, "Billing_Zip");
AdaptedControlResult billingphone = fieldListForPOST.GetEntry(this.Billing_Phone, "Billing_Phone");
AdaptedControlResult email = fieldListForPOST.GetEntry(this.Email, "Email");
AdaptedControlResult amount = fieldListForPOST.GetEntry(this.Amount, "Amount");
AdaptedControlResult desc = fieldListForPOST.GetEntry(this.Description, "Description");
AdaptedControlResult login = fieldListForPOST.GetEntry(this.Login, "Login");
AdaptedControlResult acct = fieldListForPOST.GetEntry(this.Account, "Account");
AdaptedControlResult fund = fieldListForPOST.GetEntry(this.Fund, "Fund");
AdaptedControlResult org = fieldListForPOST.GetEntry(this.Org, "Org");
AdaptedControlResult source_code = fieldListForPOST.GetEntry(this.Source_Code, "Source_Code");
String post =
"First_Name=" + firstname.Value +
"&Last_Name=" + lastname.Value +
"&Billing_Address=" + billingaddress.Value +
"&Billing_City=" + billingcity.Value +
"&Billing_State=" + billingstate.Value +
"&Billing_Zip=" + billingzip.Value +
"&Billing_Phone=" + billingphone.Value +
"&Email=" + email.Value +
"&Amount=" + amount.Value +
"&Description=" + desc.Value +
"&Login=" + login.Value +
"&Account=" + acct.Value +
"&Fund=" + fund.Value +
"&Org=" + org.Value +
"&Invoice_Num=" + "DVXXXX";
resultReturn = sendPost(url.ToString(), post);
return resultReturn;
}
public String sendPost(string url, string post)
{
String result = "";
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
// Set credentials to use for this request.
objRequest.Credentials = CredentialCache.DefaultCredentials;
// Convert POST data to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(post);
// Set the ContentLength property of the WebRequest.
objRequest.ContentLength = byteArray.Length;
// Set the ContentType property of the WebRequest.
objRequest.ContentType = "application/x-www-form-urlencoded";
// Get the request stream.
Stream dataStream = objRequest.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = objRequest.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
result = reader.ReadToEnd ();
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
return result;
}
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Billing_Address { get; set; }
public string Billing_City { get; set; }
public string Billing_State { get; set; }
public string Billing_Zip { get; set; }
public string Billing_Phone { get; set; }
public string Email { get; set; }
public string Amount { get; set; }
public string Description { get; set; }
public string Login { get; set; }
public string Account { get; set; }
public string Fund { get; set; }
public string Org { get; set; }
public string Invoice_Num { get; set; }
public string Source_Code { get; set; }
}
}

Why do you want to use WFFM for the form if you don't want to invoke any of the WFFM functionality? The point of WFFM is to allow marking people to create their own forms without any Developer input. You're having to edit all the post data in your code which pretty much eliminates the ability for anyone to edit the form without developer input. I'd say, if you're going to go through the process of writing all the code to submit your code by hand, you can use a Sitecore item to create the form and then use your own code for processing. Skip WFFM. It's a lot more work to do things as you suggested than to just manually create a form.
If you really need any WFFM end functionality, you can easily call them... it's still way easier than trying to override WFFM base functionality to inject your own functionality.

Related

how to convert List<KeyValuePair<string,object>> to class, model in .Net Core

I have got response List<List<KeyValuePair<string, object>>> but I want to convert response into List<ClassName>.
KeyValuePair key and ClassName property both are same name and same type
What is the most programmatically way to convert response?
I have got response
My class structure
public class TestModel
{
public string TaxablePersonCode { get; set; }
public string LegalNameAsPerPan { get; set; }
public string TradeName { get; set; }
public string ConstitutionName { get; set; }
public string ResidentialStatusName { get; set; }
public string PrimaryMobileNo { get; set; }
public string FlatOrOfficeNo { get; set; }
public string TownOrCityOrDist { get; set; }
public string Pincode { get; set; }
public string StateName { get; set; }
public string CountryName { get; set; }
public string ContactName { get; set; }
public string ContactDesignationName { get; set; }
public string ContactMobile { get; set; }
public string ContactEmail { get; set; }
}
var listKeyValue = response.Select(x => x.Value).ToList();
var data = JsonConvert.DeserializeObject<List<TestModel>>(listKeyValue);
The only part of this im unsure about is when indexing the final list for the correct property, but you can choose what works best for you. Using a .Where() every time ensures you'll get the right result but it will search the list every single time and be a lot slower. If your %100 certain the order of the list will never change you could gain some performance by directly indexing the list for the element you want using [] or .ElementAt(). Anyway, heres what your looking for.
List<TestModel> myList = response.Select(x => new TestModel
{
// Using Where
TaxablePersonCode = x.Where(t => t.Key == "TaxablePersonCode").First().Value,
// Using direct index
LegalNameAsPerPan = x[1].Value,
// Using ElementAt
TradeName = x.ElementAt(2).Value,
...
});
Hope that helps!
Did you get some result with JsonConvert class? Did it worked for you? If not, you can try out something like this (if JSON and field are properly named):
var listKeyValue = response.Select(x => x.Value).ToList();
var result = new List<TestModel>();
foreach (var keyValueList in listKeyValue)
{
// convert the response list of KeyValuePair to dictionary
var dictionary = keyValueList.ToDictionary(kv => kv.Key, kv => kv.Value);
var tempModel = new TestModel();
// get actual value by name of the rpoperty
tempModel.TaxablePersonCode = dictionary[nameof(tempModel.TaxablePersonCode)].ToString();
// etc.
result.Add(tempModel);
}
Maybe this approach could be improved with reflection, but this will degrade the performance.
// get all properties to populate
var properties = typeof(TestModel).GetProperties(BindingFlags.Public | BindingFlags.Instance);
I see you're using JSON, so you should probably just deserialize the object properly, which I would expect to look something like this:
List<TestModel> models = JsonConvert.DeserializeObject<List<TestModel>>(response);
Otherwise, you could use reflection to bind the known KeyValuePair keys to the properties of the object; that being said, you will need to ensure that the return values are compatible with the values from the returned data, else this will fail.
outerList.ForEach(innerList => {
TestModel result = new TestModel();
innerList.ForEach(listItem => {
result
.GetType()
.GetProperty(listItem.Key)
?.SetValue(result, listItem.Value);
});
});

RavenDB: Why do I get null-values for fields in this multi-map/reduce index?

Inspired by Ayende's article https://ayende.com/blog/89089/ravendb-multi-maps-reduce-indexes, I have the following index, that works as such:
public class Posts_WithViewCountByUser : AbstractMultiMapIndexCreationTask<Posts_WithViewCountByUser.Result>
{
public Posts_WithViewCountByUser()
{
AddMap<Post>(posts => from p in posts
select new
{
ViewedByUserId = (string) null,
ViewCount = 0,
Id = p.Id,
PostTitle = p.PostTitle,
});
AddMap<PostView>(postViews => from postView in postViews
select new
{
ViewedByUserId = postView.ViewedByUserId,
ViewCount = 1,
Id = (string) postView.PostId,
PostTitle = (string) null,
});
Reduce = results => from result in results
group result by new
{
result.Id,
result.ViewedByUserId
}
into g
select new Result
{
ViewCount = g.Sum(x => x.ViewCount),
Id = g.Key.Id,
ViewedByUserId = g.Key.ViewedByUserId,
PostTitle = g.Select(x => x.PostTitle).Where(x => x != null).FirstOrDefault(),
};
Store(x => x.PostTitle, FieldStorage.Yes);
}
public class Result
{
public string Id { get; set; }
public string ViewedByUserId { get; set; }
public int ViewCount { get; set; }
public string PostTitle { get; set; }
}
}
I want to query this index like this:
Return all posts including - for a given user - the integer of how many times, the user has viewed the post. The "views" are stored in a separate document type, PostView. Note, that my real document types have been renamed here to match the example from the article (I certainly would not implement "most-viewed" this way).
The result from the query I get is correct - i.e. I always get all the Post documents with the correct view-count for the user. But my problem is, the PostTitle field always is null in the result set (all Post documents have a non-null value in the dataset).
I'm grouping by the combination of userId and (post)Id as my "uniqueness". The way I understand it (and please correct me if I'm wrong), is, that at this point in the reduce, I have a bunch of pseudo-documents with identical userId /postId combination, some of which come from the Post map, others from the PostView map. Now I simply find any single pseudo-document of the ones, that actually have a value for PostTitle - i.e. one that originates from the Post map. These should all obviously have the same value, as it's the same post, just "outer-joined". The .Select(....).Where(....).FirstOrDefault() chain is taken from the very example I used as a base. I then set this ViewCount value for my final document, which I project into the Result.
My question is: how do I get the non-null value for the PostTitle field in the results?
The problem is that you have:
ViewedByUserId = (string) null,
And:
group result by new
{
result.Id,
result.ViewedByUserId
}
into g
In other words, you are actually grouping by null, which I'm assuming that isn't your intent.
It would be much simpler to have a map/reduce index just on PostView and get the PostTitle from an include or via a transformer.
You understanding of what is going on is correct, in the sense that you are creating index results with userId / postId on them.
Buit what you are actually doing is creating results from PostView with userId /postId and from Post with null /postId.
And that is why you don't have the matches that you want.
The grouping in the index is incorrect. With the following sample data:
new Post { Id = "Post-1", PostTitle = "Post Title", AuthorId = "Author-1" }
new PostView { ViewedByUserId = "User-1", PostId = "Post-1" }
new PostView { ViewedByUserId = "User-1", PostId = "Post-1" }
new PostView { ViewedByUserId = "User-2", PostId = "Post-1" }
The index results are like this:
ViewCount | Id | ViewedByUserId | PostTitle
--------- | ------ | -------------- | ----------
0 | Post-1 | null | Post Title
2 | Post-1 | User-1 | null
1 | Post-1 | User-2 | null
The map operation in the index simply creates a common document for all source documents. Thus, the Post-1 document produces one row, the two documents for Post-1 and User-1 produce two rows (which are later reduced to the single row with ViewCount == 2) and the document for Post-1 and User-2 produces the last row.
The reduce operation the groups all the mapped rows and produces the resulting documents in the index. In this case, the Post-sourced document is stored separately from the PostView-sourced documents because the null value in the ViewedByUserId is not grouped with any document from the PostView collection.
If you can change your way of storing data, you can solve this issue by storing the number of views directly in the PostView. It would greatly reduce duplicate data in your database while having almost the same cost when updating the view count.
Complete test (needs xunit and RavenDB.Tests.Helpers nugets):
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
using System.Linq;
using Xunit;
namespace SO41559770Answer
{
public class SO41559770 : RavenTestBase
{
[Fact]
public void SO41559770Test()
{
using (var server = GetNewServer())
using (var store = NewRemoteDocumentStore(ravenDbServer: server))
{
new PostViewsIndex().Execute(store);
using (IDocumentSession session = store.OpenSession())
{
session.Store(new Post { Id = "Post-1", PostTitle = "Post Title", AuthorId = "Author-1" });
session.Store(new PostView { Id = "Views-1-1", ViewedByUserId = "User-1", PostId = "Post-1", ViewCount = 2 });
session.Store(new PostView { Id = "Views-1-2", ViewedByUserId = "User-2", PostId = "Post-1", ViewCount = 1 });
session.SaveChanges();
}
WaitForAllRequestsToComplete(server);
WaitForIndexing(store);
using (IDocumentSession session = store.OpenSession())
{
var resultsForId1 = session
.Query<PostViewsIndex.Result, PostViewsIndex>()
.ProjectFromIndexFieldsInto<PostViewsIndex.Result>()
.Where(x => x.PostId == "Post-1" && x.UserId == "User-1");
Assert.Equal(2, resultsForId1.First().ViewCount);
Assert.Equal("Post Title", resultsForId1.First().PostTitle);
var resultsForId2 = session
.Query<PostViewsIndex.Result, PostViewsIndex>()
.ProjectFromIndexFieldsInto<PostViewsIndex.Result>()
.Where(x => x.PostId == "Post-1" && x.UserId == "User-2");
Assert.Equal(1, resultsForId2.First().ViewCount);
Assert.Equal("Post Title", resultsForId2.First().PostTitle);
}
}
}
}
public class PostViewsIndex : AbstractIndexCreationTask<PostView, PostViewsIndex.Result>
{
public PostViewsIndex()
{
Map = postViews => from postView in postViews
let post = LoadDocument<Post>(postView.PostId)
select new
{
Id = postView.Id,
PostId = post.Id,
PostTitle = post.PostTitle,
UserId = postView.ViewedByUserId,
ViewCount = postView.ViewCount,
};
StoreAllFields(FieldStorage.Yes);
}
public class Result
{
public string Id { get; set; }
public string PostId { get; set; }
public string PostTitle { get; set; }
public string UserId { get; set; }
public int ViewCount { get; set; }
}
}
public class Post
{
public string Id { get; set; }
public string PostTitle { get; set; }
public string AuthorId { get; set; }
}
public class PostView
{
public string Id { get; set; }
public string ViewedByUserId { get; set; }
public string PostId { get; set; }
public int ViewCount { get; set; }
}
}

Extract mail list addresses from MailGun

I want to extract the addresses OUT OF MailGun, into a CSV and delete the MailGun altogether.
The MailGun database is the only copy of the 951 addresses I have absolutely no access to the database in any form other than looking at the list in MailGun.
http://documentation.mailgun.com/api-mailinglists.html#mailing-lists
This is my solution to this problem in C# ,and it can get all members of a list(not only 100 limited).In addition, i use Newtonsoft.Json. Update: I found the 'total_count' has a maximum value 10000, so if the members in your mailing list is more than 10000, the request will return the maximum value 10000! In that case, this is not a great solution!
public void ExportMailList(string listName)
{
RestClient client = new RestClient();
List<MemberDetail> totalMember = new List<MemberDetail>();
client.BaseUrl = new Uri("https://api.mailgun.net/v3");
client.Authenticator =
new HttpBasicAuthenticator("api",
"key-yourKey");
RestRequest reqForTotal = new RestRequest();
reqForTotal.Resource = "lists/{list}/members";
reqForTotal.AddParameter("list", listName, ParameterType.UrlSegment);
int resultTotal= JsonConvert.DeserializeObject<Member>(client.Execute(reqForTotal).Content).total_count;
int skipTimes = resultTotal / 100;
for (int i = 0; i <= skipTimes; i++)
{
RestRequest request = new RestRequest();
request.Resource = "lists/{list}/members";
request.AddParameter("list", listName, ParameterType.UrlSegment);
request.AddParameter("skip",100*i);
totalMember.AddRange(JsonConvert.DeserializeObject<Member>(client.Execute(request).Content).items);
}
//CreateCSVFromGenericList(...);
}
public class Member
{
public List<MemberDetail> items { get; set; }
public int total_count { get; set; }
}
public class MemberDetail
{
public string address { get; set; }
public string name { get; set; }
public bool subscribed { get; set; }
public object vars { get; set; }
}
In the method CreateCSVFromGenericList(..), you can refer to this blog, and export the data to .csv file in any format you want.
Using curl,php or any other programming language you can achieve this, then simply delete the mailing list through the control panel:
curl -s --user 'api:YOURAPIKEY' -G \
https://api.mailgun.net/v2/lists/Your#MailingListName.com/members
Apikey and mailing list name are available from the control panel!
In python :
def get_members():
return requests.get(
"https://api.mailgun.net/v3/lists/LIST_NAME/members",
auth=("api", "key-YOUR_KEY"),
data={'limit': 100, 'skip': 1380})
Note that limit and skip are useful to paginate through your member list.

Custom Data-annotation not firing in unit-test

I am currently experimenting with validation attributes,
and now I am trying to validate my ViewModel which contains an EmailAddress with a custom validation attribute.
public class UserLoginModel
{
[Required]
[EmailAddress]
public string email { get; set; }
[Required]
public string password { get; set; }
public bool rememberMe { get; set; }
}
I have made a unit-test where I give a false email address and try to validate my viewmodel.
[TestMethod]
public void TestingInvalidEmailAddress()
{
UserLoginModel model = new UserLoginModel();
model = GetAValidLoginModel(); //Get a default-model where all parameters are correct
model.email = "thisisnotavalidemail.com";
ValidationContext context = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();
bool validModel= Validator.TryValidateObject(model, context, results);
//This is always true
Assert.IsFalse(validModel);
}
The result of this test is always False.
So I checked my attribute, because I thought I might have made a mistake:
[TestMethod]
public void Email()
{
string email;
var attr = new EmailAddressAttribute();
email = "myemail#domain.com";
Assert.IsTrue(attr.IsValid(email));
email = "thisisnotavalidemail.com";
Assert.IsFalse(attr.IsValid(email)); //If this fails, the test is successfull
}
And that did pass the test, using the exact same email address.
And when I test it in my browser, it also validates correctly.
So why does it not tell me that my email address is invalid in the first test-method?
I found my solution in over here.
Apparently I am just missing an extra parameter.
bool validModel= Validator.TryValidateObject(model, context, results, **true**);

Asp.Net Mvc Consume Url posting json data

I was wondering if anyone had any pointers for parsing json data consumed from a URL in Asp.Net. I've found plenty of docs about Model Binding json datatypes but this is coming from a URL and I cant seem to find an example for that. The closest thing I've found is datacontractjsonserializer but again, I cant seem to find an example of that in context with a URL outputting the json data. Any help is appreciated.
You could use the JavaScriptSerializer class. You start by defining a model class which will hold the data. So let's suppose that the remote URL returns the following JSON:
{ name: 'John', addresses: [ { city: 'Paris' }, { city: 'London' } ] }
which could be represented by this model:
public class Person
{
public string Name { get; set; }
public Address[] Addresses { get; set; }
}
public class Address
{
public string City { get; set; }
}
And then deserialize the received JSON back to the model:
var serializer = new JavaScriptSerializer();
// TODO: Fetch the JSON from a remote URL
var json = "{name: 'foo', addresses: [{city: 'Paris'}, {city: 'London'}]}";
var person = serializer.Deserialize<Person>(json);
UPDATE:
In order to fetch the JSON from remote url you could use WebClient:
using (var client = new WebClient())
{
string json = client.DownloadString("http://someurl.com");
}
Here is what I have so far. A product of all answers that I get here in stack.
The idea is to get the json value from external web service and publish it in my controller as a json values and I dont have to create model for it. Hope this helps.
public class ApiJson: Controller
{
public JsonResult getUser()
{
WebClient client = WebClient();
NameValueCollection data = new NameValueCollection();
data.Add("param1", "value1");
byte[] result = client.UploadValues("http://localhost:9000/", data);
String json = Encoding.ASCII.GetString(result);
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>(json);
return Json(item, JsonRequestBehavior.AllowGet);
}
}