Documentation > CMS Template API Library > Asset > ExtractedContent

ExtractedContent

Used with Search G2. If this is a binary asset and content parsing is enabled, this is extracted on upload and stored as a JSON string. In the SearchG2Context contexts, this will be populated on-the-fly, if not loaded yet. Failure will return empty object. Check context.Error for details on failure.

Type

CrownPeak.CMSAPI.ContentParseMetadata

Code Example

C#

Sample:
    // Extracting content from a binary attached to an asset.
	Asset myAsset = Asset.Load("/PathTo/Asset");
	if(myAsset.IsLoaded)
	{		
		UploadedFile document = myAsset.UploadedFiles["document_upload"]; // Reference file attached to asset under the field name "document_upload"
		if(document.IsLoaded)
		{	
			ContentParseMetadata extractedContent = document.ExtractedContent;
			if (extractedContent != null)
			{
				Out.WriteLine("Content: " + extractedContent.Content);

				Dictionary<string, object> metadata = extractedContent.Metadata;
				if (metadata != null)
				{
					if (metadata.ContainsKey("meta:author"))
					{
						// NOTE: these values are usually strings, but can sometimes be arrays of strings
						// depending on the source document and the property
						var value = metadata["meta:author"] as string;
						if (value != null)
							Out.WriteLine("Author: " + value + "<br />");
					}
					if (metadata.ContainsKey("meta:creation-date"))
					{
						var value = metadata["meta:creation-date"] as string;
						if (value != null)
							Out.WriteLine("Create Date: " + value + "<br />");
					}
					if (metadata.ContainsKey("Last-Modified"))
					{
						var value = metadata["Last-Modified"] as string;
						if (value != null)
							Out.WriteLine("Last Modified: " + value + "<br />");
					}
				}
			}
		}
	}


   // Loading a binary direct
	Asset document = Asset.Load("/PathTo/Document.pdf");
	if (document.IsLoaded)
	{
		ContentParseMetadata extractedContent = document.ExtractedContent;
		if (extractedContent != null)
		{
			Out.WriteLine("Content: " + extractedContent.Content);

			Dictionary<string, object> metadata = extractedContent.Metadata;
			if (metadata != null)
			{
				if (metadata.ContainsKey("meta:author"))
				{
					// NOTE: these values are usually strings, but can sometimes be arrays of strings
					// depending on the source document and the property
					var value = metadata["meta:author"] as string;
					if (value != null)
						Out.WriteLine("Author: " + value + "<br />");
				}
				if (metadata.ContainsKey("meta:creation-date"))
				{
					var value = metadata["meta:creation-date"] as string;
					if (value != null)
						Out.WriteLine("Create Date: " + value + "<br />");
				}
				if (metadata.ContainsKey("Last-Modified"))
				{
					var value = metadata["Last-Modified"] as string;
					if (value != null)
						Out.WriteLine("Last Modified: " + value + "<br />");
				}
			}
		}
	}

Connect with Crownpeak