Export (0) Print
Expand All

Walkthrough: Creating a Web Part for SharePoint by Using a Designer

Web Parts enable users to directly modify the content, appearance, and behavior of SharePoint site pages by using a browser. This walkthrough shows you how to create a Web Part visually by using the SharePoint Visual Web Part project template in Visual Studio 2010.

The Web Part displays a monthly calendar view and a check box for each calendar list on the site. Users can choose which calendar lists to include in the monthly calendar view by selecting the checkboxes.

This walkthrough illustrates the following tasks:

  • Creating a Web Part by using the Visual Web Part project template.

  • Designing the Web Part by using the Visual Web Developer designer in Visual Studio.

  • Adding code to handle the events of controls on the Web Part.

  • Testing the Web Part in SharePoint.

    NoteNote

    Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Working with Settings.

You need the following components to complete this walkthrough:

First, create a Web Part project by using the Visual Web Part project template.

To create a Visual Web Part Project

  1. Start Visual Studio 2010 by using the Run as Administrator option.

  2. On the File menu, point to New, and then click Project. If your IDE is set to use Visual Basic development settings, on the File menu, click New Project.

    The New Project dialog box appears.

  3. Open the New Project dialog box, expand the SharePoint node under the language that you want to use, and then select the 2010 node.

  4. In the Visual Studio Installed Templates pane, select Visual Web Part, and then click OK.

    The SharePoint Customization Wizard appears. This wizard enables you to select the site that you will use to debug the project and the trust level of the solution.

  5. Click Finish to accept the default local SharePoint site and default trust level for the solution.

    By default, the Visual Web Developer displays the user control of the project in Source view where you can see the page's HTML elements.

Design the Web Part by dragging controls from the Toolbox to the surface of the user control.

To design the layout of the Web Part

  1. On the Visual Web Developer designer, click the Design tab to switch to Design view.

  2. On the View menu, click Toolbox.

  3. In the Toolbox, from the Standard group, drag a CheckBoxList and a Button to the designer.

  4. In the designer, click Button.

  5. On the View menu, click Properties Window.

  6. In the Properties window, set the Text property of the button to Update.

Add code that enables the user to add calendars to the master calendar view.

To handle events of controls on the Web Part

  1. On the designer, double-click the Update button.

    The user control code file opens in Code Editor and the Button1_Click event handler appears. Later, you will add code to this event handler.

  2. Add the following statements to the top of the user control code file.

    
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    
    
    
  3. Add the following line of code to the VisualWebPart1userControl class. This code declares a monthly calendar view control.

    
    private MonthlyCalendarView MonthlyCalendarView1;
    
    
    
  4. Replace the Page_Load method of the VisualWebPart1UserControl class with the following code. This code performs the following tasks:

    • Adds a monthly calendar view to the user control.

    • Adds a check box for each calendar list on the site.

    • Specifies a template for each type of item that appears in the calendar view.

    
    protected void Page_Load(object sender, EventArgs e)
    {
        MonthlyCalendarView1 = new MonthlyCalendarView();
        this.Controls.Add(MonthlyCalendarView1);
        SPCalendarItemCollection items = new SPCalendarItemCollection();
        SPWeb thisWeb = SPControl.GetContextWeb(Context);
    
        if (CheckBoxList1.Items.Count == 0)
        {
            foreach (SPList listItem in thisWeb.Lists)
            {
                if (listItem.BaseTemplate == SPListTemplateType.Events)
                {
                    CheckBoxList1.Items.Add(new ListItem(listItem.Title));
                }
            }
        }
        MonthlyCalendarView1.ItemTemplateName =
            "CalendarViewMonthItemTemplate";
        MonthlyCalendarView1.ItemAllDayTemplateName =
            "CalendarViewMonthItemAllDayTemplate";
        MonthlyCalendarView1.ItemMultiDayTemplateName =
            "CalendarViewMonthItemMultiDayTemplate";
    }
    
    
    
  5. Replace the Button1_Click method of the VisualWebPart1UserControl class with the following code. This code adds items from each selected calendar to the master calendar view.

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        SPCalendarItemCollection items = new SPCalendarItemCollection();
        SPWeb thisWeb = SPControl.GetContextWeb(Context);
    
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected == true)
            {
                SPList calendarList = thisWeb.Lists[item.Text];
                DateTime dtStart = DateTime.Now.AddDays(-7);
                DateTime dtEnd = dtStart.AddMonths(1).AddDays(7);
                SPQuery query = new SPQuery();
                query.Query = String.Format(
                    "<Query>" +
                    "<Where><And>" +
                    "<Geq><FieldRef Name=\"{0}\" />" +
                    "<Value Type=\"DateTime\">{1}</Value></Geq>" +
                    "<Leq><FieldRef Name=\"{0}\" />" +
                    "<Value Type=\"DateTime\">{2}</Value></Leq>" +
                    "</And></Where><OrderBy><FieldRef Name=\"{0}\" /></OrderBy>" +
                    "</Query>",
                    "Start Time",
                    dtStart.ToShortDateString(),
                    dtEnd.ToShortDateString());
    
                foreach (SPListItem listItem in calendarList.GetItems(query))
                {
                    SPCalendarItem calItem = new SPCalendarItem();
                    calItem.ItemID = listItem["ID"].ToString();
                    calItem.Title = listItem["Title"].ToString();
                    calItem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);
                    calItem.StartDate = (DateTime)listItem["Start Time"];
                    calItem.ItemID = listItem.ID.ToString();
                    calItem.WorkSpaceLink = String.Format(
                        "/Lists/{0}/DispForm.aspx", calendarList.Title);
                    calItem.DisplayFormUrl = String.Format(
                        "/Lists/{0}/DispForm.aspx", calendarList.Title);
                    calItem.EndDate = (DateTime)listItem["End Time"];
                    calItem.Description = listItem["Description"].ToString();
                    if (listItem["Location"] != null)
                    {
                        calItem.Location = listItem["Location"].ToString();
                    }
                    items.Add(calItem);
                }
                MonthlyCalendarView1.DataSource = items;
            }
    
        }
    
    }
    
    
    

When you run the project, the SharePoint site opens. The Web Part is automatically added to the Web Part Gallery in SharePoint. To test this project, you will perform the following tasks:

  • Add an event to each of two separate calendar lists.

  • Add the Web Part to a Web Part Page.

  • Select lists to include in the monthly calendar view.

To add events to calendar lists on the site

  1. In Visual Studio, press F5.

    The SharePoint site opens and the Microsoft SharePoint Foundation 2010 Quick Launch bar appears on the page.

  2. In the Quick Launch bar, under Lists, click Calendar.

    The Calendar page appears.

  3. Click Events, and then click New Event.

  4. In the Title box, type Event in the default calendar, and then click Save.

  5. Click Site Actions, and then click More Options.

  6. In the Create page, click Calendar, name the calendar, and then click Create.

    The Custom Calendar page appears.

  7. Add a new event to the custom calendar named Event in the custom calendar.

To add the Web Part to a Web Part Page

  1. Click Site Actions, and then click More Options.

  2. In the Create page, click Web Part Page, and then click Create.

  3. In the New Web Part Page page, name the page SampleWebPartPage.aspx, and then click Create.

    The Web Part page appears.

  4. Select any zone on the Web Part page.

  5. At the top of the page, click the Insert tab, and then click Web Part.

  6. Click the Custom folder, click the VisualWebPart1 Web Part, and then click Add.

    The Web Part appears on the page. The following controls appear on the Web Part:

    • A monthly calendar view.

    • An Update button.

    • A Calendar check box.

    • A Custom Calendar checkbox.

To select lists to include in the monthly calendar view

  • In the Web Part, select calendars that you want to include in the monthly calendar view, and then click Update.

    Events from all selected calendars appear in the monthly calendar view.

Community Additions

ADD
Show:
© 2014 Microsoft