contentstack.utils 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package contentstack.utils --version 1.0.0
NuGet\Install-Package contentstack.utils -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="contentstack.utils" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add contentstack.utils --version 1.0.0
#r "nuget: contentstack.utils, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install contentstack.utils as a Cake Addin
#addin nuget:?package=contentstack.utils&version=1.0.0

// Install contentstack.utils as a Cake Tool
#tool nuget:?package=contentstack.utils&version=1.0.0

Contentstack Utils Dotnet

Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.

This guide will help you get started with Contentstack .NET Utils SDK to build apps powered by Contentstack.

Prerequisites

To get started with .NET, you will need the following:

  • .NET version 3.1 or later

Setup and Installation

Note: If you are using Contentstack .NET SDK, you don’t need to download the Contentstack.Utils package separately as it will be already available for use.

To download the Contentstack.Utils module, open the terminal and perform any of the following options:

  • Via Package Manager:
PM> Install-Package contentstack.utils
  • Via .NET CLI:
dotnet add package contentstack.utils

After successful installation, to use the module in your application, you need to add a namespace to your class:

using Contentstack.Utils

Usage

Let’s learn how you can use .NET Utils SDK to render embedded items by performing the following steps:

  1. Create CustomRenderOption class

To render embedded items on the front-end, use the CustomRenderOption class, and define the UI elements you want to show in the front-end of your website, as shown in the example below. In this example, we have specified the cases for each method of adding embedded items: Block, Inline, Link, Display and Download (for asset).

using System.Collections.Generic;  
using Contentstack.Utils.Interfaces;  
using Contentstack.Utils.Models;  
using Contentstack.Utils.Enums;  
public  class CustomRenderOption: Options  
{  
	public CustomRenderOption(IEntryEmbedable entry) : base(entry)  
	{  
	}  
	public  override  string RenderOption(IEmbeddedObject embeddedObject, Metadata metadata)  
	{  
		switch (metadata.StyleType)  
			{

			//if you have added embedded object using the “Block” option  
			case StyleType.Block:  
				string renderString = "";  
				if (embeddedObject is IEmbeddedEntry)  
				{  
					renderString += $"<div> <b>{((IEmbeddedEntry)embeddedObject).Title}</b></div>";  
				}  
				else  if (embeddedObject is IEmbeddedContentTypeUid)  
				{  
					renderString += $"<div> <b>{embeddedObject.Uid}</b></div>";  
				}  
				return renderString;  
			
			//if you have added embedded object using the “Inline” option
			case StyleType.Inline:  
				if (embeddedObject is IEmbeddedEntry)  
				{  
					return  $"<span><b>{((IEmbeddedEntry)embeddedObject).Title}</b></span>";  
				}  
				else  if (embeddedObject is IEmbeddedContentTypeUid)  
				{  
					return  $"<span><b>{embeddedObject.Uid}</b></span>";  
				}  
				return  "<span>" + embeddedObject.Uid + "</span>";  
				
			//if you have added embedded object using the “Link” option
			case StyleType.Link:  
				if (embeddedObject is IEmbeddedEntry)  
				{ 
					return  $"<span> Please find link to: <a><b>{metadata.Text ?? ((IEmbeddedEntry)embeddedObject).Title}</b></a></span>";  
				}  
				else  if (embeddedObject is IEmbeddedContentTypeUid)  
				{  
					return  $"<span> Please find link to: <a><b>{metadata.Text ?? embeddedObject.Uid}</b></a></span>";  
				}  
				return  "<a href=\"" + embeddedObject.Uid + "\">" + (metadata.Text ?? embeddedObject.Uid) + "</a></span>";  

			//if you have embedded an asset into the RTE field
			case StyleType.Display:  
				if (embeddedObject is IEmbeddedAsset)  
				{  
					return  $"<b>{((IEmbeddedAsset)embeddedObject).Title}</b><p>{((IEmbeddedAsset)embeddedObject).FileName} image: <img src=\"{((IEmbeddedAsset)embeddedObject).Url}\" alt=\"{((IEmbeddedAsset)embeddedObject).Title}\"/></p>";  
				}  
				return  "<img src=\"" + embeddedObject.Uid + "\" alt=\"" + embeddedObject.Uid + "\" />";  

			//if you have embedded an asset directly via a download link.
			case StyleType.Download:  
				if (embeddedObject is IEmbeddedAsset)  
				{  
					return  "<span> Please find link to: <a href=\"" + ((IEmbeddedAsset)embeddedObject).Url + "\">" + (metadata.Text ?? ((IEmbeddedAsset)embeddedObject).Title) + "</a></span>";  
				}  
				return  "<a href=\"" + embeddedObject.Uid + "\">" + (metadata.Text ?? embeddedObject.Uid) + "</a>";  
		}  
		return  base.RenderOption(embeddedObject, metadata);  
	}  
}
  1. Initialize the class

Initialize either the Options or CustomRenderOption class to use them for rendering embedded items as shown below:

//To use the default render option:  
Options defaultRender = new Options(entry);
  
//To use CustomRenderOptions:  
CustomRenderOption defaultRender = new CustomRenderOption(entry);  

Note: Make sure the entry parameter has implemented the IEmbeddedObject property.

Basic Queries

Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.

Fetch Embedded Item(s) from a Single Entry

To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID:

using Contentstack.Core; // ContentstackClient  
using Contentstack.Core.Models; // Stack, Query, Entry, Asset, ContentType, ContentstackCollection  
using Contentstack.Core.Configuration; // ContentstackOptions  
using Contentstack.Utils; // Utils.RenderContent  
Using Contentstack.Utils.Models; // Options, Metadata  
ContentstackClient client = new ContentstackClient("api_key", "delivery_token", "enviroment_name");  
  
client.ContentType("product").Entry("<entry_uid>");  
	.includeEmbeddedItems()
	.Fetch<Product>().ContinueWith((response) => {  
		if (!response.IsFaulted) {
			// To use the default render option:
			string result = Utils.RenderContent(response.result.rte, new Option(response.result));
			// To use the Custom render option:  
			string result = Utils.RenderContent(response.result.rte, new CustomRenderOption(response.result));  
		}  
});

Fetch Embedded Item(s) from Multiple Entries

To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, content type UID. You can use the path variable in case the entries have multiple RTE fields.

using Contentstack.Core; // ContentstackClient  
using Contentstack.Core.Models; // Stack, Query, Entry, Asset, ContentType, ContentstackCollection  
using Contentstack.Core.Configuration; // ContentstackOptions  
using Contentstack.Utils; // Utils.RenderContent  
Using Contentstack.Utils.Models; // Options, Metadata  
  
ContentstackClient client = new ContentstackClient("api_key", "delivery_token", "enviroment_name");  
  
client.ContentType("product").Query()  
	.includeEmbeddedItems();  
	.Find<Product>().ContinueWith((t) => {  
		if (!t.IsFaulted) {  
			ContentstackCollection<Product> result = t.Result;  
			foreach (var product in result.Items)  
			{  
				// To use the default render option  
				string result = Utils.RenderContent(product.rte, new Option(product));  
				// To use the Custom render option  
				string result = Utils.RenderContent(product.rte, new CustomRenderOption(product));  
			}  
		}  
	});
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on contentstack.utils:

Package Downloads
contentstack.csharp

.NET SDK for the Contentstack Content Delivery API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 349 3/20/2024
1.0.1 257,512 7/16/2021
1.0.0 6,192 4/5/2021

Initial release