Adding an Item to the Registration List

After the page loads, the registration form appears. The following illustration shows this form.

Training Management registration form

Ff648799.db28c4fc-1487-4513-8ba3-0aa0a2115357(en-us,PandP.10).png

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. The following illustration shows the control flow of the use case.

Course registration control flow

Ff648799.eeb80aa4-6545-4087-8a81-5151d65b40e3(en-us,PandP.10).png

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 registration 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, user.Name);
     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 The Service Locator Pattern 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.

// This is 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)];
}

// This is 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. 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 provided 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.

Home page on MSDN | Community site