I have hundreds of pages that are using the same page metadata and I would like to set an specific value in one of this fields that is a text with C# or other automatic way. Does anyone knows how to do it? Thanks in advance.
-
Do you want to set a default value, or just update all the Pages with this metadata Schema and set a specific value for one of its fields?– Bart KoopmanCommented Feb 11, 2014 at 14:28
-
The second part. I need to update all the pages that has this new page metadata field.– ITBCommented Feb 11, 2014 at 14:30
2 Answers
On Tridion 2009 you should use the TOM Interop APIs (COM) which are provided under Tridion\bin\Client\PIA. You'll need to use the Tridion.Interop.TDS.dll
and Tridion.Interop.TDSDefines.dll
.
Create a new Tridion session with code like this:
TDSE tdse = new TDSE();
If the current Windows user is a valid Tridion user, then it will just work. If it's not it's a bit more complex, you need to run under a valid Impersonation account, then call tdse.Impersonate("accountname")
.
Then you need to find all pages that may contain the value - you probably want to do something like a "Where Used" on that schema that gives you all the pages:
Schema s = (Schema) tdse.GetObject("tcm:123-99-8", EnumOpenMode.OpenModeView, Constants.URINULL, XMLReadFilter.XMLReadNull);
ListRowFilter filter = tdse.CreateListRowFilter();
filter.SetCondition("ItemType", ItemType.ItemTypePage);
XmlDocument doc = new XmlDocument();
doc.LoadXml(s.Info.GetListUsingItems(ListColumnFilter.ListID, filter);
Now you have an XML Document that contains all the IDs of all Pages that use the schema you loaded ("tcm:123-99-8" in the example above).
Next, you need to load each individual page in a loop, get the fields, update them, save the page, move on to the next. Something along these lines:
foreach (XmlNode node in doc.SelectNodes("/tcm:ListUsingItems/tcm:Item", nm))
{
Page page = tdse.GetObject(node.Attributes["ID"].Value, EnumOpenMode.OpenModeEdit, Constants.URINULL, XMLReadFilter.XMLReadData);
ItemFields metadata = page.MetadataFields;
string somevalue = metadata["FieldName"].value.ToString();
metadata["FieldName"].value.Add("New value");
metadata["FieldName"].value.RemoveAll();
page.Save(true);
if(page.Info.IsCheckedOut)
page.CheckIn();
}
I haven't tested that code, just found it lying around in an old project...
EDIT As Bart suggests, all these COM objects should be released with Marshal.ReleaseComObject(comObject);
-
When using TOM Interop, don't forget to release the System resources to prevent memory leaks, see tridion.stackexchange.com/questions/1207/… for more details. Commented Feb 11, 2014 at 15:49
Since you are on version 2009 your options are a bit limited, you would either use the Business Connector (SOAP) API, or the TOM API directly on the server.
For the TOM API you could write a ASP Page, or you can do an ASPX Page (with C#), but then you have to use the TOM Interop, and run it from the Custom Pages. I don't think I have any direct examples of how to do that, it has been more than 3 years ago since I wrote stuff like that.
Probably not what you wanted to hear, but I would suggest upgrading to SDL Tridion 2013, and then you can use the Core Service to easily write an application in C# to suit your needs. You will also find a lot more examples online on how to do such things.