Example #1
0
        //==========================================================================================
        // *** Walkthrough #6 - Get Documents List and Download Documents
        //==========================================================================================
        private void GetDocsListAndDownloadDocuments()
        {
            //*****************************************************************
            // ENTER VALUES FOR FOLLOWING VARIABLES!
            //*****************************************************************
            string AccountEmail = "***";
            string AccountPassword = "******";
            string EnvelopeId = "***";
            //*****************************************************************

            // user credentials 
            Account account = new Account();
            account.Email = AccountEmail;
            account.Password = AccountPassword;

            // make the login call (retrieves your baseUrl and accountId)
            bool result = account.Login();
            if (!result)
            {
                Console.WriteLine("Login API call failed for user {0}.\nError Code:  {1}\nMessage:  {2}", account.Email, account.RestError.errorCode, account.RestError.message);
                return;
            }

            // create envelope object and assign login info
            Envelope envelope = new Envelope();
            envelope.Login = account;

            // assign the passed in envelope id to the newly created envelope object
            envelope.EnvelopeId = EnvelopeId;

            // get the envelope's documents 
            List<EnvelopeDocument> envDocs = envelope.GetDocuments();

            // loop through the documents and display some info about them
            foreach (var doc in envDocs)
            {
                Console.WriteLine("Document id {0} is named {1}.", doc.documentId, doc.name);
            }

            // now let's download the actual document bytes and store in a local file in the application's main directory
            string applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            byte[] completedDocBytes = envelope.GetCompletedDocument(EnvelopeId, true);

            // set local document name here, currently using envelopeID.pdf as name
            string localFileName = applicationPath + "\\" + EnvelopeId + ".pdf";

            // combine all envelope documents (including certificate) into local PDF file
            File.WriteAllBytes(localFileName, completedDocBytes);

            Console.WriteLine("Completed documents have been downloaded to {0}", localFileName);
        }
        public void EnvelopeCreateWithDocumentObjectsAndGetDocumentFieldsTest()
        {
            const bool expected = true;
            const string expectedDocumentAttributeName = "Document Attribute Name";
            const string expectedDocumentAttributeValue = "Document Attribute Value";
            var actual = false;

            Assert.IsFalse(string.IsNullOrEmpty(_account.BaseUrl));

            var target = new Envelope { Login = _account };
            var fi = new FileInfo("./Test Contract.pdf");

            var documentCustomFields = new List<DocumentField>
            {
                new DocumentField {name = expectedDocumentAttributeName, value = expectedDocumentAttributeValue}
            };
            var documents = new List<Document>
            {
                new Document
                {
                    attachmentDescription = fi.Name.Replace(fi.Extension, string.Empty),
                    documentId = "1",
                    documentFields = documentCustomFields.ToArray(),
                    fileExtension = fi.Extension,
                    name = fi.Name
                }
            };

            var fileBytes = new List<Byte[]> { File.ReadAllBytes(fi.FullName) };

            try
            {
                actual = target.Create(fileBytes, documents);
            }
            catch (ArgumentNullException)
            {
            }

            Assert.AreEqual(expected, actual);
            Assert.IsFalse(string.IsNullOrEmpty(target.SenderViewUrl));
            var actualDocuments = target.GetDocuments();
            Assert.AreEqual(documents.Count, actualDocuments.Count);
            for (var i = 0; i < documents.Count; i++)
            {
                Assert.AreEqual(documents[i].documentId, actualDocuments[i].documentId);
                Assert.AreEqual(documents[i].name, actualDocuments[i].name);

                var fields = target.GetDocumentFields(documents[i].documentId);
                Assert.AreEqual(documents[i].documentFields.Count(), fields.documentFields.Count());
                for (var j = 0; j < documents[i].documentFields.Count(); j++)
                {
                    Assert.AreEqual(documents[i].documentFields[j].name, fields.documentFields[j].name);
                    Assert.AreEqual(documents[i].documentFields[j].value, fields.documentFields[j].value);
                }
            }
        }
        public DocumentForSign GetEmbeddedDocument(Envelope envelope, TemplateInfo templateInfo, string returnUrl)
        {
            try
            {
                if (envelope == null)
                    throw new ArgumentNullException("envelope");
                if (templateInfo == null)
                    throw new ArgumentNullException("templateInfo");

                envelope.GetRecipients();
                var signer = envelope.Recipients.signers[0];

                if (signer != null)
                {
                    var documents = envelope.GetDocuments();

                    var envelopeDocument = documents.FirstOrDefault(x => x.name.Contains(templateInfo.Name));

                    if (envelopeDocument != null)
                    {
                        var tabs = new CustomTabs(int.Parse(envelopeDocument.documentId), signer.recipientId, signer.name);

                        envelope.AddTabs(new TabCollection
                        {
                            companyTabs = tabs.CompanyTabs,
                            dateSignedTabs = tabs.dateSignedTabs,
                            signHereTabs = tabs.SignHereTabs,
                            textTabs = tabs.TextTabs
                        });

                        var doc = new DocumentForSign
                        {
                            Document = Template.GetTemplatePreview(templateInfo.Id),
                            DocumentName = templateInfo.Name,
                            EmbeddedUrl = envelope.GetEmbeddedSignerView(returnUrl, signer)
                        };

                        if (!string.IsNullOrEmpty(doc.EmbeddedUrl))
                        {
                            _context.Documents.Add(new DocuSignDocument()
                            {
                                EnvelopeId = envelope.EnvelopeId,
                                EmbeddedUrl = doc.EmbeddedUrl,
                                Status = "sent",
                                UserId = signer.clientUserId
                            });

                            _context.SaveChanges();
                        }

                        return doc;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }