How to get the user who initiated a publish action in a SDL Tridion C# TBB - tridion

From a C# TBB used by a Modular Page Template in SDL Tridion 2011, is it possible to access the User object who initiated the Publishing action?
Looking at the TOM.NET 6 Programmers Reference Guide, it seems that the property I need is the Creator property of the PublicationTransaction object, but I can’t find a way to access that from a C# TBB, I don’t see an obvious way to get the current PublicationTransaction from the engine or package objects, and I can only find a way to get a list of PublicationTransaction objects using the PublishEngine object.
Any advice would be greatly appreciated.

Have a look at these two blog posts from Mihai Cadariu:
How to look up the current Publish Transaction (based on a trick from Chris Summers)
Create a new Publish Transaction based on an existing one
With those two you should be able to find what you need.
The basic function you need in your TBB is this:
public PublishTransaction GetPublishTransaction(Engine engine)
{
String binaryPath = engine.PublishingContext.PublishInstruction.
RenderInstruction.BinaryStoragePath;
Regex tcmRegex = new Regex(#"tcm_\d+-\d+-66560");
Match match = tcmRegex.Match(binaryPath);
if (match.Success)
{
String transactionId = match.Value.Replace('_', ':');
TcmUri transactionUri = new TcmUri(transactionId);
return new PublishTransaction(transactionUri, engine.GetSession());
}
return null;
}
It might also be worth noting that the property engine.PublishingContext.PublishInstruction.RenderInstruction.BinaryStoragePath will return something different when rendering the coder in PreviewMode or from the Template Builder compared to when the code is running in the Publisher. To see the PublishTransaction URI in the BinaryStoragePath, you must attach your Visual Studio TBB Debug Project to the TcmPublisher.exe process in order for there to actually be a PublishTransaction object present, otherwise the BinaryStoragePath will just contain a generic path like ../preview.

Related

Fetch a component by its tcm id using the core services

I need to fetch a component based on its tcm id using the core services. Please help me which function of core service I can use along with some sample code if possible as well.
Thanks in advance!!
Check the section in the documentation, which you can find here
A basic sample (out of my head, untested ;) ):
ComponentData component = (ComponentData)client.Read(yourComponentId, new ReadOptions());
string title = component.Title;
XElement content = XElement.Parse(component.Content);
The Read method will get you an IdentifiableObjectData object, which you can cast to the needed type if you're sure about what you're supposed to be getting back.
The ReadOptions object will instruct the CoreService how to load the item, for instance, with all Keyword URI's loaded as well using LoadFlags.KeywordXlinks

List of Publication Targets using Tridion Core Service?

How would I get a list of Publication Targets with the Tridion Core Service? I see this code sample to get the Target Info, but cannot find a way to get the list from the Core Service. Maybe it is part of the Publication object?
var pubtarget = (PublicationTargetData)client.Read("tcm:0-21-65537", readoption);
Also, if there is a way to get this via the Anguilla JavaScript client it would also be cool.
var filter = new PublicationTargetsFilterData();
var pubTargets = ClientAdmin.GetSystemWideList(filter);
var pubTargetsXml = ClientAdmin.GetSystemWideListXml(filter);
You can set additional filter properties on filter object
user978511 already answered how to do it using the Core Service, so let me just answer how you can do it using Anguilla:
var system = $models.getItem($const.TCMROOT);
var list = system.getListPublicationTargets();
From then on, it's a normal list -- so you'll want to check isLoaded() and call load() if it returns false (hooking into the "load"/"loadfailed" events for the callback).
The Publication Targets are available either as XML through the getXml() method or as an array through the getItems() method (which returns an array of model items -- so again, you can check isLoaded() on those, etc.)

generating xml containg fields of a component in sdl tridion

How to generate xml file which contain all the fields of a component in SDl Tridion?
I have to do this by writing C# code.
I am using the following code:
public class stockcompany : ITemplate
{
private Engine engine;
private Package package;
public void Transform(Engine engine, Package package)
{
this.engine = engine;
this.package = package;
StringBuilder sb = new StringBuilder();
Component component = engine.GetObject(package.GetValue("Component.ID")) as Component;
ItemFields componentfields = new ItemFields(component.Content, component.Schema);
ItemFields compometa = new ItemFields(component.Metadata, component.Schema);
if (compometa.Contains("check"))
{
}
if (componentfields.Contains("companyname"))
{
string company = string.Empty;
company = componentfields["companyname"].ToString();
package.PushItem("xml", package.CreateHtmlItem(company.ToString()));
XDocument myxml = new XDocument (new XDeclaration ("1.0","UTF-8",null ),new XElement ("companies",new XElement ("company",xml)));
}
You are probably re-inventing the wheel here.
Tridion items are XML. The .Content and .Metadata properties of a component return XML, would this be enough?
DD4T uses templates that publish XML to the delivery side, might be worth checking it: http://code.google.com/p/dynamic-delivery-4-tridion/
Given that DD4T is open sourced, you may want to check how they do it as an example.
Another approach to generating XML is to use an XmlSerializer. It works as follows:
Create a POCO
Fill it with values from the component
Use XmlSerializer to deserialize it into an XML string
Store the XML in the Output variable of the package
This is how DD4T does it. As Nuno said, it is worth while to check it out (see his answer).
I've typically resorted to XSLT to get source XML via a component template, but we can definitely do the same with C#.
[TcmTemplateTitle("Show XML Guts")]
public class ShowXmlGuts : ITemplate
{
public void Transform(Engine engine, Package package)
{
Item contentItem = package.GetByType(ContentType.Component);
Component component = engine.GetObject(contentItem.GetAsSource().GetValue("ID")) as Component;
package.PushItem("componentSource", package.CreateHtmlItem(component.Content.OuterXml));
}
}
Pick-and-Choose Fields
If at all possible, I'd start with an intermediate XML format that's not a one-to-one mapping to component source.
We're meant to get and transform fields with the appropriate APIs. Relying on the source component format could be problematic in your next major change, which could include:
schema changes and new keywords
presentation/rendering side or CMS changes
"unexpected" content (rich text, special characters, tcm references, etc)
Alternatives to C#
In terms of technology:
XSLT is great for "XML generation," even if done in a C# template building block.
The XSLT Mediator would a good choice, if allowed in your environment
You could create XML with DWT, simplifying field selection; however, it's easier to create invalid XML this way (XSLT doesn't validate your XML either, but it's slightly harder to break node nesting)
If possible, update your question with the output XML you're trying to achieve or start a new question to get from your raw component XML to desired output.
Your code isn't very clear and contains a lot of problems, but generally you seem to be on the correct track.
1) You need to cast your
componentfields["companyname"].ToString();
I suspect you're working with a TextField here so cast it to a TextField object and then use the objects .value property
2) Here is where you push the value into the package, this will contain whatever you got from your 'companyname' field, it could be xml, it may not be:
package.PushItem("xml", package.CreateHtmlItem(company.ToString()));
..But I think with this information you can find your way to what you need.
Good luck!

how do I use ClassifiedItemsFilterData for folders in Tridion 2011 CoreService?

I'm rewriting a .NET backend application so that it uses the Tridion 2011 CoreService.
There's one part where it will get a folder in Tridion that uses a particular keyword.
In the current setup, this is done by calling the method 'GetListClassifiedItems' on the keyword itself, but how am I suppose to do this using the CoreService?
There is a ClassifiedItemsFilterData available in the CoreService API, but how do I use it?
I've tried this piece of code:
ClassifiedItemsFilterData filter = new ClassifiedItemsFilterData()
{
ItemTypes = new ItemType[] { ItemType.Folder }
};
XElement list = client.GetListXml("tcm:113-363331-1024", filter);
but it will only return the keyword itself, with URI tcm:113-363331-1024, and not the folders that have been classified with it.
If I add the component ItemType to the filter too, I will get all components that have been classified with this keywordk, but still not that folder.
How do I get the folder too?
When I run a similar test, I do get both Folders and Components in my result
var filter = new ClassifiedItemsFilterData();
filter.ItemTypes = new ItemType[] {ItemType.Folder};
var transactions = client.GetListXml("tcm:1-134-1024", filter);
Console.WriteLine(transactions.ToString());
I added a Keyword field to a Metadata Schema and associate that with the Folder. Is that the same way you did it?
When I remove the item types filter from the code above, I get all Components and Folders classified against that Keyword, but I do not get the Keyword itself. These are all exactly how I'd expect a ClassifiedItemsFilterData to work, so we really should focus on what is different in your scenario.

JSON string to list or other usable format in asp.net 2.0

I have one JSON that is coming in a string format. I need to store it in a key-pair value or something like that. I am using asp.net 2.0 and can not use 3rd party DLL like Newtonsoft.Json.dll. I guess last option will be to use regular expression.
Can anybody please help me in this?
If you go to http://www.json.org/ and look towards the bottom of the page there are dozens of json libraries most of them open source, I believe they list 8 for C#. If you can not reference one of these libraries, I think your best bet would be to find one with a permissive license and simply add the code to your project.
Another idea is to look at the diagrams, grammer, and syntax at http://www.json.org/ and just write your own parser, but regex is NOT the way to do it. If you dont know how to write a parser you could look at one of the open source json libraries or start with something less complicated like a good CSV parser, here is a paper that looks pretty good: http://www.boyet.com/Articles/CsvParser.html
It is possible to serialize JSON using JScript in C# into key/value pairs. You need to add a few references to your project. They're part of the .NET framework, you just need to add the references to your project. You'll need:
Microsoft.JSript
Microsoft.Vsa
First, the usings at the top of your class:
using Microsoft.JScript;
using Microsoft.JScript.Vsa;
Then the Engine that will execute the script needs to be initialized somewhere in your 'Page' code-behind:
VsaEngine Engine = VsaEngine.CreateEngine();
Then you just create this method and call it by passing in your JSON object:
object EvalJScript(string JScript)
{
object result = null;
try
{
result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine);
}
catch (Exception ex)
{
return ex.Message;
}
return result;
}
The type of object returned (if JSON is passed in) is a 'JSObject'. You can access its values as key/value pairs. Read the MSDN documentation for more details on this object.
Here's an example of using the code:
string json = "({Name:\"Dan\",Occupation:\"Developer\"})";
JSObject o = EvalJScript(json) as JSObject;
string name = o["Name"] as string; // Value of 'name' will be 'Dan'
Could you use JScript.NET?
If so, should be easy enough with eval() - then just loop through the objects returned and translate into KeyValuePair's or whatever
You will need to use jscript.net as the code behind language, but other pages of your site should be fine to stay as c# if thats what you prefer.
As mentioned in previous comment, you will need to be aware of the security aspects and risks - only use eval if you trust the JSON you're parsing!

Resources