This is my first post on the wordpress platform. I’m planning to post here frequently (yes that’s what everybody says. Well we will see)

I’m currently working on a project/team site creation process in SharePoint 2010. Each site needs to be a separate site collection.

I started of with the creation of a site management list in which site requests are created by users.

Then an approval workflow is created every time a site request is added to the site request list. When workflow sets a site status to approved or rejected and Updating event handler kicks in which generates the site collection using the template selected in the site request.

All sounds good so far. The customer can modify the workflow whenever they need or like to, using SharePoint Designer, as long as the site status column is set to the correct values.

I used AfterProperties to collect the site request details and the record the result of the site creation and all worked in one go. Going well so far!

Then I implemented my workflow to start the event handler and this is where the problems started. Suddenly AfterProperties isn’t being recognised anymore except for the column that has been modified (in my case site status). Also updates to AfterProperties all generated exceptions.

I changed every occurrence of properties.AfterProperties to properties.ListItem and everything started working again. Here is the code of the event handler:

public override void ItemUpdating(SPItemEventProperties properties)
{

   string webTemplate = null;

   // Check that this is the right update
   if (properties.AfterProperties["SiteStatus"].ToString().Equals("Approved"))
   {
     base.EventFiringEnabled = false;

     switch (properties.ListItem["SiteType"].ToString())
     {
        case "Project":
           webTemplate = "MYTEMPLATE#2";
           break;
        case "Team":
           webTemplate = "MYTEMPLATE#3";
           break;
        default:
           webTemplate = "MYTEMPLATE#2";
           break;

     }

     try
     {
        string relativeSiteUrl = String.Concat("sites/", properties.ListItem["SiteURLName"].ToString());
        string title = properties.ListItem["Title"].ToString();
        string description = properties.ListItem["SiteDescription"].ToString();

        SPSite newSiteCollection = null;

        // Create a subsite
        SPWeb site = properties.ListItem.ParentList.ParentWeb;
        SPWebCollection subSites = site.Webs;

        // The creation of the site collection will only work if the primary administrator is set to the current user
        string primarySiteCollectionAdministratorLogin = site.CurrentUser.LoginName;
        string primarySiteCollectionAdministratorName = site.CurrentUser.Name;
        string primarySiteCollectionAdministratorEmail = site.CurrentUser.Email;

        uint localIdentifier = (uint)properties.ListItem.ParentList.ParentWeb.Locale.LCID;

        using (SPSite rootSite = new SPSite(properties.ListItem.ParentList.ParentWeb.Url))
        {
            newSiteCollection = rootSite.SelfServiceCreateSite(
               relativeSiteUrl,
               title,
               description,
               localIdentifier,
               webTemplate,
               primarySiteCollectionAdministratorLogin,
               primarySiteCollectionAdministratorName,
               primarySiteCollectionAdministratorEmail,
               null,
            null,
            null);
         }

         properties.ListItem["Errors"] = "Site creation successful";
         properties.ListItem["SiteCreationDate"] = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ");
         properties.ListItem["SiteStatus"] = "Active";

      }
      catch (Exception ex)
      {
         properties.ListItem["Errors"] = String.Concat("Failed to create site. Please contact the site administrator: ",  ex.Message.ToString());
         properties.ListItem["SiteStatus"] = "Failed";

      }

   }

   properties.ListItem.Update();
   base.EventFiringEnabled =true;
   base.ItemUpdating(properties);
}

so what happened?

When the event handler was started after manually updating the listitem AfterProperties worked and when it’s started as part of a workflow it suddenly isn’t working. So depending on how the event handler is called AfterProperties seems to work in a different way.

Advertisements