Rizo's

Tag: Poll

Making Polls with Xforms in a usercontrol

by on dec.06, 2010, under Developing

XForm is a somewhat of a lazy way out. You know you should do the forms yourself but XForm is simple for the editors and also saves you a lot of job. Anyway, if you want to use XForm to make a poll, you’ll need to make a few adjustments.

First:
Create your new control and add an XForm property to the controls pagetype

Second:
Here you can take the template code from EPiServer to get the basic XForm functions.

Third:
Create your XForm in EPiServer and assign it to your page. Make sure to let anonymous post and also multiple times (Because all your users will be anonymous and you want anonymous to be able to vote more than once)

Fourth:
Create a cookie so people only vote once (they can delete their cookies and vote over and over so if you want a more secure way, go ahead and do it =D) and also so you can display the results insetad of the poll whenever they visit the page again.

The cookie could look something like this:
[sourcecode lang=”c#”]
private void SetCookie()
{
var cookie = new HttpCookie(”Poll-” + page.PageLink.ID);
cookie.Values.Add(”voted”, ”true”);
cookie.Expires = DateTime.Now.AddYears(24);
Response.Cookies.Add(cookie);
}
[/sourcecode]
and then you can check for the cookie and give the user different views depending if they voted or not. And that code could look something like this
[sourcecode lang=”c#”]
return;
var cookie = Request.Cookies.Get(”Poll-” + page.PageLink.ID);
if (cookie== null)
SetupForm();
else
SwitchView(null,null);
[/sourcecode]
Fifth:
If you’re using your XForm directly on a page, then you probably don’t need this step and you’re pretty much set. But if you want to use this as a usercontrol and add the poll with webparts or anything alike you’ll need to do this step.

The thing with EPiServers Statistics is that it will always fetch ”this.PageLink” so if you’re using this as a webpart/control it will try to get the currentpages xform property which will be null. Even if you load the XForm correctly and fetch it from your usercontrol path, the statistics won’t do it. In order to make Statistics work you’ll have to do this:
[sourcecode lang=”c#”]
Statistics.PageLink = page.PageLink;
Statistics.PageGuid = Form.PageGuid;
[/sourcecode]
Otherwise it will take the currentpage pagelink and pageguid. So basicly, you need to set up the Statistics just as you need to set up XForms.

Good luck!

18 Comments :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!