Fetch a component by its tcm id using the core services - tridion

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

Related

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.)

ActivityBuilder.Implementation doesn't include NamespacesForImplementation when serialized

I'm having a problem serializing to xaml a WorkflowService that includes an ActivityBuilder for its Body parameter. The ActivityBuilder contains C# expressions and the serialized WorkflowService doesn't activate because some C# expressions refer to custom types. The actual error is "Type 'SomeTypeIPassToAnArgument' is not defined".
My ActivityBuilder is created from a xaml file which includes a TextExpression.NamespacesForImplementation element. This is missing when the WorkflowService is serialized and it seems this is the cause of the problem.
I found that if I serialize the ActivityBuilder itself, the namespaces are included, but using ActivityBuilder.Implementation fails. Unfortunately, when I build the WorkflowService to serialize I have to use ActivityBuilder.Implementation as its Body parameter. I tried using the TextExpression.SetNamespacesForImplementation method to ensure these were attached to the implementation but that didn't help.
The following snippet shows what I'm trying to do and where it's failing.
var loadedXaml = File.ReadAllText(#"C:\workflow.xaml");
Assert.IsTrue(loadedXaml.Contains("NamespacesForImplementation"));
var xamlReader = new XamlXmlReader(new StringReader(loadedXaml));
var builderReader = ActivityXamlServices.CreateBuilderReader(xamlReader);
var builder = XamlServices.Load(builderReader) as ActivityBuilder;
// namespaces are available in the builder itself
Assert.AreNotEqual(0, TextExpression.GetNamespacesForImplementation(builder).Count);
// when saving, the assert fails because the namespaces aren't serialized
var savedXaml = XamlServices.Save(builder.Implementation);
Assert.IsTrue(savedXaml.Contains("NamespacesForImplementation"));
Any idea why this isn't working, or if there's an alternative approach I could use that would work? Ultimately my aim is to be able to load workflows from xaml and incorporate them in a serialized WorkflowService that I can host in an MVC app using a VirtualPathProvider.
Thanks!
You can't use ActivityBuilder as the body of a WorkflowService, ActivityBuilder is just intended to manipulate an Activity at design time. To convert your ActivityBuilder to an Activity, use ActivityXamlServices.CreateBuilderWriter.
You can leverage XamlServices.Transform to pipe a XamlReader over your ActivityBuilder to the writer you create in ActivityXamlServices.CreateBuilderWriter.

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

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.

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