Friday, March 25, 2011

How to debug silverlight webpart in sharepoint 2010.?

SharePoint Silverlight debugging in Visual Studio is not supported by default, due to Script debugging settings turned on. It means that you can’t set breakpoints and step into your code in VS if you are not deploying Silverlight controls via feature. Such functionality is supported when you host your SL project in external Web Project (another reason not to host in SharePoint during development)



To provide the SharePoint SL debugging capabilities you need to package your .XAP file in SharePoint Project (in VS) and deploy as a Feature. Such approach allows to enable “Siverlight Debugging” instead of client one, and step into your .cs code from Visual Studio 2010. This options is set from the VS IDE. Navigate to the SharePoint project properties –> SharePoint Tab –> click “Enable Silverlight Debugging”. That checkbox will activate the Remote Debugger and you will be able to debug SL hosted in SharePoint using Visual Studio 2010 (but you need to configure the Remote Debugger first, opening the firewall ports. On the 64bit windows you need to configure the x86 version of msvsmon.exe because the Visual Studio is x86)


Note: Debugging doesn’t work if you don’t have another instance of the the SL page opened in browser. Seems there is a bug resolving assemblies, because when you start debugging your solution might be re-builded , IIS restarts, thus remote debugger can’t attach to the right assemblies. To fix it just open another tab in browser, navigate to page with SL control you are debugging and only after that start debugging which will open the second instance of that page.

 Debug both silverlight and javascript together

Sometimes you need to debug both silverlight and javascript together, especially when you’re doing HTML bridge development where requests seamlessly pass between SL and JS

  1. Use Chrome instead of IE,
  2. Rig your SharePoint project to use Silverlight debugging,
  3. Inside Chrome press CTRL_ALT_J to open the javascript debugger, and use chrome’s SL debugger.


Thursday, March 24, 2011

SP 2010 PowerShell Commands

Windows PowerShell is a command-line scripting tool that provides an administrator full access to applicable application programming interfaces (APIs), along with the ability to unlock the capability to interact directly with SharePoint 2010 Products to manipulate Web applications, site collections, sites, lists and much more. In addition, the administrator has the ability to script cmdlets (pronounced "command-lets"), which makes for an improved experience from past product versions.



Windows PowerShell 2.0 is a pre-requisite for installing SharePoint 2010 Products. It will be installed, if necessary, when you run the Microsoft SharePoint Products Preparation Tool. By default, Windows PowerShell is located at the following path: <%SystemRoot%>\System32\WindowsPowerShell\v1.0\PowerShell.exe.


Windows PowerShell 2.0 includes the following new features.


  Remoting
   
      Windows PowerShell 2.0 lets you run commands on one or many remote
      computers with a single Windows PowerShell command. You can run
      individual commands, or you can create a persistent connection
      (a session) to run a series of related commands. You can also start a
      session with a remote computer so that the commands you type run
      directly on the remote computer.


      The remoting features of Windows PowerShell are built on Windows
      Remote Management (WinRM). WinRM is the Microsoft implementation of
      the WS-Management protocol, a standard SOAP-based, firewall-compatible
      communications protocol.


      The remote computers must have Windows PowerShell 2.0, the Microsoft .NET
      Framework 2.0, and the WinRM service. Remote commands are supported
      on all operating systems that can run Windows PowerShell. The
      current user must have permission to run commands on the remote
      computers.


      To support remoting, the Invoke-Command, Enter-PSSession, and
      Exit-PSSession cmdlets have been added, along with other cmdlets
      that contain the PSSession noun. These cmdlets let you create and manage
      persistent connections.


      The ComputerName parameter has also been added to several cmdlets,
      including the Get-Process, Get-Service, and Get-Eventlog cmdlets. This
      parameter allows you to get information about remote computers.
      These cmdlets use .NET Framework methods to get their data,
      so they do not rely on Windows PowerShell remoting. They do not require
      any new programs or configuration.

  

Background Jobs
   
      Background jobs are commands that run asynchronously. When you run a
      background job, the command prompt returns immediately, even if the
      command is still running. You can use the background job feature to run a
      complex command in the background so that you can use your session for
      other work while the command runs.


      You can run a background job on a local or remote computer and then save
      the results on the local or remote computer. To run a job remotely, use
      the Invoke-Command cmdlet.
   

      Windows PowerShell includes a set of cmdlets that contain the Job noun
      (the Job cmdlets). Use these cmdlets for creating, starting, managing,
      and deleting background jobs and for getting the results of a background
      job. To get a list of the job cmdlets, type the following command:

          get-command *-job

  Script Debugger
   
      Windows PowerShell 2.0 includes a cmdlet-based debugger for scripts and
      functions. The debugger is supported by a fully documented public API
      that you can use to build your own debugger or to customize or extend
      the debugger.


      The debugger cmdlets let you set breakpoints on lines, columns,
      variables, and commands. These cmdlets let you manage the breakpoints
      and display the call stack. You can create conditional breakpoints and
      specify custom actions at a breakpoint, such as running diagnostic and
      logging scripts.


      When you reach a breakpoint, Windows PowerShell suspends execution
      and starts the debugger. The debugger includes a set of custom commands
      that let you step through the code. You can also run standard Windows
      PowerShell commands to display the values of variables, and you can use
      cmdlets to investigate the results.



  Data Section
   
      Scripts designed for Windows PowerShell 2.0 can have one or more
      DATA sections that isolate the data from the script logic. The data in
      the new DATA section is restricted to a specified subset of the Windows
      PowerShell scripting language.


      In Windows PowerShell 2.0, the DATA section is used to support
      script internationalization. You can use the DATA section to isolate
      and identify user message strings that will be translated into
      multiple user interface languages.


  Script Internationalization
   
      Windows PowerShell 2.0 script internationalization features allow you
      to better serve users throughout the world. Script internationalization
      enables scripts and functions to display messages and Help text to users
      in multiple languages.


      The script internationalization features query the operating system user
      interface culture ($PsUICulture) during execution and then import the
      appropriate translated text strings so you can display them to the user.
      The Data section lets you store text strings separate from code so that
      they are easily identified. A new cmdlet, ConvertFrom-StringData,
      converts text strings into dictionary-like hash tables to facilitate
      translation.


  WMI Cmdlets
   
      The Windows Management Instrumentation (WMI) functionality of
      Windows PowerShell 2.0 is improved with the addition of the following
      cmdlets:

          - Remove-WmiObject
          - Set-WmiInstance
          - Invoke-WmiMethod


      New parameters have been added to the Get-WmiObject cmdlet. All the WMI
      cmdlets now support the following parameters:

          - EnableAllPrivileges
          - Impersonation
          - Authentication
          - Authority


      These new parameters give you more refined control over the security
      configuration of your WMI operations without requiring you to work
      directly with the types in the .NET Framework Class Library.


      For a list of WMI cmdlets, type the following command:

          get-help *wmi*

      To get help for each cmdlet, type get-help followed by the cmdlet name.

   
  The Get-WinEvent Cmdlet
   
      The Get-WinEvent cmdlet gets events from Event Viewer logs and from
      Event Tracing for Windows (ETW) event log files on local and remote
      computers. It can get events from classic event logs and from the
      Windows Event Logs that were introduced in Windows Vista.


      You can use Get-WinEvent to get the objects that represent event logs, event
      log providers, and the events in the logs. Get-WinEvent lets you combine
      events from different sources in a single command. It supports
      advanced queries in XML Path Language (XPath), XML, and hash table
      format.


      Get-WinEvent requires Windows Vista or Windows Server 2008 and the
      Microsoft .NET Framework 3.5.


  The Out-Gridview Cmdlet
   
      The Out-GridView cmdlet displays the results of other commands in an
      interactive table in which you can search, sort, group, and filter the
      results. For example, you can send the results of a Get-Process,
      Get-WmiObject, Get-WinEvent, or Get-Eventlog command to Out-GridView and
      then use the table features to examine the data.

        help out-gridview -full


  The Add-Type Cmdlet
   
      The Add-Type cmdlet lets you add .NET Framework types to
      Windows PowerShell from the source code of another .NET Framework
      language.


      Add-Type compiles the source code that creates the types and generates
      assemblies that contain the new .NET Framework types. Then, you can use
      the .NET Framework types in Windows PowerShell commands along with the
      standard object types provided by the .NET Framework.


      You can also use Add-Type to load assemblies into your session so that
      you can use the types in the assemblies in Windows PowerShell.


      Add-Type allows you develop new .NET Framework types, to
      use .NET Framework types in C# libraries, and to access Win32 APIs.

   

  Event Notification
   
      Windows PowerShell 2.0 introduces event notification. Users can register
      and subscribe to events, such as Windows PowerShell events, WMI events,
      or .NET Framework events. And, users can listen, forward, and act on
      management and system events both synchronously and asynchronously.


      Developers can write applications that use the event architecture
      to receive notification about state changes. Users can write
      scripts that subscribe to various events and that react to the content.
  

      Windows PowerShell provides cmdlets that create new events, get
      events and event subscriptions, register and unregister events,
      wait for events, and delete events. For more information about these
      cmdlets, type the following command:

          get-command *-event

  
  Modules
   
      Windows PowerShell modules let you divide and organize your
      Windows PowerShell scripts into independent, self-contained,
      reusable units. Code from a module executes in its own context,
      so it does not add to, conflict with, or overwrite the variables,
      functions, aliases, and other resources in the session.

   
      You can write, distribute, combine, share, and reuse modules to build
      simple scripts and complex applications.


      Windows PowerShell 2.0 includes cmdlets to add, get, and remove modules
      and to export module members. For more information about the cmdlets
      that are related to modules, type the following command:

          get-command *-module*


  Transactions
   
      Windows PowerShell 2.0 includes support for transactions. Transactions
      let you undo an entire series of operations. Transactions are available
      only for operations that support transactions. They are designed for
      applications that require atomicity, consistency, isolation, and
      recoverability, like databases and message queuing.


      Cmdlets and providers that support transactions have a new
      UseTransaction parameter. To start an operation within a transaction,
      use the Start-Transaction cmdlet. Then, when you use the cmdlets that
      perform the operation, use the UseTransaction parameter of each cmdlet
      when you want the command to be part of a transaction.


      If any command in the transaction fails at any point, use the
      Rollback-Transaction cmdlet to undo all the commands in the transaction.
      If all the commands succeed, use the Commit-Transaction cmdlet to make
      the command actions permanent.


      Windows PowerShell 2.0 includes cmdlets to start, use, commit, and roll
      back transactions. For information about these cmdlets, type the
      following command:

          get-command *transaction*


Accessing Windows PowerShell for SharePoint 2010 Products

After installing SharePoint 2010 Products, the applicable Windows PowerShell cmdlets are available by using the SharePoint 2010 Management Shell, or by using the Windows PowerShell console. With the management shell, you can manage every aspect of SharePoint 2010 Products. You can create new site collections, Web applications, user accounts, service applications, proxies, and more. The commands from the management shell output SharePoint objects based on the Microsoft .NET Platform. These objects can be applied as input to subsequent commands or stored in local variables for later use.

With the management shell, you do not have to register the snap-in that contains the cmdlets. Registration of the Microsoft.SharePoint.PowerShell.dll module for SharePoint 2010 cmdlets is automatic, as a result of the line Add-PSSnapin Microsoft.SharePoint.PowerShell in the SharePoint.ps1 file located in %CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\Config\PowerShell\Registration. If you choose to use the Windows PowerShell console, you must register this snap-in manually.

Whether you are using the management shell or the Windows PowerShell console, you can also load additional snap-ins. For more information, see Customizing Profiles (http://go.microsoft.com/fwlink/?LinkId=183166).

To access the SharePoint 2010 Management Shell

   1.On the Start menu, click All Programs.
   2.Click Microsoft SharePoint 2010 Products.
   3.Click SharePoint 2010 Management Shell.


The SharePoint 2010 Management Shell and the Windows PowerShell console also differ in the use of the ReuseThread option, which defines how the threading model is used. The management shell's use is defined by this line, {Host.Runspace.ThreadOptions = "ReuseThread"}, which is in the SharePoint.ps1 file. For more information, see PS Thread Options (http://go.microsoft.com/fwlink/?LinkId=183145).

Permissions

Before you can use the management shell and the Windows PowerShell cmdlets, verify that you meet the following minimum requirements:

If you do not have membership in the SharePoint_Shell_Access role or WSS_Admin_WPG local group, use the Add-SPShellAdmin cmdlet. When the Add-SPShellAdmin cmdlet is used, the user is added to the WSS_Admin_WPG group in all front-end Web servers and is added to the SharePoint_Shell_Access role. If the target database does not have a SharePoint_Shell_Access role, the role is automatically created. Once the Add-SPShellAdmin cmdlet has been run, the user can run SharePoint 2010 Windows PowerShell cmdlets in a multiple-server farm environment.

When you run the Add-SPShellAdmin cmdlet to add a user to the SharePoint_Shell_Access role, you must have the following security permissions:

    * Securityadmin server role access on the SQL instance and the db_owner role in a database.

    * Administrative permission on the local computer.


Typically, the person that will use the Add-SPShellAdmin cmdlet must be associated with the user account that was used for Setup.

You must run the Add-SPShellAdmin cmdlet for all databases to which you want to grant access. If no database is specified, the farm configuration database is used. If you do specify a database, the farm content database will be included in addition to the farm configuration database you specify.

To see a list of all of the *SPShellAdmin cmdlets, from a Windows PowerShell command prompt, type Get-Command -Noun SPShellAdmin.

Scripts and execution policies

Although you can use Windows PowerShell to perform a single administrative task, the real benefit of using Windows PowerShell cmdlets is the ability to use a script to automate a series of tasks. A script is a plain text file that contains one or more Windows PowerShell commands. Windows PowerShell scripts have a .ps1 file name extension.

When using a script, be aware that the minimum required execution policy for SharePoint 2010 Products is RemoteSigned, while the default policy for Windows PowerShell is Restricted. If the policy is left as Restricted, the SharePoint 2010 Management Shell will change the policy for Windows PowerShell to RemoteSigned. This means the SharePoint 2010 Management Shell must be launched with elevated administrative permission by selecting Run as administrator. This change will apply to all Windows PowerShell sessions.


Web Site vs Web Application project in .Net

Website project type was introduced in the VS 2005 and Web Application model introduced in VS 2003. Website project type is based on folder structure and does not require a project file. Later in VS 2005 sp1 project file was introduced for the VS 2005 website project type. Now in VS 2008 we can create both project types.A common question by asp.net developers is what project model should I use for asp.net application?Every project model has it's own advantages and disadvantages.

Major difference between Web Application and Website is when you deploy the WebApps, you can just copy the Compiled Assembly and Visual elements to the IIS folder where as in Website you need to copy everything to the IIS to work.

Website:
It is a collection of web pages and other information such as images, sound, and video files, etc. that are made available to end users using different browsers like IE, FireFox, Chrome etc. through a single web server.


Web Application:
A web application serves a specific function such a shopping cart or a blogging application.

A web site can be a collection of several web applications. 

Web Application project model
  1. Provides the same Web project semantics as Visual Studio .NET 2003 Web projects.
  2. Has a project file (structure based on project files).
  3. Build model - all code in the project is compiled into a single assembly.
  4. Supports both IIS and the built-in ASP.NET Development Server.
  5. Supports all the features of Visual Studio 2005 (refactoring, generics, etc.) and of ASP.NET 2.0 (master pages, membership and login, site navigation, themes, etc).
  6. Using FrontPage Server Extensions (FPSE) are no longer a requirement.
Web Site project model
  1. No project file (Based on file system).
  2. New compilation model.  (Read here or here for more details) and ...
  3. Dynamic compilation and working on pages without building entire site on each page view.
  4. Supports both IIS and the built-in ASP.NET Development Server.
  5. Each page has it's own assembly.
  6. Different code model.  (Read here for more details)
Which model should I use?
  1. You need to migrate large Visual Studio .NET 2003 applications to VS 2005? use the Web Application project.
  2. You want to open and edit any directory as a Web project without creating a project file? use Web Site project.
  3. You need to add pre-build and post-build steps during compilation? use Web Application project.
  4. You need to build a Web application using multiple Web projects? use Web Application project.
  5. You want to generate one assembly for each page? use Web Site project
  6. You prefer dynamic compilation and working on pages without building entire site on each page view? use Web Site project
  7. You prefer single-page code model to code-behind model? use Web Site project.

Sharepoint server 2010 vs Sharepoint Foundation 2010

Here is an image showing differences between sharepoint  2010 versions.You can find all these in the below link
http://sharepoint.microsoft.com/en-us/buy/Pages/Editions-Comparison.aspx


How to Deploy a SharePoint site from one server to another?

Some times we need to deploy our share point Site on Staging server for client access to test the website.


Follow the below steps to  migrate Sharepoint site from Dev Server to Staging.

Method I


1. Use SQL Management Studio to backup the Content Database
How to: Back Up a Database (SQL Server Management Studio):
http://msdn.microsoft.com/en-us/library/ms187510.aspx

2. Restore the Content Database to your new SQL server
How to: Restore a Database Backup (SQL Server Management Studio):
http://msdn.microsoft.com/en-us/library/ms177429.aspx

3. Create a new web application in Central Admin on the new farm:
Create a Web application (SharePoint Foundation 2010):
http://technet.microsoft.com/en-us/library/cc288705.aspx
 
4. Delete the content Database that get's created by the new web application(from step 4)
Central Admin > Application Management > Content Databases > delete the empty db from your web application (created in step 3)

5. Use SharePoint Management Shell(Mount-SPContentDatabase), Stsadm(-o addcontentdb) or Central Admin to add the contentdatabase:
Add a content database (SharePoint Server 2010):
http://technet.microsoft.com/en-us/library/cc825314.aspx
 
6. Make sure that your account has administrative permissions on the site collection(update site collection administrator)
 
Add or remove site collection administrators (SharePoint Server 2010):
 
 Method II

We can use Central Adminstration to take back up of a farm or site collection to a shared location and we can restore the backup file into a new server farm using the new server farm's Central Administration as shown in the below images.

Sitefinity 3.7 - Custom Url Rewrite Module

In our application we need to redirect the user from a old website page to the new website page. Sitefinity has built in Module to rewrite the url. The below link clearly explains it.

http://www.sitefinitywatch.com/blog/09-01-08/URL_Rewriting_with_Sitefinity_s_AdvancedUrlRewriter.aspx

In our application we have plenty of  URLS to rewrite and we dont want to put all these stuff in web.config. So We have created a Custom Module to rewrite the URLs. This module reads the xml file (which contains a set of url rewrites and the format is same as url rewrites defined in the web.config.) and rewrites the URL.

In our application we have two xml files, one is for pdf documents and the other is for page re-directions.

Code :

App_Code/RedirectModule.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;


namespace Ramp.UrlRedirect
{

/// <summary>
/// Summary description for RedirectModule
/// </summary>
public class RedirectModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(OnBeginRequest);
}

private void OnBeginRequest(object src, EventArgs e)
{
HttpApplication app = src as HttpApplication;
string requestedUrl = app.Request.Url.AbsolutePath.ToLower();
if (requestedUrl == "/index.html")
{
try
{
app.Response.StatusCode = 301; // make a permanent redirect
app.Response.AddHeader("Location", "/");
app.Response.End();
return;
}
catch { }
}

if (!requestedUrl.Contains(ConfigurationManager.AppSettings["SearchResultsPage"].ToLower()))
{

DataSet dataset = new DataSet();

Regex pdfRegx = new Regex("(.pdf)$", RegexOptions.IgnoreCase);

if (pdfRegx.IsMatch(requestedUrl))
{
dataset.ReadXml(app.Server.MapPath("~/Res/Redirections/doc_redirections.xml"));
}
else
{
dataset.ReadXml(app.Server.MapPath("~/Res/Redirections/page_redirections.xml"));
}


for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
try
{
string sourceUrl = dataset.Tables[0].Rows[i].ItemArray[0].ToString();
string targetUrl = dataset.Tables[0].Rows[i].ItemArray[1].ToString();

Regex targetRegex = new Regex(targetUrl, RegexOptions.IgnoreCase);

if (targetRegex.IsMatch(requestedUrl))
{
return;
}

Regex sourceRegex = new Regex(sourceUrl, RegexOptions.IgnoreCase);
if (sourceRegex.IsMatch(requestedUrl))
{
string destinationUrl = sourceRegex.Replace(requestedUrl, targetUrl, 1);
app.Response.StatusCode = 301; // make a permanent redirect
app.Response.AddHeader("Location", destinationUrl + app.Request.Url.Query);
app.Response.End();
break;
}
}
catch { }
}
}

}

public void Dispose()
{
}
}
}

web.config changes
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="ScriptModule" />
<remove name="Cms" />
<!--<remove name="LibraryModule"/>-->
<remove name="RadUploadModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="Cms" type="Telerik.Cms.Web.CmsHttpModuleUrlRewrite, Telerik.Cms" />
<add name="RedirectModule" type="Ramp.UrlRedirect.RedirectModule, App_Code" />

<!-- Http Module for extension mappings in Library module.-->
<!--<add name="LibraryModule" type="Telerik.Libraries.LibraryHttpModule, Telerik.Libraries"/>-->
<add name="RadUploadModule" preCondition="managedHandler" type="Telerik.Web.UI.RadUploadHttpModule, Telerik.Web.UI" />
</modules>
...............................
.................................

Xml file URL rewrite element format

<?xml version="1.0" standalone="yes"?>
<urlrewrites>
<rule>
<url>subscribe.html</url>
<rewrite>subscribe.aspx</rewrite>
</rule>
<rule>

    . . . . . . .

</rule>
.. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
</urlrewrites>



 

Wednesday, March 23, 2011

Sitefinity - Custom Pollbox

I have created a custom PollBox  for Sitefinity to display Poll Questions and Results.

Why Custom PollBox?

Sitefinity  had its Rad Control to display poll questions and poll results.But the default RadChart control to display the results, uses images  to display graphical results.This is fine and working properly when we host our application on a single Web server. If we use Server farm with Load balance, We will get an error "RadChart image not found" and the results box will not display properly.

Even if we set the property 'Use Session" to false, the same error remains on the Web Farm.

Then I came up with a Custom poll box, which displays the poll results in pure html.It avoids the images and the error vanished.

Custom Pollbox Advantages:

1.)As it is showing all poll results in pure html, we have full control on Bar colors and width and heights.We can manipulate these with css.
2.) On the admin Side u can add this user control  by drag and drop as we add the default poll box.
3.) We can select the poll Id as we select it for default PollBox.
4.) We can specify User Blocking time in Hours .If we set this time,  a cookie will be created on the user machine and will block the user to vote again for a specified time in Hours.
5.) We can give our own Bar Colors and Main Colors and Horizontal Width of the Graph.
6.) We can customize this Custom Pollbox according to our requirements.

Screen shots:



Code:

you can download the code files  from the below link

https://docs.google.com/leaf?id=0B0LGUQrVMkGoNzc4ZGFhNzUtNjE4Yy00MDYyLThiYmQtM2E2MjU0YjA4MTc2&hl=en

CustomPollBox.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomPollBox.ascx.cs" Inherits="UC_CustomPollBox" %>
<div class="pollBox" >
<asp:Repeater runat="server" ID="rptPoll" OnItemDataBound="rptPoll_ItemDataBound" >
<ItemTemplate>
<div class="pollQuestion" >
<asp:Literal ID="litQuestion" runat="server" ></asp:Literal>
</div>
<asp:RadioButtonList ID="rblAnswers" runat="server" ></asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>

<asp:Repeater runat="server" ID="rptResults" OnItemDataBound="rptResults_ItemDataBound" Visible="false" >
<ItemTemplate>
<div class="pollQuestion" >
<asp:Literal ID="litQuestion" runat="server" ></asp:Literal>
</div>
<asp:Panel ID="pnlGraph" runat="server" ></asp:Panel>
</ItemTemplate>
</asp:Repeater>
<div class="pollBtnContainer" >
<asp:Literal ID="litTotalVotes" runat="server" Visible="false"></asp:Literal>
<asp:PlaceHolder ID="plhVoteButton" runat="server">
<a class="buttonLeftNew btn_BlueLeftNew" onmouseover="javascript:AddUndeline(this.id);" onmouseout="javascript:RemoveUndeline(this.id);" id="btnVoteLink" >
<asp:Button ID="btnVote" runat="server" Text="Submit" CssClass="btnMiddleNew btn_BlueMiddleNew buttonInput" OnClick="btnVote_Clicked" CausesValidation="true" onmouseover="javascript:AddUndeline(this.id);" onmouseout="javascript:RemoveUndeline(this.id);" />
</a>
</asp:PlaceHolder>
</div>
</div>

CustomPollBox.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Telerik.Cms.Web.UI;
using Telerik.Polls;
using System.Collections;
using System.Drawing;

public partial class UC_CustomPollBox : System.Web.UI.UserControl
{
[Description("Gets or sets the ID of the poll which will be displayed")]
[Bindable(true)]
[WebEditor("Telerik.Polls.WebControls.PollBoxEditor, Telerik.Polls")]
[DisplayName("Poll ID")]
public Guid PollID { get; set; }

[PersistenceMode(PersistenceMode.Attribute)]
[Description("Chart background main color")]
[NotifyParentProperty(true)]
[TypeConverter(typeof(ColorConverter))]
[DefaultValue(typeof(Color), "240, 248, 255")]
[DisplayName("Main Color")]
public Color MainColor { get; set; }

[NotifyParentProperty(true)]
[DefaultValue(150f)]
public float Width { get; set; }

[Description("Chart plot area main color")]
[NotifyParentProperty(true)]
[TypeConverter(typeof(ColorConverter))]
[PersistenceMode(PersistenceMode.Attribute)]
[DefaultValue(typeof(Color), "153, 209, 248")]
[DisplayName("Bar Color")]
public Color BarColor { get; set; }

[Description("Block user to resubmit the poll (in hours)")]
[DisplayName("Block Time")]
public int BlockTime { get; set; }

PollManager pollManager;


protected void Page_Load(object sender, EventArgs e)
{
pollManager = new PollManager();

if (BarColor.A == 0 && BarColor.B == 0 && BarColor.G == 0 && BarColor.R == 0)
BarColor = Color.FromArgb(153, 209, 248);

if (MainColor.A == 0 && MainColor.B == 0 && MainColor.G == 0 && MainColor.R == 0)
MainColor = Color.FromArgb(240, 248, 255);
if (Width == 0)
Width = 150;

if (BlockTime == 0)
BlockTime = 120;

if(!IsPostBack)
{
if (PollID != null)
{
double total = pollManager.GetPollById(PollID).VoterNumber;
if (Request.Cookies[PollID.ToString()] != null && total > 0)
{
DisplayPollResults();
}
else
BindQuestionsRepeater();
}
}
}

protected void rptPoll_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
PollQuestion question = e.Item.DataItem as PollQuestion;
List<PollAnswer> answersList = PollHelper.GetAnswersByQuestionID(question.ID);
RadioButtonList rblAnswers = e.Item.FindControl("rblAnswers") as RadioButtonList;

Literal litQuestion = e.Item.FindControl("litQuestion") as Literal;
litQuestion.Text = question.Text;

rblAnswers.DataSource = answersList;
rblAnswers.DataTextField = "Text";
rblAnswers.DataValueField = "ID";
rblAnswers.DataBind();

}
}

protected void btnVote_Clicked(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptPoll.Items)
{
RadioButtonList rblAnswers = item.FindControl("rblAnswers") as RadioButtonList;
if (!string.IsNullOrEmpty(rblAnswers.SelectedValue))
{
pollManager.IncrementAnswerCount(new Guid(rblAnswers.SelectedValue), 1);
}
}

IPollItem poll = pollManager.GetPollById(PollID);
if (poll != null)
{
poll.VoterNumber++;
pollManager.UpdatePoll(poll);
}
DisplayPollResults();
SetCookie();
Response.Redirect(Request.RawUrl);
}

private void BindQuestionsRepeater()
{
rptPoll.DataSource = PollHelper.GetQuestionsByPollID(PollID);
rptPoll.DataBind();
}

private void DisplayPollResults()
{
rptPoll.Visible = false;
plhVoteButton.Visible = false;
rptResults.Visible = true;

rptResults.DataSource = PollHelper.GetQuestionsByPollID(PollID);
rptResults.DataBind();
}

protected void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
PollQuestion question = e.Item.DataItem as PollQuestion;
List<PollAnswer> answersList = PollHelper.GetAnswersByQuestionID(question.ID);

Literal litQuestion = e.Item.FindControl("litQuestion") as Literal;
litQuestion.Text = question.Text;

Panel chartPanel = e.Item.FindControl("pnlGraph") as Panel;
chartPanel.Controls.Add(LoadGraph(question));
}


private Table LoadGraph(PollQuestion questionItem)
{
double total = pollManager.GetPollById(PollID).VoterNumber;
litTotalVotes.Text = string.Format("Total Votes: {0}", total);
litTotalVotes.Visible = true;
IList<PollAnswer> answersList = PollHelper.GetAnswersByQuestionID(questionItem.ID);

Table table = new Table();
table.CssClass = "pollBoxTable";

double iWidth = 0;
Unit uWidth = 0;
string sWidth = (int)Width + "px";
table.Width = Unit.Pixel((int)Width);

if (sWidth.IndexOf("%") > -1)
{
iWidth = Convert.ToInt32(sWidth.Substring(0, sWidth.IndexOf("%")));
iWidth = iWidth / 10;
}
else
{
if (sWidth.IndexOf("px") > -1)
{
iWidth = Convert.ToInt32(sWidth.Substring(0, sWidth.IndexOf("px")));
}
else
{
iWidth = Convert.ToInt32(sWidth);
}
iWidth = iWidth / 100;
}

for (int i = 0; i < answersList.Count; i++)
{
TableRow tr = new TableRow();

TableCell td1 = new TableCell();
tr.Cells.Add(td1);


Panel container = new Panel();
container.BackColor = MainColor;
container.BorderColor = Color.White;
container.BorderWidth = Unit.Pixel(1);

Label lblBar = new Label();
double dblPercentage = (answersList[i].VoteCount/total) * 100;
lblBar.Width = Unit.Pixel(Convert.ToInt32(dblPercentage * iWidth));
lblBar.Height = Unit.Pixel(20);
lblBar.BackColor = BarColor;
lblBar.Text = answersList[i].VoteCount.ToString();
lblBar.CssClass = "chartBar";

Label lblPercentage = new Label();
lblPercentage.Text = answersList[i].Text + "&nbsp;/&nbsp;" + Math.Round(dblPercentage, 2).ToString() + "%<br/>";
td1.Controls.Add(lblPercentage);


td1.Controls.Add(container);
container.Controls.Add(lblBar);


table.Rows.Add(tr);
}

return table;
}

/// <summary>
/// Returns the width set as pixels or percentage
/// </summary>
/// <param name="width"></param>
/// <returns></returns>
private Unit GetWidth(string width)
{
Unit retVal = new Unit(0);
try
{
int iWidth = 0;
if (width.IndexOf("%") > -1)
{
iWidth = Convert.ToInt32(width.Substring(0, width.IndexOf("%")));
retVal = Unit.Percentage(iWidth);
}
else
{
if (width.IndexOf("px") > -1)
{
iWidth = Convert.ToInt32(width.Substring(0, width.IndexOf("px")));
}
else
{
iWidth = Convert.ToInt32(width);
}
retVal = Unit.Pixel(iWidth);
}
return retVal;
}
catch { return retVal; }
}

/// <summary>
/// Sets a cookie. Prevents multiple entries from a computer
/// </summary>
private void SetCookie()
{
HttpCookie cookie = new HttpCookie(PollID.ToString(), DateTime.Now.ToString());
cookie.Expires = DateTime.Now.AddHours(BlockTime);
Response.Cookies.Set(cookie);
}


}

App_Code/PollHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for PollHelper
/// </summary>
public class PollHelper
{
//public static PollHelper PollHelper = new PollHelper();
    public PollHelper()
    {
        
    }

public static List<PollQuestion> GetQuestionsByPollID(Guid pollID)
{
List<PollQuestion> pollQuestions = new List<PollQuestion>();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sitefinity"].ConnectionString);
using (connection)
{
try
{
connection.Open();
SqlCommand commad = new SqlCommand(string.Format("select * from dbo.sf_Polls_QuestionData where PollID = @PollID"), connection);
SqlParameter parameter = new SqlParameter("PollID", System.Data.SqlDbType.UniqueIdentifier);
parameter.Value = pollID;
commad.Parameters.Add(parameter);
SqlDataReader reader = commad.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

while (reader.Read())
{
pollQuestions.Add(new PollQuestion((Guid)reader["ID"], (string)reader["Text"], (Guid)reader["PollID"]));
}
}
finally
{
connection.Close();
}
}
return pollQuestions;
}

public static List<PollAnswer> GetAnswersByQuestionID(Guid questionID)
{
List<PollAnswer> pollAnswers = new List<PollAnswer>();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sitefinity"].ConnectionString);
using (connection)
{
try
{
connection.Open();
SqlCommand commad = new SqlCommand(string.Format("select * from dbo.sf_Polls_AnswerData where QuestionID = @QuestionID"), connection);
SqlParameter parameter = new SqlParameter("QuestionID", System.Data.SqlDbType.UniqueIdentifier);
parameter.Value = questionID;
commad.Parameters.Add(parameter);
SqlDataReader reader = commad.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

while (reader.Read())
{
pollAnswers.Add(new PollAnswer((Guid)reader["ID"], (string)reader["Text"], (Guid)reader["QuestionID"], (int)reader["VoteCount"]));
}
}
finally
{
connection.Close();
}
}

return pollAnswers;
}

}

public class PollQuestion
{
private Guid iD;
private string text;
private Guid pollID;

public Guid ID
{
get
{
return iD;
}
set
{
iD=value;
}
}

public Guid PollID
{
get
{
return pollID;
}
set
{
pollID=value;
}
}

public string Text
{
get
{
return text;
}
set
{
text=value;
}
}

public PollQuestion()
{
}

public PollQuestion(Guid ID,string Text,Guid PollID)
{
this.iD = ID;
this.pollID = PollID;
this.text = Text;
}
}


public class PollAnswer
{
private Guid iD;
private string text;
private Guid questionID;
private int voteCount;

public Guid ID
{
get
{
return iD;
}
set
{
iD = value;
}
}

public Guid QuestionID
{
get
{
return questionID;
}
set
{
questionID = value;
}
}

public string Text
{
get
{
return text;
}
set
{
text = value;
}
}

public int VoteCount
{
get
{
return voteCount;
}
set
{
voteCount = value;
}
}

public PollAnswer()
{
}

public PollAnswer(Guid ID, string Text, Guid QuestionID,int VoteCount)
{
this.iD = ID;
this.questionID = QuestionID;
this.text = Text;
this.voteCount = VoteCount;
}
}