Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE

Qlik Sense: Child objects are linked to the original sheet when cloning a sheet with CloneObject()

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
Sonja_Bauernfeind
Digital Support
Digital Support

Qlik Sense: Child objects are linked to the original sheet when cloning a sheet with CloneObject()

Last Update:

Apr 26, 2021 7:59:48 AM

Updated By:

Sonja_Bauernfeind

Created date:

Apr 26, 2021 7:59:48 AM

Cloning a sheet with CloneObject() (Engine API or CloneGenericObject() in .NET SDK), Child objects are linked to the original sheet which causes that if an object is modified on the new sheet, the change is also reflected on the original sheet.

 

Environment

 

The information in this article is provided as-is and to be used at own discretion. Depending on tool(s) used, customization(s), and/or other factors ongoing support on the solution below may not be provided by Qlik Support.

 

The CloneObject() method clones all child objects but it does not automatically update the reference to the new child objects in the new sheet, this needs to be done separately with SetProperties(), below is an example:

using System.Collections.Generic;
using System.Linq;
using Qlik.Engine;
using Qlik.Sense.Client;

namespace CloneSheet
{
	class Program
	{
		static void Main(string[] args)
		{
			var url = "https://<url>";
			var appName = "<appName>";
			var sourceSheetId = "<sheetId>";

			var location = Location.FromUri(url);
			location.AsNtlmUserViaProxy();
			var appId = location.AppWithNameOrDefault(appName);
			using (var app = location.App(appId))
			{
				var destinationSheetId = app.CloneGenericObject(sourceSheetId);
				var sourceSheet = app.GetGenericObject(sourceSheetId);
				var destinationSheet = app.GetGenericObject(destinationSheetId);
				var destinationSheetProps = destinationSheet.Properties.As<SheetProperties>();
				var sourceChildInfos = sourceSheet.GetChildInfos();
				var destinationChildInfos = destinationSheet.GetChildInfos();
				var childIdMap = new Dictionary<string, string>(sourceChildInfos.Zip(destinationChildInfos, (i0, i1) => new KeyValuePair<string,string>(i0.Id, i1.Id)));
				foreach (var destinationCell in destinationSheetProps.Cells)
				{
					destinationCell.Name = childIdMap[destinationCell.Name];
				}

				destinationSheetProps.MetaDef.Title = "Clone";
				destinationSheet.SetProperties(destinationSheetProps);
				app.DoSave();
			}
		}
	}
}


See on github: https://github.com/kolsrud/qlik-dot-net-sdk-examples/blob/master/CloneSheet/Program.cs

Version history
Last update:
‎2021-04-26 07:59 AM
Updated by: