I have a program that processes some documents and based on business rules it creates CustomXMLParts and creates ContentControl for each part.
This is the function that creates the CustomXMlPart with OpenXML and adds a Guid as an attribute. This Guid is used to link to a ContentControl.
private static void InsertMetadata(WordprocessingDocument document, Dictionary<string, object> metaData, string customPartName, string id = null)
{
XElement metadataElement = null;
CustomXmlPart customXml = null;
if (id != null)
{
metadataElement = new XElement(customPartName, new XAttribute("Guid", "NeedsID"));
customXml = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
var guidID = document.MainDocumentPart.GetIdOfPart(customXml);
}
else
{
metadataElement = new XElement(customPartName);
customXml = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
}
foreach (KeyValuePair<string, object> entry in metaData)
{
metadataElement.Add(new XElement(entry.Key, entry.Value));
}
using (StreamWriter sr = new StreamWriter(customXml.GetStream()))
{
sr.Write(metadataElement);
}
}
There is a WordAddIn
That reads the parts like this and sets the Guid attribute to the ID of the customXML element
foreach (CustomXMLPart part in userAction.Application.ActiveDocument.CustomXMLParts.OfType<CustomXMLPart>())
{
var section = part.SelectSingleNode("MySection");
if (section!=null)
{
part.SetAttributeValue("Guid", part.Id.ToString());
}
}
In the WordAddIn the CustomXMLPart has the property Id and it is a Guid. The AddIn reads the parts and updates the attribute with the Guid that is the Id of the part.
The issue i'm having is that I can create the XML parts no problem and i can Create the ContentControl and add the tag of the Guid so the CC and XMLpart are linked. I create the CustomXMLPart then the CC and i Add the guid to link. However, when using openXML i cant get the ID as a guid after I've created and added it to the DocumentPart
var guidID = document.MainDocumentPart.GetIdOfPart(customXml);
This does not return a Guid. It returns something like this Rd96388a5e95041a0
But i need a Guid so i can add it as a tag to the CC. I couldn't figure a way to add a CustomXMLPart with a predefined Guid so i just create the part then try to get the Id.