How to get username and description from its identifier in SDL Tridion Anguilla framework

I wrote a GUI extension for the SDL Tridion 2011 SP1. The GUI consists of an additional ribbon button and an event handler that runs when the 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 is as follows:

 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 works fine except that the commentername variable is always undefined. Is there a better approach for getting a username and description?

Also, does anyone know if the value returned by eventitem.source.getCreatorId() is really a Reviser or actually the person who created the element?

Thanks in advance

+6
tridion tridion-2011
source share
2 answers

I usually follow this approach in Anguilla:

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

It all comes down to the following:

 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(); } 

Then you call your webservice model from the event handler, as you are sure that the object will be loaded by then.

In your current code, you are probably trying to read the description before loading the object, hence undefined. I tend to put the variables that I will need for several functions in this.properties var ( p in my example), then do something like this at the beginning of each function:

 var p = this.properties; var whatever = p.whatever; 
+8
source share

As Nuno said, you are probably reading a property that has not been loaded.

However, you should not accept the name sent by the client in any way. Instead, you should get the identifier and name of the current user in your web service. The same goes for some other arguments, such as date / time.

Basically, your web service should never trust incoming data. Thus, everything that he can understand on his own should not even be an argument regarding the method, and everything that he needs should be considered malicious content and sanitized before you use it.

+1
source share

All Articles