Home > Code > Fun and games with the ActivityEvent

Fun and games with the ActivityEvent

I haven’t seen a massive amount of documentation on MSDN or other posts about this, so I thought I would write a small note.

This details a way of extracting the info you see in the news feed on your ‘My Site’

First of all you need to get the the format used by the ActivityEvent, this done using ActivityType and ActivityTemplate.


ActivityType type = activityMan.ActivityTypes[activity.ActivityEventId];
ActivityTemplate template = type.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
string format = GetResourceString(template.TitleFormatLocStringResourceFile, template.TitleFormatLocStringName, (uint)CultureInfo.CurrentUICulture.LCID);

The format variable will now contain something along the lines of “{Pubisher} has posted a note {Link} on {PublishDate}”

Now you could at this point take the corresponding Properties in the ActivityEvent, but there is a whole load of formatting that needs to be done. The Publisher Tag needs to be turned into a link to the users profile page and display their name, etc, etc.

There is an easier way, you can use the static methods of the ActivityTemplateVariable.


ActivityTemplateVariable.DateOnlyToString(tag, variable, ContentType.Html, CultureInfo.CurrentCulture);

The problem here is that you have to pass in an ActivityTemplateVariable as one of the parameter and there is no obvious way of doing this.

There are no properties or methods in the ActivityEvent that give you an ActivityTemplateVariable or is there?

After further investigation of the TemplateVariable string property, you can see that this actually returns an XML string. This XML string is a de-serialised ActivityTemplateVariable object.


var variable = (ActivityTemplateVariable)FromXml(item.TemplateVariable, typeof(ActivityTemplateVariable));

You can now use this object to pass to the static method of ActivityTemplateVariable to return the full HTML representation of the tag.

If you then combine that with SimpleTemplateFormat you can then loop round all the tags and replace them.


var items = SimpleTemplateFormat.SimpleParse(formatToUse);

I’ll try and upload a code sample soon.

Advertisements
  1. LG
    25 October 2011 at 7:28 am

    Hi,

    Very interesting read. May I ask you where you get the “FromXml” method to get the activitytemplatevariable.

    • 25 October 2011 at 9:01 am

      Sorry, forgot to add that method. See below

      
      public object FromXml(string xml, Type objType)
      {
        var ser = new XmlSerializer(objType);
        var stringReader = new StringReader(xml);
        var xmlReader = new XmlTextReader(stringReader);
        object obj = ser.Deserialize(xmlReader);
      
        xmlReader.Close();
        stringReader.Close();
      
        return obj;
      }
      
      
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: