How to: Create a New Activity Type

Applies to: SharePoint Server 2010

Microsoft SharePoint Server 2010 enables you to extend the My Newsfeed page on the My Site host by creating new activity types from which you can create and insert events into user newsfeeds. The sample method below demonstrates how to create a new ActivityType object and associate it with an ActivityTemplate object, and then (through that ActivityTemplate) associate it to the resource file that defines how the event is displayed in newsfeeds. See Code Sample: Multicasting Activity Events Console Application and Code Sample: Send a Link to Your Colleagues Activity Event for code samples that demonstrate how to perform tasks that are included in this topic. This topic assumes that you have created and deployed a resource (.resx) file to the \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources directory and that you have added the following references to your Microsoft Visual Studio 2010 project:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

Creating a New Activity Type

Each ActivityType object contains an ActivityTemplates property. This property contains a collection of ActivityTemplate objects. The ActivityTemplate class contains properties that connect it to display templates (defined in resource files) that you must create and deploy with any custom applications that you build for creating and gathering activity events in an activity feed. For instructions about how to create a resource file, see Creating Resource Files. Because you create a specific ActivityType only once, your solution should ensure that it does not create duplicates. The following sample method demonstrates how to create a new ActivityType object and define its display.

private void SetupCustomActivity(SPSite site)

    string resFile = "name of your resource (.resx) file";
    //Get an SPServiceContext from the site.
    SPServiceContext context = SPServiceContext.GetContext(site);
    //Create a UserProfileManager.
    UserProfileManager upm = new UserProfileManager(context);
    //Get the current user's profile.
    UserProfile p = upm.GetUserProfile(true);

    //Create an activity manager.
    ActivityManager am = new ActivityManager(p, context);
    //Make sure that the current user has permission to do this
    bool hasrights = am.PrepareToAllowSchemaChanges();

    // Create an activity application.
    ActivityApplication app = am.ActivityApplications["Name of Activity Application"];
    if (app == null)
      app = am.ActivityApplications.Create("Name of Activity Application");

    //Create an ActivityType; check to see whether it already exists.
    ActivityType activityType = app.ActivityTypes["Name of Activity Type"];
    if (activityType == null)
      activityType = app.ActivityTypes.Create("Name of Activity Type");
//Associate the ActivityType with the resource file that you have deployed and with the
//localized string name stored in the resource file.
      activityType.ActivityTypeNameLocStringResourceFile = resFile;
      activityType.ActivityTypeNameLocStringName = "Activity Type Localized String Name";
      activityType.IsPublished = true;
      activityType.IsConsolidated = true;
    //Create a single value ActivityTemplate; check to see whether it already exists.
    ActivityTemplate urlTemplateSV = activityType.ActivityTemplates[ActivityTemplatesCollection.CreateKey(false)];
    if (urlTemplateSV == null)
      urlTemplateSV = activityType.ActivityTemplates.Create(false);
//Associate the template with the resource file that you have deployed and with the
//localized string name stored in the resource file.
      urlTemplateSV.TitleFormatLocStringResourceFile = resFile;
      urlTemplateSV.TitleFormatLocStringName = "Activity Template Localized String Name ";

See Also




How to: Get Events for Users

How to: Create and Insert Events on a User's Newsfeed

Other Resources

Microsoft SharePoint Server 2010: Activity Feeds Console Application

Creating Resource Files