Scripts to Find Pages by Page Template, Update Page Metadata & Publish

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 –

  1. Update the app.config file with proper version core service client.
  2. 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.

Anonymous
  • Hi Sayantan,

    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);

    foreach (var item in pages.Elements())

    {

    string tcmID = item.Attribute("ID").Value;

    \\ write code further

    }

    }

    With reference to above code, client.GetListXML or client.GetList retrieve pages with only TCMID details and all other attribute returning null.

    Here. my problem is with the help of Page template, its return more than 5000 pages and I need to loop all the way 5000 pages. But I want only pages which are created or updated just one day before so its avoid me to loop all the pages. If we get somehow version info in the return list, we can linq list as require.

    Please guide me how we can add any filter in UsingItemsFilterData or how we get version info in the return list. Appreciate you help.

    Regards,

    Prashant S.