Elczar Adame's Shared Points on SharePoint

SharePoint Web Part Template


 

SharePoint Web Part Template

With Windows SharePoint Services 3.0 Web Part using Microsoft Visual Studio 2005 Extensions, several steps we manually do when using Class Library or Web Control Library are automated – it simplifies the creation, deployment, and testing of Microsoft ASP.NET Web Parts in Windows SharePoint Services.

Web Parts in Microsoft Windows SharePoint Services offer us means to create user interface components that support both customization and personalization. The Windows SharePoint Services 3.0 Web Part infrastructure is designed and built on a layer over the Microsoft ASP.NET 2.0 Web Part infrastructure.

With an assumption that we have already installed and configured the Visual Studio 2005 Extensions for Windows SharePoint Services 3.0, we will create a calendar web part that could be displayed in the home page of our portal with link to our Calendar list.

Create Project

1.       Let us open our Microsoft Visual Studio 2005. In the File menu, point to New, then click Project.

2.       In the New Project dialog box, under Visual C# | SharePoint, let us select Web Part template. We will name our project WebPartTemplate. Then, click OK. Below is the illustration.

Initial Settings

1.       In our Solution Explorer, right-click Web Part project, then click Properties. The Properties window will be displayed.

2.       In the Properties window, as illustrated, we will select Debug. In the Start Action section, we will choose Start Browser with URL, pointing it to the SharePoint site where we will deploy our Web Part – e.g. http://Server Name:Port Number/.

3.       Still in the Properties window, we will select SharePoint Solutions to customize the Web Part solution feature package. The template automatically creates XML files necessary to package our Web Part as a Windows SharePoint Services 3.0 Feature. These are:

a.       Solution – written into the manifest.xml file for the Feature.

b.      Feature – written into the feature.xml for the Feature.

c.       Element – written into the element.xml for the Feature. It contains the information contained in a .webpart file.

Logic Description

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Web.UI.WebControls;

namespace WebPartTemplate
{
    [Guid("f72a2732-1bfc-4a95-8d24-b8c6e1302cae")]
    public class WebPartTemplate : System.Web.UI.WebControls.WebParts.WebPart
    {
        public enum PeriodEnum
        {
            Day,
            Week,
            Month
        };

        private const string DefaultList = "Calendar";
        private const PeriodEnum DefaultPeriod = PeriodEnum.Day;
        private const bool DefaultViewToday = true;
      
        private string List = DefaultList;
        private PeriodEnum Period = DefaultPeriod;
        private bool ViewToday = DefaultViewToday;

        Calendar calendar;

        public WebPartTemplate()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        [Personalizable]
        [WebBrowsable]
        [FriendlyName("Calendar List")]
        public string CalendarList
        {
            get
            {
                return List;
            }
            set
            {
                List = value;
            }
        }

        [Personalizable]
        [WebBrowsable]
        [FriendlyName("Calendar Period")]
        public PeriodEnum CalendarPeriod
        {
            get
            {
                return Period;
            }
            set
            {
                Period = value;
            }
        }

        [Personalizable]
        [WebBrowsable]
        [FriendlyName("View Today")]
        public bool CalendarViewToday
        {
            get
            {
                return ViewToday;
            }
            set
            {
                ViewToday = value;
            }
        }

        protected override void CreateChildControls()
        {
            calendar = new Calendar();
            calendar.VisibleDate = System.DateTime.Today;
            calendar.SelectionChanged += new EventHandler(CalendarSelectionChanged);
            Controls.Add(calendar);
        }

        public void CalendarSelectionChanged(object sender, EventArgs e)
        {
            try
            {
                Page.Response.Redirect("~/Lists/" + List + "/calendar.aspx?CalendarPeriod=" + Period + "&CalendarDate=" + calendar.SelectedDate.ToShortDateString());
            }
            catch
            {
                Title = "Incorrect Settings";
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.Write("<table border=0 cellspacing=1 width=100%>");
            output.Write("<tr>");
            output.Write("<td>");
            calendar.RenderControl(output);
            output.Write("</td>");
            output.Write("</tr>");

            if (ViewToday)
            {
                output.Write("<tr>");
                output.Write("<td>");
                output.Write("Today is");
                output.Write("</td>");
                output.Write("</tr>");
                output.Write("<tr>");
                output.Write("<td>");
                output.Write(System.DateTime.Today.ToLongDateString());
                output.Write("</td>");
                output.Write("</tr>");
            }

            output.Write("</table>");
        }

    }
}

Notice the following items:

1.       We have added System.Web.UI.WebControl namespace directives for our calendar control.
2.       We have defined three objects/attributes:
a.       Calendar List – the name of our Calendar list in SharePoint where our calendar web part will be redirected.
b.      Calendar Period – the calendar period to be displayed during the redirection. The values are enumerated through PeriodEnum.
c.       View Today – an option to display the current date below our calendar web part.
3.       We have instantiated a calendar web control. Additionally, we can add reference to System.Drawing to set the skin our control.
4.       We have set an EventHandler, named CalendarSelectionChanged, for the SelectionChanged event of our calendar web control.
5.       Lastly, we have rendered our web part.

Deployment

As we have mentioned earlier, several steps that we manually do when using Class Library or Web Control Library are automated by the extensions. This is noticeable in the deployment process.
1.       Let us press F5. The extensions will automatically perform a number of tasks for us – e.g. assembly building, installing to global assembly cache, adding to SafeControls list, et al.  
2.       In our SharePoint page, click Site Actions, then click Edit Page.
 
For our purpose, our SharePoint page must have a Calendar list named Calendar, where our calendar web part will be redirected. Else, we need to set the Calendar List property of our Web Part later through Modify Shared Web Part by pointing it to a specific Calendar list.
3.       In a specific Web Part zone, click Add a Web Part to the Add Web Parts dialog box.
4.       Select our WebPartTemplate, then Add. Lastly, click Exit Edit Mode to have our final output.

I hope this would help!

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: