Adding an Item to the Registration List

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.

After the page loads, the registration form appears. Figure 1 illustrates this form.

Ff648468.registration_OK(en-us,PandP.10).png

Figure 1
Training Management registration form

The employee clicks the Register for Course button to process the registration. The code associated with the button click is an example of how to programmatically add a new item to a SharePoint list. Figure 2 illustrates the control flow of the use case.

Ff648468.training_manage_registration(en-us,PandP.10).png

Figure 2
Course registration control flow

Selecting the Register for Course button causes the CourseRegistrationSubmit_Click method to execute. The following sections explain the course registration process by examining the methods that are called. The calls occur in the following order:

  1. CourseRegistration.Submit_Click. This method is invoked by Internet Information Services (IIS) when the user clicks the Register for Course button.
  2. CourseRegistrationPresenter. Register. This method validates the arguments and invokes the repository layer to add a new list item.
  3. CourseRegistrationPresenter. PerformRegistration. This is a helper method of the presentation layer.
  4. RegistrationRepository.Add. This method is provided by the repository layer for adding a new Registrations list item.
  5. BaseEntityRepository<T>.AddListItem. This is a helper method provided by the registration repository's base class.
  6. ListItemRepository.Add. This is an application utility method for adding items to SharePoint lists.

The Submit_Click Handler

The following code shows the CourseRegistrationSubmit_Click method that handles the registration button click. It is located in the CourseRegistration.aspx.cs file in the Contoso.TrainingManagement.Web project.

protected void Submit_Click(object sender, EventArgs e)
{

    CourseRegistrationPresenter presenter = new CourseRegistrationPresenter(this);
    presenter.Register(SPContext.Current.Web, SPContext.Current.Web.CurrentUser.LoginName);
}

The Submit_Click method is registered as the button's handler in the CourseRegistration.aspx file.

The Register method then calls SharePoint methods and ASP.NET methods to register the user for the course. The PerformRegistration method executes as part of the Register method. This code is in CourseRegistrationPresenter.cs.

Performing the Registration — Presentation Layer

The CourseRegistrationPresenter.Register method is the entry point into the presenter when adding a new registration. The method invokes a private helper method named PerformRegistration. The PerformRegistration method is located in the CourseRegistrationPresenter.cs file of the Contoso.TrainingManagement.Web project.

private void PerformRegistration(SPWeb web, TrainingCourse course, SPUser user)
{
     Registration registration = new Registration();
     registration.Title = String.Format("{0} - {1}", course.Code, course.Title);
     registration.CourseId = course.Id;
     registration.UserId = user.ID;
     registration.RegistrationStatus = "Pending";

     IRegistrationRepository registrationRepository = 
                                                       ServiceLocator.GetInstance().Get<IRegistrationRepository>();

     int id = registrationRepository.Add(registration, web);
}

This code constructs a new Registration object and populates its properties with values that define the course registration to be added to the SharePoint list. The status of the new registration is Pending. The Registration class contains the same fields as the Registration Content Type in SharePoint.

The code demonstrates how the Service Locator pattern is used to access the Training Management application's RegistrationRepository class. This class is exposed through an interface named IRegistrationRepository. For more information about the Service Locator pattern, see Design Patterns in this guidance and Service Locator on MSDN.

Finally, the code invokes the RegistrationRepository class's Add method to add the item to the Registrations list stored in SharePoint.

Adding the List Item — Repository Layer

In the Registration Repository component of the Training Management application, a constructor and the following three methods are involved in processing the addition of a new Registrations list item:

  • RegistrationRepository.Add
  • BaseEntityRepository<T>.AddListItem
  • ListItemRepository.Add

The following code is for the RegistrationRepository.Add method. This code is located in the RegistrationRepository.cs class of the Contoso.TrainingManagement.Repository project.

public int Add(Registration registration, SPWeb web)
{
     return AddListItem(registration, web);
}

The Add method uses the functionality that is provided by the base class BaseEntityRepository<T>. The following code is the BaseEntityRepository<T>.AddListItem method. This code is located in the BaseEntityRepository.cs file of the Contoso.TrainingManagement.Repository project.

// in class BaseEntityRepository<T>
protected int AddListItem(T entity, SPWeb web)
{
    Dictionary<Guid, object> fields = GatherParameters(entity, web);

    SPListItem item = null;

    item = listItemRepository.Add(web, this.ListName, fields);

    return (int)item[new Guid(Fields.Id)];
}

// in class RegistrationRepository  
protected override Dictionary<Guid, object> 
                                    GatherParameters(Registration entity, SPWeb web)
 {
     Dictionary<Guid, object> fields = new Dictionary<Guid, object>();
     fields.Add(new Guid(Fields.Title), entity.Title);
     fields.Add(new Guid(Fields.CourseId), entity.CourseId);
     fields.Add(new Guid(Fields.UserId), entity.UserId);
     fields.Add(new Guid(Fields.User), web.SiteUsers.GetByID(entity.UserId));
     fields.Add(new Guid(Fields.RegistrationStatus), entity.RegistrationStatus);

     return fields;
}

The preceding code creates a Dictionary object that contains values for each of the fields. It then creates an instance of the helper class named ListItemRepository. The AddListItem method calls the ListItemRepositoryAdd method to process the addition. It returns the ID of the newly created item.

The following code is for the ListItemRepositoryAdd method is shown below. This code is found in the ListItemRepository.cs file in the Contoso.TrainingManagement.Repository project.

public SPListItem Add(SPWeb web, string listName, Dictionary<Guid, object> fields)
{
    SPListItem newItem = null;

    newItem = web.Lists[listName].Items.Add();

    foreach ( Guid key in fields.Keys )
    {
        newItem[key] = fields[key];
    }

    newItem.Update();

    return newItem;
}

In summary, creating a new list item is a three-step process:

  1. Use the SPListItemCollection class's Add method to create a new empty list item in the Registrations list.
  2. Set each field of the new item. The code****uses the Dictionary object that was given as an argument to the method invocation. The dictionary contains key/value pairs that specify each of the list item's field values.
  3. Use the SPListItem.Update method to commit the changes.
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.