protected void Page_Load(object sender, EventArgs e)
        {
            DocusignAuth docusignObj = new DocusignAuth();
            docusignObj.UserName = ConfigurationManager.AppSettings["Username"].ToString();
            docusignObj.Password = ConfigurationManager.AppSettings["Password"].ToString();
            docusignObj.IntegratorKey = ConfigurationManager.AppSettings["IntegratorKey"].ToString();
            docusignObj.TemplateId = ConfigurationManager.AppSettings["TemplateId"].ToString();
            docusignObj.Url = ConfigurationManager.AppSettings["Url"].ToString();

            DocusignMethods docusignMethods = new DocusignMethods(docusignObj);

            templateRole tempRole = new templateRole();
            tempRole.accessCode = "123";
            tempRole.name = "jay";
            tempRole.role = "admin";
            tempRole.roleName = "admin";
            tempRole.email = "*****@*****.**";

            List<templateRole> tempList = new List<templateRole>();
            tempList.Add(tempRole);

            envelopeDefinition data = new envelopeDefinition();
            data.emailSubject = "Hello";
            data.emailBlurb = "Hello";
            data.status = signatureStatus.sent;
            data.templateRoles = tempList;
            data.templateId = "62AF9A9C-0794-40E1-822F-5F070151D790";

            string resp = docusignMethods.RequestSignatureFromTemplate(data);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DocusignAuth docusignObj = new DocusignAuth();
            docusignObj.UserName = ConfigurationManager.AppSettings["Username"].ToString();
            docusignObj.Password = ConfigurationManager.AppSettings["Password"].ToString();
            docusignObj.IntegratorKey = ConfigurationManager.AppSettings["IntegratorKey"].ToString();
            docusignObj.TemplateId = ConfigurationManager.AppSettings["TemplateId"].ToString();
            docusignObj.Url = ConfigurationManager.AppSettings["Url"].ToString();

            DocusignMethods docusignMethods = new DocusignMethods(docusignObj);

            templateRole tempRole = new templateRole();
            tempRole.name = "jay";
            tempRole.roleName = "admin";
            tempRole.email = "*****@*****.**";

            List<templateRole> tempList = new List<templateRole>();
            tempList.Add(tempRole);

            envelopeDefinition data = new envelopeDefinition();
            data.emailSubject = "Hello";
            data.emailBlurb = "Hello";
            data.status = signatureStatus.created;
            data.templateRoles = tempList;
            data.templateId = "62AF9A9C-0794-40E1-822F-5F070151D790";

            string response = docusignMethods.EmbedSendingUX(data, "http://www.docusign.com");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DocusignAuth docusignObj = new DocusignAuth();
            docusignObj.UserName = ConfigurationManager.AppSettings["Username"].ToString();
            docusignObj.Password = ConfigurationManager.AppSettings["Password"].ToString();
            docusignObj.IntegratorKey = ConfigurationManager.AppSettings["IntegratorKey"].ToString();
            docusignObj.TemplateId = ConfigurationManager.AppSettings["TemplateId"].ToString();
            docusignObj.Url = ConfigurationManager.AppSettings["Url"].ToString();

            DocusignMethods docusignMethods = new DocusignMethods(docusignObj);

            #region Document
            Document document=new Document();
            document.documentId="1";
            document.name="test_document.pdf";

            DocDetail docDetail = new DocDetail();
            docDetail.document = document;
            #endregion

            #region RecipientCreation
            SignHere objSignHere = new SignHere();
            objSignHere.xPosition = "100";
            objSignHere.yPosition = "100";
            objSignHere.documentId = "1";
            objSignHere.pageNumber = "1";

            SignHereTab objSignHereTab = new SignHereTab();
            objSignHereTab.signHere = objSignHere;

            Tab objTab = new Tab();
            objTab.signHereTabs = objSignHereTab;

            signer objSigner=new signer();
            objSigner.recipientId = "1";
            objSigner.name = "jay";
            objSigner.email = "*****@*****.**";
            objSigner.tabs = objTab;

            List<signer> signerList = new List<signer>();
            signerList.Add(objSigner);

            Recipients recip=new Recipients();
            recip.signers = signerList;

            #endregion

            envelopeDefinition data = new envelopeDefinition();
            data.emailSubject = "Hello";
            data.emailBlurb = "Hello";
            data.status = signatureStatus.sent;
            data.documents = docDetail;
            data.recipients = recip;

            string response = docusignMethods.RequestSignatureFromDocument(@"C:\Users\jay\Desktop\test_document.pdf", data);
        }
        /// <summary>
        /// RequestSignatureFromTemplate request signature using Template
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public string RequestSignatureFromTemplate(envelopeDefinition requestData)
        {
            string accountId = string.Empty;
            string baseURL = string.Empty;
            errorMessage = string.Empty;
            //
            // STEP 1 - Login
            //
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.Accept = "application/xml";
                request.Method = "GET";
                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                string responseText = sr.ReadToEnd();
                using (XmlReader reader = XmlReader.Create(new StringReader(responseText)))
                {
                    while (reader.Read())
                    {	// Parse the xml response body
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "accountId"))
                            accountId = reader.ReadString();
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "baseUrl"))
                            baseURL = reader.ReadString();
                    }
                }

                requestData.accountId = accountId;
                string requestBody = Helper.GetXMLFromObject(requestData);

                // append "/envelopes" to baseURL and use in the request
                request = (HttpWebRequest)WebRequest.Create(baseURL + "/envelopes");
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.ContentType = "application/xml";
                request.ContentLength = requestBody.Length;
                request.Accept = "application/xml";
                request.Method = "POST";
                // write the body of the request
                byte[] body = System.Text.Encoding.UTF8.GetBytes(requestBody);
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(body, 0, requestBody.Length);
                dataStream.Close();
                // read the response
                webResponse = (HttpWebResponse)request.GetResponse();
                sr.Close();
                responseText = "";
                sr = new StreamReader(webResponse.GetResponseStream());
                responseText = sr.ReadToEnd();

                return responseText;
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    errorMessage = "Error code:: " + httpResponse.StatusCode;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        errorMessage += text;
                    }
                }
                return errorMessage;
            }
        }
        /// <summary>
        /// RequestSignatureFromDocument request signature using Document
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public string RequestSignatureFromDocument(string filePath, envelopeDefinition requestData)
        {
            string accountId = string.Empty;
            string baseURL = string.Empty;
            errorMessage = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.Accept = "application/xml";
                request.Method = "GET";
                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                string responseText = sr.ReadToEnd();
                using (XmlReader reader = XmlReader.Create(new StringReader(responseText)))
                {
                    while (reader.Read())
                    {	// Parse the xml response body
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "accountId"))
                            accountId = reader.ReadString();
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "baseUrl"))
                            baseURL = reader.ReadString();
                    }
                }

                //
                // STEP 2 - Create an Envelope with one recipient and one tab and send
                //

                string envDef = Helper.GetXMLFromObject(requestData);

                // read contents of document into the request stream
                FileStream fileStream = File.OpenRead(@"C:\Users\jay\Desktop\test_document.pdf");

                // build the multipart request body
                string requestBodyStart = "\r\n\r\n--BOUNDARY\r\n" +
                        "Content-Type: application/xml\r\n" +
                        "Content-Disposition: form-data\r\n" +
                        "\r\n" +
                        envDef + "\r\n\r\n--BOUNDARY\r\n" + 	// our xml formatted envelopeDefinition
                        "Content-Type: application/pdf\r\n" +
                        "Content-Disposition: file; filename=\"test_document.pdf\"; documentId=1\r\n" +
                        "\r\n";

                string requestBodyEnd = "\r\n--BOUNDARY--\r\n\r\n";

                // use baseURL value + "/envelopes" for url of this request
                request = (HttpWebRequest)WebRequest.Create(baseURL + "/envelopes");
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.ContentType = "multipart/form-data; boundary=BOUNDARY";
                request.Accept = "application/xml";
                request.ContentLength = requestBodyStart.ToString().Length + fileStream.Length + requestBodyEnd.ToString().Length;
                request.Method = "POST";
                // write the body of the request
                byte[] bodyStart = System.Text.Encoding.UTF8.GetBytes(requestBodyStart.ToString());
                byte[] bodyEnd = System.Text.Encoding.UTF8.GetBytes(requestBodyEnd.ToString());
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(bodyStart, 0, requestBodyStart.ToString().Length);

                // Read the file contents and write them to the request stream
                byte[] buf = new byte[4096];
                int len;
                while ((len = fileStream.Read(buf, 0, 4096)) > 0)
                {
                    dataStream.Write(buf, 0, len);
                }

                dataStream.Write(bodyEnd, 0, requestBodyEnd.ToString().Length);
                dataStream.Close();
                // read the response
                webResponse = (HttpWebResponse)request.GetResponse();
                sr.Close();
                responseText = "";
                sr = new StreamReader(webResponse.GetResponseStream());
                responseText = sr.ReadToEnd();

                return responseText;
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    errorMessage = "Error code:: " + httpResponse.StatusCode;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        errorMessage += text;
                    }
                }
                return errorMessage;
            }
        }
        /// <summary>
        /// EmbedSigningUX embeds the signing UX
        /// </summary>
        /// <param name="envData"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public string EmbedSigningUX(envelopeDefinition envData,string returnUrl)
        {
            string accountId = string.Empty;
            string baseURL = string.Empty;
            string envelopeId = string.Empty;
            string uri = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.Accept = "application/xml";
                request.Method = "GET";
                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                string responseText = sr.ReadToEnd();
                using (XmlReader reader = XmlReader.Create(new StringReader(responseText)))
                {
                    while (reader.Read())
                    {	// Parse the xml response body
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "accountId"))
                            accountId = reader.ReadString();
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "baseUrl"))
                            baseURL = reader.ReadString();
                    }
                }

                //
                // STEP 2 - Request Envelope Result
                //

                // Construct an outgoing XML request body

                envData.accountId = accountId;
                string requestBody = Helper.GetXMLFromObject(envData);

                // append "/envelopes" to baseUrl and use in the request
                request = (HttpWebRequest)WebRequest.Create(baseURL + "/envelopes");
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.ContentType = "application/xml";
                request.Accept = "application/xml";
                request.ContentLength = requestBody.Length;
                request.Method = "POST";
                // write the body of the request
                byte[] body = System.Text.Encoding.UTF8.GetBytes(requestBody);
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(body, 0, requestBody.Length);
                dataStream.Close();
                // read the response
                webResponse = (HttpWebResponse)request.GetResponse();
                sr = new StreamReader(webResponse.GetResponseStream());
                responseText = sr.ReadToEnd();
                using (XmlReader reader = XmlReader.Create(new StringReader(responseText)))
                {
                    while (reader.Read())
                    {	// Parse the xml response body
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "envelopeId"))
                            envelopeId = reader.ReadString();
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "uri"))
                            uri = reader.ReadString();
                    }
                }

                //
                // STEP 3 - Get the Embedded Console Sign View
                //

                // construct another outgoing XML request body
                string reqBody = "<recipientViewRequest xmlns=\"http://www.docusign.com/restapi\">" +
                    "<authenticationMethod>email</authenticationMethod>" +
                        "<email>" + Username + "</email>" +	 	// NOTE: Use different email address if username provided in non-email format!
                        "<returnUrl>" + returnUrl + "</returnUrl>" +  // username can be in email format or an actual ID string
                        "<clientUserId>1</clientUserId>" +
                        "<userName>Name</userName>" +
                        "</recipientViewRequest>";

                // append uri + "/views/recipient" to baseUrl and use in the request
                request = (HttpWebRequest)WebRequest.Create(baseURL + uri + "/views/recipient");
                request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
                request.ContentType = "application/xml";
                request.Accept = "application/xml";
                request.ContentLength = reqBody.Length;
                request.Method = "POST";
                // write the body of the request
                byte[] body2 = System.Text.Encoding.UTF8.GetBytes(reqBody);
                Stream dataStream2 = request.GetRequestStream();
                dataStream2.Write(body2, 0, reqBody.Length);
                dataStream2.Close();
                // read the response
                webResponse = (HttpWebResponse)request.GetResponse();
                sr = new StreamReader(webResponse.GetResponseStream());
                responseText = sr.ReadToEnd();
                return responseText;
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    errorMessage = "Error code:: " + httpResponse.StatusCode;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        errorMessage += text;
                    }
                }
                return errorMessage;
            }
        }