Updating a Registration List Item

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, as discussed in Registration Approval Page Load, the registration approval form appears. Figure 1 illustrates this form.

Ff650477.registrationapproval_v1p2(en-us,PandP.10).png

Figure 1
Manager approval form

The manager clicks the Submit button to process the registration approval. The code associated with this button click is an example of how to programmatically update an item in a SharePoint list. The control flow is illustrated in Figure 2.

Ff650477.training_manage_registration_approval(en-us,PandP.10).png

Figure 2
Registration approval control flow

Selecting the Submit button causes the RegistrationApproval.Submit_Click method to execute. The following sections explain the Registration Approval process by examining the methods that are called. The calls occur in the following order:

  1. RegistrationApproval.Submit_Click. This method is invoked by Internet Information Services (IIS) when the manager clicks the Submit button.
  2. RegistrationApproval.ProcessApproval. This is a helper method that invokes the presenter.
  3. RegistrationApprovalPresenter. ProcessApproval. This method has the business logic to process a registration approval.
  4. RegistrationRepository.Update. This method is provided by the repository layer for updating a registration list item.
  5. BaseEntityRepository<T>.UpdateListItem. This is a helper method provided by the registration repository's base class.
  6. ListItemRepository.Update. This method is an application utility method for updating items to SharePoint lists.

The Submit_Click Handler

The following code shows the RegistrationApproval.Submit_Click method that handles the registration approval button click. It is located in the Contoso.TrainingManagement.Web project in the RegistrationApproval\RegistrationApproval.aspx.cs file.

protected void Submit_Click(object sender, EventArgs e)
{
    if ( Page.IsValid )
    {
        SPUtility.ValidateFormDigest();
        ProcessApproval(SPContext.Current.Web, (string)ViewState[taskIdViewStateKey], (string)ViewState[sourceUrlViewStateKey]);
    }
}

private void ProcessApproval(SPWeb web, string taskID, string sourceUrl)
{
    RegistrationApprovalPresenter presenter = 
                                     new RegistrationApprovalPresenter(this);
    bool success = presenter.ProcessApproval(web, taskID, Status.SelectedValue);

    if ( success )
    {
        SPUtility.Redirect(sourceUrl, SPRedirectFlags.Default, HttpContext.Current);
    }
} 

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

The ProcessApproval method continues the update by calling into the presenter through the RegistrationApprovalPresenter.ProcessApproval method. This method is described in Performing the Registration – Presentation Layer. The Status.SelectedValue property gives the new status that will be used to update the registration associated with the current task ID.

Performing the Registration – RegistrationApprovalPresenter

The RegistrationApprovalPresenter.ProcessApproval method is the entry point into the presenter when approving or rejecting a registration. The ProcessApproval method is located in the RegistrationApprovalPresenter.cs file of the Contoso.TrainingManagement.Web project.

public bool ProcessApproval(SPWeb web, string taskID, string registrationStatus)
{
    Registration registration = null;

    bool success = GetTaskAndRegistration(web, _view, taskID, out registration);

    if ( success )
    {
        registration.RegistrationStatus = registrationStatus;
        IRegistrationRepository registrationRepository = 
                         ServiceLocator.GetInstance().Get<IRegistrationRepository>();
        registrationRepository.Update(registration, web);
    }

    return success;
} 

This code retrieves the Registration object that is associated with the current task ID. This is explained in more detail in Rendering the Registration Approval View. The Registration class is described in more detail in Register for a Course Use Case use case.

Next, the code uses the Service Locator pattern 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 Service Locator on MSDN.

Next, the code updates the RegistrationStatus field of the registration object to contain the new status given by the registrationStatus field that is specified by the user. The new status will be Approved or Rejected.

Finally, the code invokes the RegistrationRepository class's Update method to update the item in the registration list stored in SharePoint.

Updating the Registrations List Item – Repository Layer

In the Repository component of the Training Management application, the following three methods are involved in processing the updating a registration list item:

  • RegistrationRepository.Update
  • BaseEntityRepository<T>.UpdateListItem
  • ListItemRepository.Update

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

public void Update(Registration registration, SPWeb web)
{
    UpdateListItem(registration, web);
} 

The Update method uses the functionality provided by the base class BaseEntityRepository<T>.

The following code is the BaseEntityRepository<T>.UpdateListItem method. This code is located in the BaseEntityRepository.cs file of the Contoso.TrainingManagement.Repository project.

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

    listItemRepository.Update(web, this.ListName, entity.Id, fields);
}

The following code is the RegistrationRepository.GatherParameters method.

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

    return fields;
}

This code creates a Dictionary object that contains values for each of the fields.

The UpdateListItem method calls the ListItemRepository Update method to process the modification.

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

public void Update(SPWeb web, string listName, int listItemId, Dictionary<Guid,
                                                                     object> fields)
{
    SPListItem item = null;
    SPListItemCollection collection = null;

    collection = web.Lists[listName].GetItems(this.BuildQuery(listItemId));

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

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

        item.Update();
    } 
} 

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

  1. Get the SPListItem associated with the current list item ID. (The code for this is not shown.)
  2. Set each field of the 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.