dotnet thoughts 

a dotnet developer's technical blog

Creating a WebPart using Visual Web Developer Express

Recently I installed the WSS 3.0 in my machine. In this post I am talking about how to develop a web part using Visual Web Developer express, which can be deployed in WSS 3.0. From ASP.Net 2.0 onwards WSS Sharepoint class is inheriting from WebPart class in ASP.Net, but the Sharepoint WebPart class contains more features to create connected WebParts etc.

  1. Create a Class Library project in VWD Express; I am using VWD 2010, and after creating the project make sure you choose the Target framework as .Net Framework 2.0. Otherwise you may get some unable to load target assembly error from WSS.
  2. Add Reference of System.Web.
  3. Create a Class and inherit that from WebPart class, which is available in System.Web.UI.WebControls.WebParts namespace.
  4. Override the CreateChildControls() method, which helps to add /setup ASP.Net controls to the webpart, also you need to override the RenderControl() method, which helps to render the control to the Page.
  5. public class SampleWebPart : WebPart
    {
        private TextBox t;
        private Calendar c;
        private DateTime _userDOB = DateTime.Now;
        protected override void CreateChildControls()
        {
            this.t = new TextBox();
            t.Text = this._userDOB.ToString();
            this.c = new Calendar();
            c.SelectionChanged += new System.EventHandler(c_SelectionChanged);
            this.Controls.Add(t);
            this.Controls.Add(c);
        }
    
        void c_SelectionChanged(object sender, System.EventArgs e)
        {
            this.t.Text = this.c.SelectedDate.ToString();
        }
    
        public override void RenderControl(HtmlTextWriter writer)
        {
            RenderChildren(writer);
        }
    
        [WebBrowsable(true),
        WebDescription("Date of Birth of the User"),
        WebDisplayName("DOB"),
        Personalizable(PersonalizationScope.User)]
        public DateTime UserDOB
        {
            get
            {
                return _userDOB;
            }
            set
            {
                _userDOB = value;
            }
        }
    }
    
  6. Build the assembly; put the DLL to the bin of your Website, most probably it will be located in C:\inetpub\wwwroot\wss\VirtualDirectories\{port}\
  7. Also you need to make the assembly into the SafeControl list of the web.config file of the website, which will be in C:\inetpub\wwwroot\wss\VirtualDirectories\{port}\ this location.
  8. Add Assembly to Safe Control list

    Add Assembly to Safe Control list

  9. Now you need to import the WebPart in WSS, you need to Select the Site Actions > Site Settings > Web Parts page. This page will display all the webparts available in the Website. You need to click on the New Link. And from the New Web Parts page, you need to select the new web part you added to the Bin folder.
  10. Add New Web Part

    Add New Web Part

  11. The selected webpart will be added to the Web Part Gallery.
  12. New WebPart in WebPart Gallery

    New WebPart in WebPart Gallery

  13. Now go to any page, select the Edit Page option from Site Actions. Click on the Add Web Part button, and you can see the newly added web part in the Add WebPart dialog.
  14. New WebPart in Add Web Part Dialog

    New WebPart in Add Web Part Dialog



And this is the web part running on my home page.

Web Part in my Home Page

Web Part in my Home Page




The attributed defined in the “UserDOB” property helps to customize the web part.

WebMatrix Beta 2

WebMatrix is package of tools you need to build Web sites using Windows. It includes IIS Developer Express (a development Web server), ASP.NET (a Web framework), and SQL Server Compact (an embedded database). It streamlines Web site development and makes it easy to start Web sites from popular open-source apps.

Web Matrix Logo

Web Matrix Logo

You can get more details about WebMatrix from Microsoft’s WebMatrix Site

Unrecognized attribute ‘targetFramework’ error

Yesterday while publishing a WCF Service to IIS I got this error from the VS2010 auto generated web.config file.

<compilation debug="true" targetFramework="4.0">

After searching I found it is because of the Framework version setting in the Application Pool of the web application.

In IIS 7, you can modify the application pool by selecting the Application Pools node from left side bar, and Select the Basic Settings, which will bring the Edit Application Pool dialog, where you can choose the .Net Framework Version. To fix the issue, it should set to version 4.x

Edit Application Pool Dialog

Edit Application Pool Dialog

Creating PDF file using C#

Most of the forums, it’s a common question that how can we create PDF files from C#. Few days back I got a chance to work with iTextSharp library which helps to create PDF files from .Net. You can download the iTextSharp from sourceforge.net. It also supports HTML Parsing too.
Here is the code which converts and HTML File into PDF using iTextSharp libraries.

using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

class Program
{
static void Main(string[] args)
    {
        //Creating the instance of the document object.
        Document doc = new Document();
        //Creating the PDF File
StreamWriter streamWriter = new StreamWriter(@"C:\Sample.pdf");
PdfWriter.GetInstance(doc, streamWriter.BaseStream);
        doc.Open();
IEnumerable<IElement> elements;
        //Reading the HTML Contents
using (StreamReader streamReader = new StreamReader(@"C:\SamplePage.htm"))
        {
            //Parsing HTML Contents.
elements = HTMLWorker.ParseToList(streamReader, new StyleSheet());
        }
foreach (IElement item in elements)
        {
            doc.Add(item);
        }
        //Custom Text which will be appended using Paragraph class.
        string paragraph = "This is custom text which will be appended";
        doc.Add(new Paragraph(paragraph));

        //Closing the document.
        doc.Close();
    }
}

Thanks to Rahul for providing initial inputs on iTextSharp library.

Optional Parameters and Named Parameters in C# 4.0

Last day I got an assignment to explore new features of .Net Framework 4.0. So I thought about writing a few posts with the new features. The first feature I explored was Optional Parameters and Named Parameters, as I am coming from VB background, I missed this feature in C# and yes we can resolve this using Overloading still we missed that feature. In .Net 4.0 Microsoft introduced Optional Parameters and Named Parameters in C#.

Both of these features are almost similar like in VB 6 or in VB.Net, unlike we don’t need an optional keyword.

class Program
{
    static void Main(string[] args)
    {
        //Optional Parameters
        ShowMessage("Hello World");
        //Named parameters
        ShowMessage("HelloWorld", backColor: ConsoleColor.Blue);
        //Normal Function call
        ShowMessage("Hello World", ConsoleColor.Red, ConsoleColor.Blue);

        Console.Read();
    }

    //In .Net 4.0
    static void ShowMessage(string message,
        ConsoleColor foreColor = ConsoleColor.Black,
        ConsoleColor backColor = ConsoleColor.White)
    {
        Console.ForegroundColor = foreColor;
        Console.BackgroundColor = backColor;
        Console.WriteLine(message);
        //Resetting Console colors to default.
        Console.ResetColor();
    }
}
« Newer PostsOlder Posts »