# Document XMP metadata

**URL:** <https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486>\
**Category:** Photoshop\
**Created:** [August 10, 2021, 7:12pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486 "2021-08-10T19:12:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavideBarranca](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/davidebarranca/32/1523_2.png) [@DavideBarranca](https://forums.creativeclouddeveloper.com/u/DavideBarranca)\
**Post date:** [August 10, 2021, 7:12pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/1 "2021-08-10T19:12:09Z")

</div>

Hi,  
I’m trying to get, set and delete a namespaced key, value pair on the XMP at the document’s level. I’ve borrowed @simonhenke code from [this post](http://forums.creativeclouddeveloper.com/t/photoshop-uxp-plugin-xmpmetadata/2166) on Layer XMP, and edited a bit to transform it on Document XMP. I’m using `xmldom`, and I’ve got this:

```auto
const bp = require("photoshop").action.batchPlay
const { DOMParser, XMLSerializer, DOMImplementation } = require('xmldom');

// Get all the Doc XMP
const getDocumentXMP = () => {
  return bp([{
    _obj: "get",
    _target: {
      _ref: [
        { _property: "XMPMetadataAsUTF8" },
        { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
      ]
    }
  }], { synchronousExecution: true })[0].XMPMetadataAsUTF8
}

// Set all the Doc XMP
const setDocumentXMP = (xmpString) => {
  bp([{
    _obj: "set",
    _target: [
      { _ref: "property", _property: "XMPMetadataAsUTF8" },
      { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
    ],
    to: {
      _obj: "document",
      XMPMetadataAsUTF8: xmpString
    }
  }], {});
}

// Get key of namespace
export const getDocumentMetadata = ({ key, namespace }) => {
  const xmpString = getDocumentXMP();
  if (xmpString) {
    const xmlDoc = new DOMParser().parseFromString(xmpString, 'text/xml');
    const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] : xmlDoc.getElementsByTagName(key)[0]
    if (node) {
      return node.textContent
    }
  }
}

// set key, value of namespace
export const setDocumentMetadata = ({ key, value, namespace }) => {
  const xmpString = getDocumentXMP();
  const xmlDoc = xmpString ? new DOMParser().parseFromString(xmpString, 'text/xml') : new DOMImplementation().createDocument(namespace, key, null);
  const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] || xmlDoc.createElementNS(namespace, key) :
    xmlDoc.getElementsByTagName(key)[0] || xmlDoc.createElement(key);
  node.textContent = value;
  xmlDoc.appendChild(node);
  console.log(xmlDoc); // the document does contain the new node
  const newXmpString = new XMLSerializer().serializeToString(xmlDoc); // <= something wrong here...?
  console.log("oldXmpString", xmpString); // Old XMP string
  console.log("newXmpString", newXmpString); // New XMP string... they're equal :-/
  setDocumentXMP(newXmpString)
}

// delete key of namespace
export const deleteDocumentMetadata = ({ key, namespace }) => {
  const xmpString = getDocumentXMP();
  const xmlDoc = new DOMParser().parseFromString(xmpString, 'text/xml');
  const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] : xmlDoc.getElementsByTagName(key)[0]
  if (node) {
    xmlDoc.removeChild(node)
    setDocumentXMP(new XMLSerializer().serializeToString(xmlDoc))
  }
}

// TEST
setDocumentMetadata({key: "someKey", value: "someValue", namespace: "undavide:"})
var newXMP = getDocumentMetadata({key: "myKey", namespace: "undavide:"});
console.log({newXMP}) // undefined

```

Which in theory should work, in practice it doesn’t. The test sets a namespaced key, value pair, then retrieves it but it gets through as `undefined`.

I think I’ve been able to pinpoint the problem in the `setDocumentMetadata()` function. The new `node` is successfully created and also appended to the `xmlDoc`, as the log in the Console shows. For some reason, though, the `XMLSerializer()` produces a `newXmpString` that is perfectly equal to the original one, that is to say without the new node. I’m logging them both. From that point onwards, the subsequent call to `setDocumentXMP()` is pointless, because the new xml string lacks the additional node.

Can your fresh pairs of eyes spot any issue in the above code?  
Thanks!!

Davide

---

<div class="post-metadata">

**Author:** ![DavideBarranca](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/davidebarranca/32/1523_2.png) [@DavideBarranca](https://forums.creativeclouddeveloper.com/u/DavideBarranca)\
**Post date:** [August 11, 2021, 10:13am UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/2 "2021-08-11T10:13:30Z")

</div>

Further inspection shows that I was wrong in saying that the two strings are equal, the console logs a long series of carriage returns that I thought were pointless:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/f/fd2fcaf0e9e33584375143673e40660dfc5e074f.png)

Instead, at the very end there is the custom namespaced tag:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/4/4259c0da00174669138291b307453ffd33fc8876.png)

Although it is **outside** the `<?xpacket end="w"?>` tag, i.e. not reachable when you later try to get it 😐

It still doesn’t work, although I’ve a clearer idea on the reason why…

---

<div class="post-metadata">

**Author:** ![DavideBarranca](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/davidebarranca/32/1523_2.png) [@DavideBarranca](https://forums.creativeclouddeveloper.com/u/DavideBarranca)\
**Post date:** [August 11, 2021, 7:50pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/3 "2021-08-11T19:50:16Z")

</div>

Solved 🍷  
I had to use a different XML library called `xmlbuilder2`.

```auto
const { convert } = require('xmlbuilder2');

// GETTER
// -------------------------------------------------
// <rdf:Description xmlns:undavide="http://davidebarranca.com/notes/" etc...>
// <undavide:Notes>someValue</undavide:Notes>
// -------------------------------------------------
// key: Notes
// prefix: undavide
// namespace: http://davidebarranca.com/notes/

export const getDocumentMetadata = ({ key, prefix = 'undavide', namespace = 'http://davidebarranca.com/notes/' }) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  return obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] || "nothing";
}

// SETTER
// -------------------------------------------------
// <rdf:Description xmlns:undavide="http://davidebarranca.com/notes/" etc...>
// <undavide:Notes>someValue</undavide:Notes>
// -------------------------------------------------
// key: Notes
// value: someValue
// prefix: undavide
// namespace: http://davidebarranca.com/notes/

export const setDocumentMetadata = async ({ key, value, prefix = 'undavide', namespace = 'http://davidebarranca.com/notes/' }) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`@xmlns:${prefix}`] = namespace;
  obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] = value;
  const newXmpString = convert(obj, {format: "xml"})
  await setDocumentXMP(newXmpString);
}

```

Might need a bit of extra testing for edge cases. What fun XML is, who would have thought 🙂  
If you run the prerelease version of PS see my post in the forums there for details on the BatchPlay call.

Davide

---

<div class="post-metadata">

**Author:** ![AOK](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aok/32/2360_2.png) [@AOK](https://forums.creativeclouddeveloper.com/u/AOK)\
**Post date:** [September 21, 2021, 5:48pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/4 "2021-09-21T17:48:45Z")

</div>

Hi Davide,

Do you have any resources for xmp manipulation similar to what was possible with extendscript?  
I tried incorporating some of the code above but couldn’t get it working in my scenario.

I have created several CEP panels and scripts that display and interact with custom XMP metadata with in Photoshop. I’ve searched this forum and I have not found an easy way to do this via UXP plug ins.  
I used to be able to retrieve this metadata easily and append it in extendscript:

var docRef = app.activeDocument;  
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);  
var customField = xmp.getProperty(“[http://my.custom.namespace/”](http://my.custom.namespace/%E2%80%9D), “Custom”);  
xmp.setProperty(“[http://my.custom.namespace/”](http://my.custom.namespace/%E2%80%9D), “Custom”, “Test”);  
docRef.xmpMetadata.rawData = xmp.serialize(XMPConst.SERIALIZE\_USE\_COMPACT\_FORMAT);

Is there an easy way to do this in the UXP API or BatchPlay?

Thanks

---

<div class="post-metadata">

**Author:** ![erpang](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/erpang/32/1994_2.png) [@erpang](https://forums.creativeclouddeveloper.com/u/erpang)\
**Post date:** [April 7, 2022, 2:54am UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/5 "2022-04-07T02:54:44Z")

</div>

Hello, I modified the XMP data of the document according to your method, but it was not successfully modified 😩

---

<div class="post-metadata">

**Author:** ![BillCipher](https://avatars.discourse-cdn.com/v4/letter/b/34f0e0/32.png) [@BillCipher](https://forums.creativeclouddeveloper.com/u/BillCipher)\
**Post date:** [August 15, 2023, 8:33am UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/6 "2023-08-15T08:33:04Z")

</div>

Your solution work for me until set the metadata with the batchplay code. It didn’t edit the metadata and didn’t return any error. Do you have any ideas to why? I change the code a bit

```auto
const bp = require("photoshop").action.batchPlay;
const getDocumentXMP = () => {
  return bp([{
    _obj: "get",
    _target: {
      _ref: [
        { _property: "XMPMetadataAsUTF8" },
        { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
      ]
    }
  }], { synchronousExecution: true })[0].XMPMetadataAsUTF8
}
const setDocumentXMP = (xmpString) => {
  bp([{
    _obj: "set",
    _target: [
      { _ref: "property", _property: "XMPMetadataAsUTF8" },
      { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
    ],
    to: {
      _obj: "document",
      XMPMetadataAsUTF8: xmpString
    }
  }], {});
}
const getDocumentMetadata = (key, prefix) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  return obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] || "";
}
const setDocumentMetadata = (key, value, prefix) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] = value;
  const newXmpString = convert(obj, { format: "xml" })
  setDocumentXMP(newXmpString);
}
try {
  setDocumentMetadata("CreateDate", "1", "xmp");
} catch (error) {
  console.log(error.message);
}

```

---

<div class="post-metadata">

**Author:** ![Wanokuni](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/wanokuni/32/2203_2.png) [@Wanokuni](https://forums.creativeclouddeveloper.com/u/Wanokuni)\
**Post date:** [August 15, 2023, 12:38pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/7 "2023-08-15T12:38:25Z")

</div>

I test the same code, and it didn’t work for me as well: it would be great to get some assistance on this special for adobe team @Erin_Finnegan . I am trying to set the plus:Licensor and Web Statement.

```auto
    <plus:Licensor>
            <rdf:Seq>
               <rdf:li rdf:parseType="Resource">
                  <plus:LicensorName>Text</plus:LicensorName>
                  <plus:LicensorID>text</plus:LicensorID>
                  <plus:LicensorCity>text</plus:LicensorCity>
                  <plus:LicensorRegion>text</plus:LicensorRegion>
                  <plus:LicensorCountry>text</plus:LicensorCountry>
                  <plus:LicensorEmail>text@url.com</plus:LicensorEmail>
                  <plus:LicensorURL>www.url.com</plus:LicensorURL>
               </rdf:li>
            </rdf:Seq>
         </plus:Licensor>

```

and Web Statement properties:

```auto
      /<xmpRights:WebStatement>text</xmpRights:WebStatement>/

```

These properties are not recordable with the action panel, the only way is via XML manipulation. Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Erin\_Finnegan](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/erin_finnegan/32/100_2.png) [@Erin\_Finnegan](https://forums.creativeclouddeveloper.com/u/Erin_Finnegan)\
**Post date:** [August 15, 2023, 8:26pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/8 "2023-08-15T20:26:08Z")

</div>

Maybe @Sujai can help?

Recently InDesign added UXP support, and we have an XML-related “recipe” here: [https://developer.adobe.com/indesign/uxp/resources/recipes/xml/](https://developer.adobe.com/indesign/uxp/resources/recipes/xml/)

I’m not sure how I could help beyond this.

---

<div class="post-metadata">

**Author:** ![Sujai](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/sujai/32/2791_2.png) [@Sujai](https://forums.creativeclouddeveloper.com/u/Sujai)\
**Post date:** [August 16, 2023, 2:59am UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/9 "2023-08-16T02:59:51Z")

</div>

Handling XMP using the UXP XMP module will be available in UXP v7.2.  
I’m hoping that Photoshop will enable this feature for the 25.0 release. Will share more details on this in a few weeks once I have confirmation.

---

<div class="post-metadata">

**Author:** ![photonic](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/photonic/32/2513_2.png) [@photonic](https://forums.creativeclouddeveloper.com/u/photonic)\
**Post date:** [April 2, 2024, 10:45pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/10 "2024-04-02T22:45:05Z")

</div>

Is it available now?

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [April 2, 2024, 10:51pm UTC](https://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486/11 "2024-04-02T22:51:09Z")

</div>

Yes it is… as would reveal a search [on the forum](http://forums.creativeclouddeveloper.com/t/xmp-feature-is-available-in-photoshop-25-0/6815) or a look at the [changelog](https://developer.adobe.com/photoshop/uxp/2022/ps_reference/changelog/#photoshop-250-september-2023). 😉

.
