Copy Templates and Layouts between Users in .NET
Thursday, February 2, 2023 at 08:47amI know there is a tool to copy templates and layouts between users. I want to add that ability to my own tool we have for our help desk. Is there an example of how to do this in .NET that already exists? (we would download it form one user and upload it to another user's account using the API keys)
Reply to: Copy Templates and Layouts between Users in .NET
Thursday, February 2, 2023 at 01:52pmHi Richard,
Below is a code snippet I created to copy templates between users. Because the time is limited, the code utlizes the .NET SDK, and somehow it can't copy field conditions due to the nature of the SDK designs. Next, it could take me some time to translate the code to RESTful fashion.
OssClient ossClient1 = new OssClient("QVRxx4TA==", "https://sandbox.esignlive.com/api");
OssClient ossClient2 = new OssClient("Z0JXxxxNRNQ==", "https://preview.esignlive.com/api");
String template_from_id = "z2crzSkaEBgy81qCyifpRytmBJQ=";
String template_to_sender_id = "gBWRANNzRIoR";
//Step1: copy template
DocumentPackage template_from = ossClient1.GetPackage(new PackageId(template_from_id));
//download documents
foreach (var doc in template_from.Documents) {
doc.Content = ossClient1.DownloadOriginalDocument(template_from.Id, doc.Id);
doc.FileName = doc.Name + ".pdf";
}
//Step2: replace sender
Signer sender = null;
foreach (var signer in template_from.Signers)
{
if (signer.SignerType == "ACCOUNT_SENDER")
{
sender = signer;
}
}
if (sender != null)
{
template_from.Signers.Remove(sender);
template_from.SenderInfo = null;
Sender to_sender = ossClient2.AccountService.GetSender(template_to_sender_id);
template_from.Signers.Add(SignerBuilder.NewSignerWithEmail(to_sender.Email).WithFirstName(to_sender.FirstName).WithLastName(to_sender.LastName).WithCustomId(sender.Id).Build());
template_from.SenderInfo = SenderInfoBuilder.NewSenderInfo(to_sender.Email).Build();
foreach (var doc in template_from.Documents)
{
foreach (var signature in doc.Signatures.ToArray())
{
if (signature.SignerEmail == sender.Email)
{
doc.Signatures.Remove(signature);
Signature signature_to = new Signature(to_sender.Email,signature.Page,signature.X,signature.Y);
signature_to.Disabled = signature.Disabled;
signature_to.EnforceCaptureSignature = signature.EnforceCaptureSignature;
signature_to.Extract = signature.Extract;
signature_to.AddFields(signature.Fields);
signature_to.FontSize = signature.FontSize;
signature_to.Height = signature.Height;
signature_to.Width = signature.Width;
signature_to.Id = signature.Id;
signature_to.Name = signature.Name;
signature_to.Optional = signature.Optional;
signature_to.Style = signature.Style;
doc.Signatures.Add(signature_to);
}
}
}
}
template_from.Id = null;
template_from.Conditions = new List<FieldCondition>();
PackageId template_to_id = ossClient2.CreateTemplate(template_from);
Debug.WriteLine($"template_to_id: {template_to_id.Id}");
//Step3: copy reminder
ReminderSchedule reminderSchedule_from = ossClient1.ReminderService.GetReminderScheduleForPackage(new PackageId(template_from_id));
if (reminderSchedule_from != null) {
reminderSchedule_from.PackageId = template_to_id;
ossClient2.ReminderService.CreateReminderScheduleForPackage(reminderSchedule_from);
}
//Step4: copy document visiblity
DocumentVisibility documentVisiblity_from = ossClient1.getDocumentVisibility(new PackageId(template_from_id));
if (documentVisiblity_from != null) {
ossClient2.ConfigureDocumentVisibility(template_to_id, documentVisiblity_from);
}
Duo