Let's Go Another Round: Sitecore Webforms

I’ve been working on Sitecore webforms so much, it’s becoming content for my dreams and out-of-work references. Not that it’s a bad thing, I think they’re a powerful tool that can solve a lot of problems right out of the box. One problem we had here at work was not solved by just Webforms For Marketers, but with some Sitecore API code and a little research we were off to the races!

Many of our clients have Webforms For Marketers in place, and need a way to quickly summarize the submissions along with the user’s analytics data. Easy enough with the Form Reports included with Sitecore Webforms For Marketers, right? What about getting the analytics data for each submission, like the IP address it was submitted from or any of the other analytics data the Sitecore OMS gathers? It’s easy enough to look one-by-one in the Form Reports (I hope you knew you could do that), but we needed a way to summarize the IP Address along with the submission content in a spreadsheet. But how??

The first problem was getting the list of webform submissions for each form. After some heavy Googling and scanning the Sitecore Developer Network, I have this:

List args = new List();
args.Add(new GridFilter("storageName", string.Empty, GridFilter.FilterOperator.Contains));
args.Add(new GridFilter("dataKey", webform.ID.ToString(), GridFilter.FilterOperator.Contains));
List submits = Sitecore.Forms.Data.DataManager.GetForms().GetPage(new PageCriteria(0, 0x7ffffffe), null, args).ToList();

This snippet uses all Sitecore APIs and works like a dream. The only missing factor is getting the ID of the webform item, which I got by supplying the site tree location of the form (/sitecore/system/modules/web forms for marketers/website/). Basically it uses filters (Sitecore.Web.UI.Grids.GridFilter) to find the submissions for that particular form (dataKey contains the webform’s ID). The code returns a list of Sitecore.Forms.Data.IForms, which contain all the submitted fields and values along with a link to the related analytics data. Bingo!

Once I had the list of submitted forms, I had to get the analytics data into a data object along with the submitted form. It didn’t take long to find the IForm.SessionID property, which is the key to a user’s analytics data stored in the analytics database. For this particular report, we wanted to show the IP address of the user and the site which referred them along with the submitted values.

Sitecore provides an entire Analytics API to help with accessing the analytics data the OMS has collected, which I take advantage of here. The Analytics Manager object has a function for getting the Session object from the ID, which holds the IP Address of the user who submitted the form.

As well as the IP Address, the Referring Site needs to be included in the report. Strangely enough, the Analytics Manager doesn’t have a GetReferringSiteByReferringSiteId function, but no matter. I was able to get a list of all referring sites, and a simple IEnumerable got the one I was looking for.

 

if (AnalyticsTracker.IsActive)
{
    Sitecore.Analytics.Session session = AnalyticsManager.GetSessionBySessionId(submission.SessionId);
    if (session != null)
    {
        submissionWrapper.IPAddress = session.IpAddress;
        List referringSites = Sitecore.Analytics.AnalyticsManager.GetReferringSites();
        submissionWrapper.ReferringSite = referringSites.FirstOrDefault(site => site.ReferringSiteId == session.ReferringSiteId);
    }
}

The rest of the work involved to render the data is neither here nor there – I simply made a repeater with literals for each form field and value along with my acquired analytics data and an exporting function to Excel.  The application has been in full swing for a couple months now and the smiles on our Marketing Team’s faces tell me it’s doing its job right.

« Prev Article
Next Article »