Skip to Content
Technical Articles

Consuming Service Metadata with SAP Marketing Cloud SDK

In this blog post, you will learn how to consume service metadata of SAP Marketing Cloud using the SAP Marketing Cloud SDK. This blog will explain how you can read:

  • Properties and annotations from service metadata
  • Custom fields from service metadata

Prerequisites

To successfully execute this tutorial, you should have:

  • A basic programming background with Java Development Kit.
  • Basic understanding of OData metadata properties.
  • Gone through our Getting Started Tutorial.

How to Read Properties and Annotations

A typical OData metadata consists of many fields, and each field in-turn contains multiple properties. In this example, we illustrate how you can use the SAP namespace properties like sap:label, sap:creatable, sap:updatable and so on, that is used in the campaigns API.

To read values of these properties, you can use ServiceMetadataProvider class as follows:

ServiceMetadataProvider.getInstance("{servicePath}", "{destinationName}")
.getEntityMetaDataForEntityType("{entitySet}").getPropertyAnnoationsMap();

Use the code snippet below to fetch all the properties of a Campaign entity:

ServiceMetadataProvider.getInstance("/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV", "mkt")
.getEntityMetaDataForEntityType("Campaigns").getPropertyAnnoationsMap();

The response is as follows:

{
   MarketingArea: [
    {
      namespace: "http://www.sap.com/Protocols/SAPData",
      prefix: "sap",
      name: "label",
      text: "Marketing Area ID"
    },
    {
      namespace: "http://www.sap.com/Protocols/SAPData",
      prefix: "sap",
      name: "updatable",
      text: "false"
    }
    ...
  ],
  ...
}

From the example, we understand that the MarketingArea is not an updatable property.

How to Read Custom Fields

Based on business needs in SAP Marketing Cloud, it’s often required to add custom fields to customize applications and their UIs, reports, and so on. You can add custom fields to various business contexts such as campaigns, contacts, offers, etc. This section illustrates how you can retrieve and view the list of custom fields that you have created for your business context.

Let’s consider an example that you have created a custom field with label Campaign_Owner. In the service metadata, the name of this custom field appears as YY1_Campaign_Owner_MIN.

Here’s a snippet of the metadata:

<Property Name="YY1_Campaign_Owner_MIN" Type="Edm.String" MaxLength="20" sap:field-control="YY1_Campaign_Owner_MINF" sap:label="Campaign_Owner" sap:filterable="false" sap:is-extension-field="true"/>

Let’s say you now want to present a list of custom fields that were created for a specific business context. To identify custom fields, you first need to read the service metadata and search for the annotation sap:is-extension-field=”true”. Once you have identified the custom fields, you can then read its labels using the annotation sap:label. In our example, this will retrieve the text Campaign_Owner.

If you want to fetch all the custom fields of an entity set, for example Campaigns, you can use one of the following code snippets:

MarketingServiceFactory.getCampaignService("mkt_ba")
.getDefaultCampaignService().getAllCampaign().top(1).skip(0)
.execute(new ErpConfigContext("mkt_ba")).get(0).getCustomFieldNames();

MarketingServiceFactory.getCampaignService("mkt_ba")
.getDefaultCampaignService().getCampaignByKey(UUID.fromString("fa163ea2-ecd1-1ee9-85d1-07e581191350"))
.execute(new ErpConfigContext("mkt_ba")).getCustomFieldNames();

The response is as follows:

[
  "YY1_Campaign_Owner_MIN",
  "YY1_Campaign_Owner_MINF",
  ...
]

If you want to fetch custom fields with its values, you can use following code snippet:

MarketingServiceFactory.getCampaignService("mkt_ba")
.getDefaultCampaignService().getCampaignByKey(UUID.fromString("fa163ea2-ecd1-1ee9-85d1-07e581191350"))
.execute(new ErpConfigContext("mkt_ba")).getCustomFields();

The response is as follows:

{
  YY1_Campaign_Owner_MIN: "John Smith",
  YY1_Campaign_Owner_MINF: 3,
  ...
}

You have now learnt how to consume the service metadata using SAP Marketing Cloud SDK. Go ahead and give it a try!

Be the first to leave a comment
You must be Logged on to comment or reply to a post.