Share via


Course Registration Page Load

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The page load process begins when an employee submits an HTTP request, such as http://myserver/sites/training/CourseRegistration.aspx?ID=1, to begin the registration process. The HTTP request initiates the Page_Load handler. It validates the request and calculates the fields that are used in the ASPX page. The page load process is an example of how to programmatically query for items in a SharePoint list. The diagram shown in the previous topic illustrates the control flow of this use case.

The following sections explain the page load process by examining the methods that are called. The calls occur in the following order:

  1. CourseRegistration.Page_Load. Internet Information Services (IIS) invokes the Page_Load method of the CourseRegistration class when it receives the initial HTTP request.
  2. CourseRegistrationPresenter.RenderCourseRegistrationView. This method validates the registration context and calculates the values needed to display the registration form. To see an example of the registration form, see Register for a Course Use Case.
  3. CourseRegistrationPresenter.GetCourse. This method confirms whether the course ID that is provided as an argument corresponds to one of the Training Course list items in SharePoint. If the course ID corresponds, the course information is returned through an out parameter.
  4. TrainingCourseRepository.Get. This method queries for data in the Training Courses list.
  5. BaseEntityRepository<T>.GetListItem. This code demonstrates how to create and configure an instance of the SPQuery class.
  6. ListItemRepository.Get. This code locates an item within a SharePoint list.

The following sections describe the work that is performed by each of these methods during the registration page load.

The Page_Load Method

This is the code for the Page_Load method that is invoked by ASP.NET. The code is located in CourseRegistration.aspx.cs located in the Contoso.TrainingManagement.Web project.

protected void Page_Load(object sender, EventArgs e)
{
   if ( !Page.IsPostBack )
   {       CourseRegistrationPresenter courseRegistrationPresenter = new CourseRegistrationPresenter(this);
       courseRegistrationPresenter.RenderCourseRegistrationView(SPContext.Current.Web, SPContext.Current.Web.CurrentUser.LoginName);

       this.CourseList.DataValueField = "Id";
       this.CourseList.DataTextField = "Code";
       this.CourseList.DataBind();
   }
} 

The CourseRegistration page's Page_Load handler gets its data from the following two sources that together represent the current execution context:

  • The ASP.NET Request class. The Request class gives access to the parameters passed in as part of the HTTP request.
  • The SharePoint SPContext class. The SPContext class is a SharePoint class that provides information about the run-time context of the SharePoint application. The SPContext.Current property contains an object that provides information about the SharePoint context that exists when the page loads. The Web property of this context object gives the current SharePoint Web site. The type of the Web site is SPWeb.

Note

The SPWeb object returned by the SPContext.Current.Web property returns an object that represents the current SharePoint site. This object contains methods and properties to access the users, lists, and other content elements of the SharePoint application. For more information, see SPWeb Class (Microsoft.SharePoint) on MSDN.

The Page_Load method constructs a presenter (CourseRegistrationPresenter) and invokes the CourseRegistrationPresenter.RenderCourseRegistrationView method.****

The CourseRegistrationPresenter class validates the registration data and sets the page properties that are required to present the registration form to the user. For example, there is an error message if the user has already registered for the course. This class is described in the next section, Rendering the Course Registration View.

Rendering the Course Registration View

The RenderCourseRegistrationView method of the CourseRegistrationPresenter class validates the registration context and calculates the values needed to display the registration form. This form is illustrated at the beginning of the Register for a Course Use Case. The RenderCourseRegistrationView is invoked by the CourseRegistration class as part of the page load process.

The RenderCourseRegistrationView method calls the following two private helper methods through the GetCourseUserRegistration helper method:

  • GetCourse. This method checks to see whether the course ID provided as an argument corresponds to one of the TrainingCourse list items in SharePoint.
  • GetUserRegistration. The method checks that the registration request does not already exist. This method is not shown in the following code because it is similar to the GetCourse method.

If all the conditions are not met, an error page is displayed.

The following is the code for the GetCourse method.

private bool GetCourse(SPWeb web, ICourseRegistrationView view, 
                         int courseId, out TrainingCourse course)
{
    bool success = false;

    ITrainingCourseRepository trainingCourseRepository =  
                                                   ServiceLocator.GetInstance().Get<ITrainingCourseRepository>();

    course = trainingCourseRepository.Get(courseId, web);

    if ( course != null )
    {
        success = true;
    }
    else
    {
         view.ContentMessage = "The Course selected was not a valid.";
    }

    return success;
}

The preceding code demonstrates how the Service Locator pattern is used to access the Training Management application's TrainingCourseRepository component. The next section explains how the TrainingCourseRepository component's Get method queries SharePoint data. For more information about the Service Locator pattern, see Design Patterns in this guidance and Service Locator on MSDN.

Constructing a TrainingCourse Query Using CAML

The TrainingCourseRepository class includes a Get method that queries SharePoint for the Training Courses list item that corresponds to a given integer ID. The code is located in the TrainingCourseRepository.cs file of the Contoso.TrainingManagement.Repository project.

public TrainingCourse Get(int id, SPWeb web)
{
    StringBuilder queryBuilder = new StringBuilder("<Where>");
    queryBuilder.Append("<Eq><FieldRef Name='ID'/>");
    queryBuilder.Append(string.Format("<Value Type='Integer'>{0}</Value></Eq>", id));
    queryBuilder.Append("</Where>");

    return GetListItem(queryBuilder.ToString(), web);
}

This method constructs an XML query string and passes it to the GetListItem method that is provided by its base class, BaseEntityRepository<T>. The XML query string restricts the search to list items that match the course ID. SharePoint queries use the Collaborative Application Markup Language (CAML). For more information about the SharePoint query syntax, see Query Element (Query) on MSDN. For tools to help you write CAML queries, see CAML Query Tools.

Querying SharePoint via the ListItemRepository

The GetListItem method is defined in the BaseEntityRepository<T> class. The code is located in the BaseEntityRepository.cs file in the Contoso.TrainingManagement.Repository project.

protected T GetListItem(string caml, SPWeb web)
{
    T entity = null;

    SPQuery query = new SPQuery();
    query.Query = caml;

    SPListItem item = null;

    item = listItemRepository.Get(web, this.ListName, query);

    if (item != null)
    {
        entity = PopulateEntity(item);
    }

    return entity;
}

The preceding code demonstrates how to create and configure an instance of the SPQuery class. SPQuery objects use the CAML query language to execute data queries in SharePoint. The code creates an instance of the class and sets its Query property to a string in the CAML query syntax.

Next, the code invokes the Get method of an instance of a helper class named ListItemRepository. This class is provided by the application's Contoso.TrainingManagement.Repository component to encapsulate some aspects of interacting with SharePoint lists.

The following code is the Get method. It is located in the ListItemRepository.cs file in the Contoso.TrainingManagement.Repository project.

public SPListItem Get(SPWeb web, string listName, SPQuery query)
{
    SPListItem item = null;
    SPListItemCollection collection = null;

    collection = web.Lists[listName].GetItems(query);

    if ( collection != null && collection.Count > 0 )
    {
        item = collection[0];
    }

 return item;
}

The collection indexer (using "[]" syntax) locates the list with the name given by the listName parameter. In this case, it will be the string "Training Courses".

Next, the code invokes the GetItems method of the SPList class provided by SharePoint. This executes the CAML query against the given list.

The result of the query is an SPListItemCollection object. In the Training Management application, queries may return either a collection with zero values or a single value. If the requested item is found, the collection will be nonempty and the item found will be returned; otherwise, the value null will be returned.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Footer image

To provide feedback, get assistance, or download additional, please visit the SharePoint Guidance Community Web site.

Copyright © 2008 by Microsoft Corporation. All rights reserved.