Posts Tagged ‘components’
Get Components from specific folder
In my earlier Tridion code sample, we have seen about retrieving components in XML format. In some cases, we may need to have component objects instead of XML string.
Below code snippet will help you to retrieve component objects from a specific folder.
private void GetComponents(string publicationURI, string folderURI) { //initialize TDSE object TDSE tObj = new TDSE(); //get folder object from where the components have to be retrieved Folder folder = (Folder)tObj.GetObject(folderURI, EnumOpenMode.OpenModeEdit, publicationURI, XMLReadFilter.XMLReadAll); //get all components from folder TDSItems components = folder.GetItems(ItemType.ItemTypeComponent); //navigate through all components foreach(Component comp in components) { //do something here. Console.WriteLine(comp.Title); } }
Retrieving all Components (that are based on a certain Schema) in XML
I had this code snippet in scripting but presenting it here in C# format. When I was working on a custom migration tool, this was very handy.
The below code snippet will be useful if you want to retrieve specific type of an objects from TOM layer.
To test it clearly, let’s try a sample piece of code.
TDSE is the basic object should be initialized first by which you can access any objects from Tridion.
TDSE tdse = new TDSE(); ListRowFilter rowFilter = tdse.CreateListRowFilter(); string result = tdse.GetListItems(ListColumnFilter.XMLListID, rowFilter);
GetListItems() returns TcmUri of all objects in XML format. So the output variable result has the following xml data.
<?xml version="1.0" ?> <tcm:ListItems xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ID="tcm:89-3-2" Managed="10682"> <tcm:Item ID="tcm:89-145-16" /> <tcm:Item ID="tcm:89-146-16" /> <tcm:Item ID="tcm:89-147-16" /> <tcm:Item ID="tcm:89-148-16" /> <tcm:Item ID="tcm:89-8756-8" /> <tcm:Item ID="tcm:89-6545" /> <tcm:Item ID="tcm:89-34" /> <tcm:Item ID="tcm:89-2332" /> </tcm:ListItems>
Now I’m adding few conditions to retrieve only component objects.
rowFilter = tdse.CreateListRowFilter();
rowFilter.SetCondition("ItemType", ItemType.ItemTypeComponent);
rowFilter.SetCondition("LockUser", tdse.User);
result = tdse.GetListItems(ListColumnFilter.XMLListID, rowFilter);
Finally we will get the following output in XML format.
<?xml version="1.0" ?> <tcm:ListItems xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ID="tcm:89-3-2" Managed="10682"> <tcm:Item ID="tcm:89-145-16" /> <tcm:Item ID="tcm:89-146-16" /> <tcm:Item ID="tcm:89-147-16" /> <tcm:Item ID="tcm:89-148-16" /> </tcm:ListItems>
Now you can add one more condition to retrieve the components based on specific schema.
rowFilter.SetCondition("BasedOnSchema", schemaUri);
