ASP.Net User Control as Web Parts

In this article I would like to give answers to some of the terms and also given some steps to deploy the web part.

Q: What is Windows Sharepoint (WSS)?
Ans: Windows sharepoint is new technology, which is available in the form of service on Windows 2003 server. It uses CAML (Collaboration application markup language) Wss is more content management based with document libraries and lists.

Q: What is Share Point Portal?
Ans: Windows share point portal offering features like global navigation and searching.

Q: What is document library?
Ans: A document library is where you upload your documents, they consists of row and columns with links to the documents

Q: What is meeting workspace?
Ans: Documents workspace consists of information surrounding a single or multiple documents.

Q: What is a web part?
Ans: Web parts are nothing but the integrated controls which perform some specific task. In short web part is nothing but xml queries to full sharepoint list or document.

Q: What is web part zone?
Ans: Web part zone consist of zonetemplate, and it is nothing but a container in which we can drag and drop user control.

Q: What is DWP?
Ans: DWP is nothing but name of web part file extension

Q: What are various kinds of roles user can have?
Ans: 1. Reader: Has read only access to the web parts
        2. Contributer: Can add content to existing document libraries and lists.
        3. Web Designer: Can add content to the existing document libraries and lists
        4. Administrator: Has full control of the web site

Q: What are the steps for deploying web part on virtual server gallery?
Ans: These steps are more specific to the windows share point server 2003 & web part developed in ASP.net 2003

  • Open the web part project in visual studio
  • Choose file -> Add Project -> New Project -> Setup and deployment project -> Add -> Project Output
  • Select the setup project in the solution explorer and choose project ->Add - > Project output.
  • Select Primary output and content files from the web part project and choose  OK, visual studio adds those items to the setup project.
  • Choose Build -> Rebuild Solution (By doing this visual studio rebuilds the web part assembly and package the assembly and content files in the CAB file)
  • Copy the resulting CAB file to the webparts folder
  • Run stsadm.exe to install the CAB file on the server for e.g.

You can locate stsadm.exe file at following location:

C:\program files\common files\Microsoft shared\web server extension\60\bin\

You can run following command using command prompt

Stsadm - o addwppack - filename "c:\inetpub\wwwroot\Calendar.CAB" This will install Calendar web part to the "Bin" directory of the virtual server. And this will be available to drag and drop in share point server 2003 under Virtual Gallery section

For removing a web part from the list we have to use following command on the command prompt.

Stsadm-o deletewppack -name "Calendar.CAB"

Q. Explain about share point security?
Ans: When web part is deployed under Bin directory .net uses CAS to check if a peace of code has permission to run on the machine. Normally we will face permission issue which can be handled by different ways:

Changing trust level define by wss under c:\program files\common files\Microsoft shared\web sharepoint\60\config\ in wss_minimumaltrust.config, wss_mediumtrust.config we can set <trustlevel name="wss_medium" >

We can deploy assemblies under GAC

Create custom policy file that will grant full trust to your assemblies

Here is simple web part, which displays all the author lists. For this I am having user control called as Display.ascx which fetch author records from the database "Tbl_Authors" which is as:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Display.ascx.cs" Inherits="Display" %>

<asp:DataGrid ID="dgProducts" runat="server" AutoGenerateColumns=true></asp:DataGrid>

Display.ascx.cs Code behind file is as:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Display : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack))
        {
            BindData();
        }
    }

    public void BindData()
    {
        SqlConnection sqlCon = new SqlConnection("server=Atlas; UID=sa; Password=; Database=master");
        try
        {
            if (sqlCon.State == ConnectionState.Closed)
            {
                sqlCon.Open();
                SqlCommand sqlComd = new SqlCommand("SELECT * FROM Tbl_Author", sqlCon);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlComd);
                DataSet dtSt = new DataSet();
                sqlDa.Fill(dtSt);
                dgProducts.DataSource = dtSt;
                dgProducts.DataBind();
            }
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            sqlCon.Close();
        }
    }
}

I have Default.aspx file as below in which I have registered my user control.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagName="Display" TagPrefix="Author" Src="~/Display.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <
html xmlns="http://www.w3.org/1999/xhtml" >
   <
head runat="server">
     <
title>Untitled Page</title>
   </
head>
   <
body>
       <
form id="form1" runat="server">
          <
asp:WebPartManager ID="dispManage" runat="server" />
          <
asp:WebPartZone ID="dispZone" runat="server">
           <ZoneTemplate>
             <
Author:Display id="wbCntrl" runat="server" />
           </
ZoneTemplate>
       </
asp:WebPartZone>
   </form>
  </
body>
</
html