Microsoft

Blog Categories

Subscribe to RSS feed

Archives

Follow Microsoft Technologies on Pinterest

NewsGator Custom Activity with an Event Receiver

If you have NewsGator on your SharePoint site and want to refine the look or functionality of some items in the newsfeed, there is a good chance that you will be building a custom activity type.

Here are a few examples of where custom activity types can help:

·items from a non-community list trigger posts to the newsfeed

·disable “share” functionality for particular newsfeed items

·send out updates from a group account without forcing your users to remember an extra set of account credentials

None of this functionality is built into out-of-the-box NewsGator, but is easy enough to create with custom activity types.

Of course, NewsGator documentation outlines the process to create a custom activity for NewsGator, and you can find sample code in the NewsGator developer community. That said, I found myself going back and forth between 1.2 and 2.0, and 2.1 documents to piece together what I needed, so hopefully the steps below can simplify the creation process for you. This information may also be useful to you if you have NewsGator 1.2 custom activities that you would like to convert to 2.0 or 2.1.

Because NewsGator customizations rely on convention over configuration, it’s very important to follow the naming conventions very precisely. I color-coded the names that need to in the code below.

Key components

References

Add the following references:

NewsGator.Social

NewsGator.Social.Data

Microsoft.SharePoint.Administration

NewsGator.Social.ServerData

Resource file

·Defines the activity name and template

·Make sure it has the same name as your project. (E.g FullApplicationName -> FullApplicationName.resx)

·The value for FullApplicationName_ActivityTypeName_Display can be anything you like. It’s just a display name

·The value for FullApplicationName_ActivityTypeName_Template can contain any custom text, HTML, and replacement tokens from the table below. Note that you can use HTML in the template, but HTML in the ActivityValue will cause your post not to show up in the newsfeed.

Property

Type

Replacement Token

ActivityValue

string

{Value}

Author

UserProfileReference

{Publisher}

DateValue

DateTime?

{Date}

Link

SocialLink

{Link}

Link2

SocialLink

{Link2}

LinksList

List<SocialLink>*

{List}

Name

String

{Name}

*See NewsGator.Social API for an explanation of how to use lists of links.

·Example:

 

Installer/uninstaller feature

·You can name this one whatever you want. (referred to as InstallerFeatureName from here on out)

·Farm scope

·Contains Installation code for your custom type to appear in the Farm Features list and in the activity types list in the user preferences (a little more about that later).

·Contains custom module: it defines customizations like JavaScript, CSS. (Use firebug or some other browser extension to pinpoint which elements you want to customize)

·How to:

    • Create a new feature, rename to your liking
    • Add an Event Receiver to your feature
    • Replace the 1st two methods with the code below
    • Replace the names, productModuleGuid, and PublicKeyToken
    • Add appropriate using statements

//you can use the auto-generated value that’s been assigned to this feature receiver

Guid productModuleGuid = new Guid(“bf7d9e84-43e9-42cd-b520-98b2cc156177“);

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

ActivityTemplateManager.AddActivityTemplate(“FullApplicationName“, “ActivityTypeName“);

ActivityTemplateManager.InstallTemplates();

var customModule = new ActivityModule

{

Identifier = productModuleGuid,

EventType = “ActivityTypeName“,

// get this info from GAC:

ResourceAssembly = “FullApplicationName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3aadd33936b0“,

ResourceBaseName = “FullApplicationName.Resources.Events”,

FilterResourceName = “Filter”,

// Add this CSS file whenever we render the stream:

CustomCSS = “/_layouts/FullApplicationName/CssFileNames.css”,

// Adds this JS file whenever we render the stream. Use commas to delimit multiple files.

CustomScript = “/_layouts/FullApplicationName/JSFileNames.js”,

};

SPFarm.Local.AddActivityModule(customModule);

SPFarm.Local.Update();

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPFarm.Local.RemoveActivityModule(productModuleGuid);

SPFarm.Local.Update();

base.FeatureDeactivating(properties);

}

Triggering event receiver

·You can name this one whatever you want (referred to as EventReceiverClass from here on out)

·Put this into event receiver method that should trigger your activity:

·Note: I’m impersonating the person who is listed in the Author column of my trigger list.

·How to:

    • Add an Event Receiver
    • Copy the code below into the method you’re trying to customize (the blue code is what you will get just by adding an event receiver)
    • Add appropriate using statements
    • Be sure to change the site URL below

public override void ItemAdded(SPItemEventProperties properties)

{

base.ItemAdded(properties);

SPFieldUserValue fromUserValue = new SPFieldUserValue(properties.Web, properties.ListItem[“Author”].ToString());

SPUser user = fromUserValue.User;

String URL = @”http://www.yourURL.com“;

var activity = new SocialActivity

{

ActivityType = “ActivityTypeName“,

Application = “FullApplicationName“,

ActivityValue = “the text that replaces the {Value} piece of the template that you defined in the resx file.”,

ItemPrivacy = NGPrivacy.Public,

NewsGatorIndex = true,

Link = new SocialLink() { Href = URL, Value = “link text”, Name = “link text” },

Author = new UserProfileReference() { UserName = user.LoginName },

};

var fm = new ActivityFeedWriter();

fm.CreateActivity(activity);

}

·If the logged in user is not the author and your code doesn’t have the rights to impersonate the author, the activity will not post.

·If your template doesn’t require an ActivityValue, assign it to be “”. Leaving out this field causes an error and your activity won’t post.

Receiver feature

·You can name this one whatever you want (referred to as ReceiverFeatureName from here on out)

·Web scope. Note: in this example, I am using web scope, but depending on what event triggers your receiver, you may want a different scope. Hypothetically, you could use Site scope for a list receiver, just make sure that you are consistent between the feature definition and your receiver code.

·How to:

    • Create a new feature, rename to your liking
    • Add an Event Receiver to your feature
    • Put this into FeatureActivated method of the feature receiver and don’t forget to replace the names and PublicKeyToken:

using (SPWeb web = (SPWeb)properties.Feature.Parent)

{

SPList list = web.Lists[“ListName“];

// get this info from GAC, same as in activity module:

string assembly = “FullApplicationName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3aadd33936b0“;

// in receiver name, make sure you include the namespace too

// it will probably look silly/redundant like appName.ClassName.ClassName

string itemReceiverName = “FullApplicationName.EventReceiverNamespace. EventReceiverClass“;

// whichever events you want to address, list them here

list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assembly, itemReceiverName);

}

 

    • Put this into FeatureDeactivating method of the feature receiver and don’t forget to replace the names:

 

using (SPWeb web = (SPWeb)properties.Feature.Parent)

{

SPList list = web.Lists[ListName“];];

SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

foreach (SPEventReceiverDefinition receiver in eventReceivers)

{

if (receiver.Class.EqualsFullApplicationName.EventReceiverNamespace. EventReceiverClass))

{

receiver.Delete();

break;

}

}

}

 

Check if the Custom Activity Type actually got created

1.Make sure that the feature at farm level named FullApplicationName InstallerFeatureName exists and is activated.

2.Make sure that the feature at site level named FullApplicationName ReceiverFeatureName exists and is activated.

3.Make sure that in Newsfeed Settings, “Activities I am following” section there is a checkbox for Activity Name.

If you were able to deploy your solution, but any of the above didn’t happen, check for mismatches between the names in resx, installer feature, receiver feature, and event receiver. For example, a new checkbox in the “Activities I am following” section with no text next to it probably means that you mismatched the ActivityTypeName in the installer and the ActivityTypeName in the resx file.

Why Farm Scope for Installer Feature?

Because if you want to post to my sites, they are likely to be in a separate web app, and you will need to write into an activity feed across web apps.

Where permissions can fail…

If the logged in user is posting activities on behalf of someone else (e.g. an editor is approving a writer’s content).

Per NewsGator Documentation: All application pool accounts on the LOCAL farm are automatically granted delegation and impersonation rights. If you expose an endpoint that allows a system user to delegate event creation on behalf of a user, you will need to grant the calling user Delegation and Impersonation Rights on the service application. To do this, highlight the service application in Central Administration: Manage Service Applications, and select “Administrators”. Add the user and grant them “Delegation and Impersonation” rights. (No other rights are needed for this account.)

What other documentation is available?

Most likely if you are customizing NewsGator activities, you have access to NewsGator-provided resources:

·NewsGator.Social API 2.1

·NewsGator SocialSites Training Labs 2.1

·SampleSocialApplication in the SS2010 Lab Materials 2.1

Tags:

2 thoughts on “NewsGator Custom Activity with an Event Receiver

  1. Darya Orlova Post author

    You could include a table in the resource to format it, but you can only populate one {value} replacement token. You wouldn’t be able to insert a whole table in the activity, nor would you be able to populate multiple custom cells in a table with your code. 

    So, you can do something like
    < table>
    < tr> Author Project info {publisher} {value}

    And in the code say 
    ActivityValue = projectName+” “+projectStatus

    But not 

    Author Project Status
    {publisher} {project} {status}

    And you won’t be able to do a table with information from multiple projects because the template has predefined replacement tokens that you can fill in and they’re limited in number. The only one that has multiples is the link list. If you need a table of links, you can do that.

    I hope that helps. Let me know if I can clarify further.

  2. Srividya

    Hi
    Thanks for the post.we are trying to display the external information like project information in the activity feed, so that we can utilize the pivot viewer for searching (like the time line in facebook). for that we have to post the data using code from external source to Newsgator’s activity stream. For which we have to create our own custom activity like project and the template for the activity should be some table. In that case is it possible to have a html tag included in the resource file for the custom activity.

Leave a Reply