Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get a user's name and description from their ID in the SDL Tridion Anguilla framework

I have written a GUI extension for SDL Tridion 2011 SP1. The GUI consists of an extra ribbon button and event handler which is triggered when a component is saved.

My event handler is registered as follows:

PowerTools.Commands.ItemCommenting.prototype._execute = function (selection) {
    var item = $display.getItem();
    $evt.addEventHandler(item, "save", this.getDelegate(this._onItemSaved));
    $cme.getCommand("SaveClose")._execute(selection);
};

and the event handler looks like this:

PowerTools.Commands.ItemCommenting.prototype._onItemSaved = function (eventitem) {

    var comment = prompt("Please enter a comment", "");

    $messages.registerNotification("Saving user comments...");

    var commentitemid = eventitem.source.getId();
    var commenterid = eventitem.source.getCreatorId();
    var commenter = $tcm.getItem(commenterid);
    var commentername = commenter.getDescription();
    var commentdate = eventitem.source.getLastModifiedDate();
    var commentversion = eventitem.source.getVersion();

    //Call the service to update 
    PowerTools.Model.Services.AppDataServices.Append("ext:ItemCommenting", commentitemid, "<comment><user>" + commenterid + "</user><message>" + comment + "</message><datetime>" + commentdate + "</datetime><version>" + commentversion + "</version></comment>", null, null, null, false);

};

This is working fine, except that the variable commentername is always undefined. Is there a better approach for getting the name and description of a user?

Additionally, does anyone know if the value returned by eventitem.source.getCreatorId() is actually the Reviser or actually the person who created the item?

like image 351
Chris Summers Avatar asked Oct 09 '12 17:10

Chris Summers


2 Answers

I normally follow this approach in Anguilla:

  1. use $models.getItem(item Id) to load objects, heard from someone (@puf?) that this is cached.
  2. check if the object.isLoaded() and if so, execute my event handler
  3. if the object is not loaded, then listen to the event

It all comes down to something like this:

p.keyword = $models.getItem(p.keywordUri);
if (p.keyword.isLoaded()) {
    this._onReleaseKeywordLoaded();
} else {
    $evt.addEventHandler(p.keyword, "load", this.getDelegate(this._onReleaseKeywordLoaded));
    p.keyword.load();
}

You would then call your model's webservice from the event handler, since you're sure the object will be loaded by then.

In your current code you're probably trying to read the description before the object is loaded, hence the undefined. I tend to place variables that I will need across multiple functions in the this.properties var (p in my example) then do something like this at the beginning of every function:

var p = this.properties;
var whatever = p.whatever;
like image 165
Nuno Linhares Avatar answered Nov 07 '22 03:11

Nuno Linhares


As Nuno said, you are probably reading a property that hasn't been loaded.

However, you shouldn't be accepting the name sent by the client anyway. You should get the ID and name of the current user in your web service instead. Same goes for some of the other arguments like the date/time.

Basically, your web service should never trust the incoming data. So anything that it can figure out on its own should not even be an argument to the method, and anything that it does need should be considered malicious content and sanitized before you use it.

like image 1
Peter Kjaer Avatar answered Nov 07 '22 02:11

Peter Kjaer