How to get the schema name when we component source XML directly - tridion

I am working on creating components using core services 2011 in SDL Tridion 2011 SP1.
I just want to know, is it possible to get the schema name when I have component source XML directly in my local machine.
Consider i have ABCD.xml stored in my local machine. I want to give the this ABCD.xml as input get the schema that it is using.
Is it possible Via core services 2011, if yes please share your views on it.
Just to have a sample content xml file in my local machine. it is here.
<Content xmlns="uuid:someUUID">
<first>first filed</first>
<second>second field</second>
</Content>
Thank you.

Normally, the XML of a Component contains the reference to the Schema TcmUri and title (under node tcm:Component/tcm:Data/tcm:Schema).
However, if you load the ComponentData in the Core Service, you won't see the full Component XML exposed through the ComponentData API.
There is a way to retrieve the Schema TcmUri and title from the ComponentData. Just use ComponentData.Schema properties IdRef or Title.

You can use the following code:
XmlDocument componentXml = new XmlDocoment();
XmlDocument componentXml = new XmlDocument();
componentXml.Load("Your Xml File Path");
XmlNameTable nameTable = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nameTable);
nsmgr.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
XmlNode schemaNode = componentXml.SelectSingleNode("/tcm:Component/tcm:Data/tcm:Schema", nsmgr);
string schemaTitle = schemaNode.Attributes["xlink:title"].Value;

Related

Visual Studio treat custom extension as CSharp

I've got files with a custom extension .mcsx that are really CSharp files. I want to get code completion on these files in Visual Studio 2012, but these files are treated as plain text when I open them in VS.
I've tried to create a custom MEF extension which allows me to treat the files as having a content-type based on csharp, but I don't get autocompletion or code formatting for these documents. I might need to invoke the custom CSharp classifier for my custom content type, but I'm not sure. Can anyone help?
The custom ContentType is below:
static class ContentType
{
public const string Name = "CSScript";
[Export]
[Name(Name)]
[DisplayName("CSharp Script")]
[BaseDefinition("CSharp")]
public static ContentTypeDefinition CSharpContentType = null;
[Export]
[ContentType(Name)]
[FileExtension(".mcsx")]
public static FileExtensionToContentTypeDefinition CSharpFileExtension = null;
}
Thanks to 'Ego' on the VS forums:
You can add the custom extension file via the way below:
http://blogs.msdn.com/b/zainnab/archive/2010/08/22/using-custom-file-extension-associations-vstipenv0038.aspx
For more information about Registering a Language Service please view:
http://msdn.microsoft.com/en-us/library/vstudio/bb166421(v=vs.110).aspx
So inside of VS things are actually a bit different than the editor APIs imply -- we don't actually use content types (for the most part) to determine when to activate. Noah Richards has a great blog post that shows how to create an extension that marks another extension under an existing editor.

How to get the itemxml of a selected item in Tridion

I would like to get and display the itemxml of the selected item from the Tridion CME.
I was able to get the Itemxml from my VM server when i give the tcm id in the browser.
However, i would like to get the same information from Tridion GUI Extension.
I am able to get the selected item tcm id. Is there any way to get the itemxml using coreservice?
or is there any other way to get this?
At the moment there's no way you can get Item XML through core service. Item XML you have seen was provided to you by TCM Protocol handler that might not be there in future versions. If you want to show item XML in CME - take a look at this extention by Yoaw:
http://sdltridionworld.com/articles/sdltridion2011/tutorials/GUIextensionIn8steps.aspx
Also, keep in mind that not all properties of an item might be exposed in Xml, sometimes you have more info in Data object
Take a look at the PowerTools, it has an ItemXML viewer (written by Robert Curlette) for all items in SDL Tridion
http://code.google.com/p/tridion-2011-power-tools/wiki/ItemXML
The XML is loaded on a tab using JavaScript as follows:
ItemXmlTab.ItemXmlTab.prototype.updateView = function ItemXmlTab$updateView()
{
if (this.isSelected())
{
var xslPath = $ptUtils.expandPath("/PowerTools/Client/ItemXml/ItemXmlTab.xslt", true);
$xml.loadXsltProcessor(xslPath, function (value)
{
var xmlSource = $display.getItem().getXml();
// Filter out all spacing characters
xmlSource = xmlSource.replace(/\t|\n|\r/g, "");
var html = $xml.xsltTransform(value, $xml.getNewXmlDocument(xmlSource), null);
$dom.setOuterHTML($("#itemXml"), html);
});
}
};
You can view the source code of the extension at http://code.google.com/p/tridion-2011-power-tools/source/browse/#svn%2Ftrunk%2FPowerTools.Editor%2FPowerTools%2FClient%2FItemXml%253Fstate%253Dclosed
You can get the item XML via CoreService, but this will get you the Tridion R6 (2011) Xml format, which is not the same you would see before.
Sample code available here.
I tend to have a page "GetItemXml.aspx" on my Tcm servers that I then call with a Uri as a parameter, and then this page would return the Item Xml.
Article written by Yoav Niran (Url in the post of user978511) is perfect for your requirement.
if you are still facing any issue and in hurry to get it working just perform below steps -
1- Download the extension.
2- Apply the steps 7 and 8 of this article to configure this extension.

push external multimedia file in to package at tridion publish time

When we publish some page/dynamic component from tridion is it possible to add some external multimedia file/content(ex:jpg image) in to current executing/rendering package at publish time.So that final transportation package has this binary file present along with original published content?
Is this achivable using customization of tridion renderer/resolver?If yes please provide some inputs.
*Note:*The binary content that needs to be pushed in to package at publish time is not present as multimedia component in tridion, it is located at other file location outside tridion CMS.Instead we have some stub multimedia component being used inside published component/page which has some dummy image. we plan to replace the stub image with original image at publish(rendering/resolving) time.
Since we have huge bulk of binary content stored in DAM tool we dont want that data to be recreated as multimedia component in tridion, insted we want to use that data by querying DAM tool and attach it in to tridion package with some logical referencesplanning to maintain one to one mapping between stub multimedia comp tcmid to original content in some mapping DB for reference).
Please let us know if any solution is there to attach external binary content to package at publish time.
The best - and easiest way - is to use the mechanism provided by Tridion out-of-the-box for this. Create a new multimedia component, select "External" in the resource type drop-down, and type the URL to the object. As long as you can address it with a URL, it will work exactly as you want (item will be added to package and sent to delivery server).
If this is not good enough for you, then yes, you can add it to the package yourself. I've done this in the past with code somewhat like this:
FileInfo file = // Weird logic to get a FileInfo object from external system
Item item = package.GetItem("My original Item");
item.SetAsStream(file.OpenRead());
This replaced the content of my original component with the actual file I wanted. This will work for you IF the original component is also a multimedia component. If it's not, just create a new item with your own name, etc. If possible, do use the out-of-the-box process instead.
PS: FileInfo Class.
As Nuno suggested the best way is to use multimedia component with 'External' resource type. You may not need to create these manually, you can automate using core services or API programs.
Another way I used before to create zip file at run time and add same to package with following code. Hope it may help.
using (MemoryStream ms = new MemoryStream())
{
zip.Save(ms);
downloadAllInOneURL = String.Format("ZipAsset{0}.zip", uniqueZipID);
downloadAllInOneURL = m_Engine.PublishingContext.RenderedItem.AddBinary(ms, downloadAllInOneURL, "", "application/zip").Url;
downloadAllInOneSize = getSize(ms.Length);
}

ASP.NET creating resources at runtime

I'm developing an ASP.NET webapp that has a multilanguage feature allowing the webmaster to create new languages at runtime.
The approach that I was thinking is the following:
The user selects one available (not created) language.
When the user confirms, the application automatically copies a set of existing resources, replacing the filename with the new culture. For example: default.aspx.en-us.resx to default.aspx.es-ar.resx.
The user edits the recently created resources.
Currently I'm having troubles with step number 2. I've achieved to copy the resources, but then these new resources are ignored. I think that this happens because the new resources are not included in the running assembly, and therefore are being ignored.
When I test the following code in my local project, I would have to manually add the new resources to the solution and then recompile to make it work.
Does anyone know how to make this work?
This is the code of the mentioned copy.
string _dir = path_ + "App_LocalResources\\\\";
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir + _file.Name.Replace("en-us", idioma_.Cultura));
}
string _dir2 = path_ + "App_GlobalResources\\\\";
_dirInfo = new DirectoryInfo(_dir2);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir2 + _file.Name.Replace("en-us", idioma_.Cultura));
}
Thank you very much.
Creating or editing Resource files is not possible the same way as reading data.
In order to create or edit a resource file, you should do it the same way you create or edit XML files because resource files have with a specific structured XML elements.
Maybe this article will help you...

Get items in a folder using Core Service

Using Tridion Core Service, how can I get all the items in a folder?
You need a filter (OrganizationalItemItemsFilterData).
Then you need to call client.GetList(folderId, filter) to get the list of items in that folder.
OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData();
foreach (XElement element in client.GetListXml("tcm:3-640-2", filter).Nodes())
{
Console.WriteLine(element.Attribute("ID").Value);
}
A good example on how to use the Core Service which contains all sorts of read operations (like reading a single item and getting a list of items) is shown in the following eXtension on SDL Tridion World: Item Selector Custom URL
The download on SDL Tridion World comes with complete source but this can also be found directly on Google Code where it is hosted as an open source solution. The actual Core Service code can be found in the UserControls/TridionTreeView.ascx.cs Class.

Resources