Let’s discuss some new pointers about CoreService API (Powerful API for Web/Tridion Developer).
Recently I have got a chance to work in a Migration project where I have received few requirements which are not very common. So I have planned to prepare some scripts and share it with everyone.
First scenario was customer wants to update the fields of the page metadata based on page template and republish those pages. For this type of scenario this script is very useful.
Today I will describe how easily we can add or update field of the page metadata in one go and publish all the pages using Coreservice API.
Steps:
- Find all the pages based on page template.
- Add page metadata schema.
- Update the metadata.
- Re-Publish the page.
Now let me describe one by one –
Find the list of Pages based on Page Template TcmUri –
private static XElement GetAllPagesByPageTemplate(string templateTcmUri)
{
UsingItemsFilterData filter = new UsingItemsFilterData();
filter.ItemTypes = new[] { ItemType.Page };
filter.IncludedVersions = VersionCondition.OnlyLatestVersions;
XElement pages = client.GetListXml(templateTcmUri, filter);
return pages;
}
Add page metadata schema-
foreach (var item in pages.Elements())
{
string tcmID = item.Attribute("ID").Value;
PageData pageData = client.Read(tcmID, null) as PageData;
//Pass the page data, target value and the metadata schema tcmuri
UpdatePageData(pageData, targetValue, metadataSchemaTcmUri);
}
private static void UpdatePageData(PageData pageData, string targetValue, string metadataSchemaTcmUri)
{
pageData.MetadataSchema.IdRef = metadataSchemaUri;
}
Update the metadata.
Let’s take an example of Redirect Page Metadata Schema. That schema has redirect field with an embedded schema, where we need to update the value of externalLink field –
SchemaData schema = (SchemaData)client.Read(metadataSchemaUri, new ReadOptions());
XNamespace mns = schema.NamespaceUri;
XNamespace xlink = "http://www.w3.org/1999/xlink";
var metaData = new XElement(mns + "Metadata");
var redirect = new XElement(mns + "redirect");
var externalLink = new XElement(mns + "externalLink");
externalLink.SetAttributeValue(xlink + "type", "simple");
externalLink.SetAttributeValue(xlink + "href", targetValue);
redirect.Add(externalLink);
metaData.Add(redirect);
pageData.Metadata = metaData.ToString();
client.Update(pageData, new ReadOptions());
Now the last step –
Re-Publish that page.
var pubData = new PublishInstructionData
{
ResolveInstruction = new ResolveInstructionData()
{
IncludeChildPublications = IsPublishingFromChild,
Purpose = ResolvePurpose.RePublish
},
RenderInstruction = new RenderInstructionData()
};
client.Publish(new[] { pageData.Id }, pubData, new[] { publishingTarget }, PublishPriority.Low, new ReadOptions());
I have already prepare the draft version of the script. All you need to do –
- Update the app.config file with proper version core service client.
- Modify the code as per your requirement.
Here is the source code of the script where the requirement was to read one field from existing component presentation of the page which are using “Page – Redirect” templates and update that value in external link field of page metadata schema.