I want to move all my images from one of the folder in content manager to one of the folder on the server, how to do it using C# TBB?
-
Why don't you simply do this from the Web GUI? – Frank van Puffelen Jul 11 '12 at 12:54
-
Yes it is publishing, but i want a c# code that will allow me to publish any image that i add in image folder in content manager – Priyanku Jul 11 '12 at 13:01
-
@FrankvanPuffelen WEB GUI will not publish any image whenever it is added to the folder. – Priyanku Jul 11 '12 at 13:02
2 Answers
On SDL Tridion World you can find a set of useful Template Building Blocks which contains a solution for that: http://sdltridionworld.com/community/extension_overview/useful_tbbs.aspx
See #5: Get CSS Images - Publish all the images in a particular CMS folder.
Here is a snipped of the code from that solution just to get the idea of how its done.
Filter filter = new Filter();
filter.Conditions["ItemType"] = ItemType.Component;
filter.Conditions["Recursive"] = false;
foreach (Component img in folder.GetItems(filter))
{
if (img.ComponentType == ComponentType.Multimedia)
{
string filename = Utilities.GetFilename(img.BinaryContent.Filename);
Item item = package.GetByName(filename);
bool push = true;
if (item != null)
{
Logger.Debug("An item with the same name exists in the package");
KeyValuePair<string, string> pair = new KeyValuePair<string,string>("TCMURI", img.Id.ToString());
if (item.Properties.Contains(pair))
{
Logger.Debug("An item with the same uri exists in the package, we will not push it twice to the package.");
push = false;
}
}
if(push)
{
Logger.Debug(String.Format("Pushing item {0} to the package", filename));
package.PushItem(filename, package.CreateMultimediaItem(img.Id));
}
}
}
Instead of pushing the item into the package and allowing it to be published by the Default Finish Actions, you can also call AddBinary and specify the Structure group you want it published too.
Engine.PublishingContext.RenderedItem.AddBinary(img, structureGroup);
See the TOM.NET API documentation for more details.

- 4,835
- 17
- 30
-
-
what is the problem you have with downloading it, it should be available to everybody? – Bart Koopman Jul 11 '12 at 13:10
-
-
1the download is a zip file with in there the complete c# solution, sounds like you have a firewall trying to block you from downloading useful code. I will update the answer with a code snipped explaining the basics, but you will really benefit from the entire solution. – Bart Koopman Jul 11 '12 at 13:20
-
I am using this code but i want to access all the items of my image folder and push them to server, can u help me on that. I want to access all items of any folder, can u also give me snippet for that – Priyanku Jul 11 '12 at 14:06
-
With all items you mean other than Multimedia Components? That won't be so easy, a Component can be Published but then it must be (part of) a Dynamic Component Presentation or embedded on a Page. If you are referring to all Multimedia Components in subFolders as well, that just means adding a loop around this code, this is all available in the solution which I suggested. – Bart Koopman Jul 11 '12 at 14:12
-
I want all multimedia items only. I just want to access all multimedia items in a folder and push them to server, using the code snippet above is not exactly solving my problem. – Priyanku Jul 11 '12 at 14:30
Couple of ways you can do this:
1) static publishing, that is create a structure group (i.e. the folder that will be created on the server) and create a page inside. Your page will need a metadata schema that takes a multivalve multimedia component link such that you can add images to the page's metadata. You will need to build a page template for this page that will have a TBB which gets the multimedia components from the page metadata and uses the Engine.AddBinary method to add the images into the package and be published with the page (the page output can be some dummy stuff). Note, if you have a lot of images there will be a performance impact.
2) dynamic publishing: if you have the broker, you can configure file system publishing. Then create a dynamic component template linked to your image schema. Inside use a TBB with the engine.AddBinary method for the given MM component to publish an image to a given structure group as a dynamic component presentation.

- 3,434
- 16
- 22
-
I have added the images using the default multimedia schema, if i write a TBB then how can i render the images from it? – Priyanku Jul 11 '12 at 13:12
-
-
2I'm sorry but i dont have time to code this gor you. Your best bet is to look at the code in TBB that Bart suggested as a sample. To get the fields out of the metadata schema use the ItemFields API (consult your Tridion documentation). You don't need to render anything here. Once you get hold of the image component simply use engine.AddBinary – Nickoli Roussakov Jul 11 '12 at 13:42
-
1Have a look at the code in the TBB called Get Linked Components in that same extension that Bart suggested. It provides a good sample of AddBinary at the bottom. – Nickoli Roussakov Jul 11 '12 at 13:50