Tuesday, February 28, 2012

Saving a new ExtendedAttribute for a User Object via a Velocity Script in a Telligent Community 6.0 Studio Widget.

I did not notice way to set an ExtendedAttribute on a user (or any object) from the OOTB velocity core API documentation, so this post will attempt to show how you can use an IScriptedContentFragmentExtension to accomplish this.

Step 1:  Create your IScriptedContentFragmentExtension definition

    public class UserExtensionDefinition : IScriptedContentFragmentExtension
    {
        public object Extension
        {
            get { return new UserExtension(); }
        }

        public string ExtensionName
        {
            get { return "companyName_v1_user"; }
        }

        public string Description
        {
            get { return "Common user extensions needed by the Community"; }
        }

        public void Initialize()
        {

        }

        public string Name
        {
            get { return "Community User Extensions"; }
        }
    }

Step 2:  Create the underlying class that will contain the method to set the ExtendedAttribute for the user

using System;
using System.Collections.Generic;
using System.Linq;
using Telligent.Evolution.Components;
using Entities=Telligent.Evolution.Extensibility.Api.Entities.Version1;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution;

namespace Evolution.Extensions.v1
{  
    public class UserExtension
    {
        public bool SaveExtendedAttribute(int userId, string key, string value)
        {
            User user = Telligent.Evolution.Users.GetUser(userId);

            if (user != null)
            {
                user.SetExtendedAttribute(key, value);
                Telligent.Evolution.Users.UpdateUser(user);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}


Step 3:  Enable the extension from the "Plugins" area in the control panel

Check the box next to the Community User Extensions and click "Save"


Step 4:  Invoke the object via velocity syntax

   #set($attributeSavedBooleanResponse = $companyName_v1_user.SaveExtendedAttribute($core_v2_user.Accessing.Id, "attributeKey","attributeValue"))


I hope this helps people when trying to use velocity script to set an extended attribute for a user (or any other object).  If there is an easier way or something out of the box i'm missing, please leave a comment!

1 comment:

  1. Hey, Just want to say this post, has been a fantastic help! I have been working on a telligent project and have found this post and other posts on your telligent blog more useful than most of the articles in the entire Telligent community! good work.

    ReplyDelete