public ActionResult Sample21() { // Check is data posted if (Request.HttpMethod == "POST") { //### Set variables and get POST data System.Collections.Hashtable result = new System.Collections.Hashtable(); String clientId = Request.Form["clientId"]; String privateKey = Request.Form["privateKey"]; String email = Request.Form["email"]; String name = Request.Form["name"]; String lastName = Request.Form["lastName"]; String callbackUrl = Request.Form["callbackUrl"]; String fileId = Request.Form["fileId"]; String url = Request.Form["url"]; var file = Request.Files["file"]; String fileName = ""; String guid = ""; String basePath = Request.Form["basePath"]; String iframe = ""; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); result.Add("email", email); result.Add("firstName", name); result.Add("lastName", lastName); // Check if callbackUrl is not empty if (!String.IsNullOrEmpty(callbackUrl)) { result.Add("callbackUrl", callbackUrl); } String message = null; // Check is all needed fields are entered if (String.IsNullOrEmpty(clientId) || String.IsNullOrEmpty(privateKey) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(lastName) || String.IsNullOrEmpty(name)) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample21", null, result); } else { //path to settings file - temporary save clientId and apiKey like to property file create_info_file(clientId, privateKey, ""); if (basePath.Equals("")) { basePath = "https://api.groupdocs.com/v2.0"; } result.Add("basePath", basePath); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); //if URL to web file was provided - upload the file from Web and get it's GUID if (!url.Equals("")) { //Make request to upload file from entered Url String uploadWeb = service.UploadUrl(url); if (uploadWeb != null) { //If file uploaded return his GuId guid = uploadWeb; //Get all files from GroupDocs account Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("My Web Documents", 0, -1, null, false, null, null, true); if (storageInfo.Files.Length > 0) { // Get file id by uploaded file GuId for (int i = 0; i < storageInfo.Files.Length; i++) { if (storageInfo.Files[i].Guid == guid) { fileName = storageInfo.Files[i].Name; } } } else { message = "Get files list is failed"; result.Add("error", message); return View("Sample21", null, result); } } //If file wasn't uploaded return error else { result.Add("error", "Something wrong with entered data"); return View("Sample21", null, result); } } //if file was uploaded locally - upload the file and get it's GUID if (!file.FileName.Equals("")) { //Upload local file Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream); if (upload.Guid != null) { //If file uploaded return his guid guid = upload.Guid; fileName = upload.AdjustedName; } else { //If file wasn't uploaded return error result.Add("error", "Something wrong with entered data"); return View("Sample21", null, result); } } if (!fileId.Equals("")) { guid = fileId; //Get all files from GroupDocs account Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("", 0, -1, null, false, null, null, true); if (storageInfo.Files.Length > 0) { // Get file id by uploaded file GuId for (int i = 0; i < storageInfo.Files.Length; i++) { if (storageInfo.Files[i].Guid == guid) { fileName = storageInfo.Files[i].Name; } } } else { message = "Get files list is failed"; result.Add("error", message); return View("Sample21", null, result); } } //Check is file uploaded if (!guid.Equals("") && !fileName.Equals("")) { //Create envilope using user id and entered by user name Groupdocs.Api.Contract.Signature.SignatureEnvelopeResponse envelop = service.CreateEnvelope("", "", fileName, guid, false); if (envelop.Status.Equals("Ok")) { decimal order = new decimal(); Groupdocs.Api.Contract.Signature.SignatureEnvelopeDocumentResponse addDocument = service.AddEnvelopeDocument(envelop.Result.Envelope.Id, guid, order, true); if (addDocument.Status.Equals("Ok")) { decimal dec = new decimal(); //Get role list for curent user Groupdocs.Api.Contract.Signature.SignatureRolesResponse roles = service.GetSignatureRoles(""); String roleId = ""; //Get id of role which can sign for (int i = 0; i < roles.Result.Roles.Length; i++) { if (roles.Result.Roles[i].Name.Equals("Signer")) { roleId = roles.Result.Roles[i].Id; break; } } //Add recipient to envelope Groupdocs.Api.Contract.Signature.SignatureEnvelopeRecipientResponse addRecipient = service.AddEnvelopeRecipient(envelop.Result.Envelope.Id, email, name, lastName, roleId, dec); if (addRecipient.Status.Equals("Ok")) { Groupdocs.Api.Contract.Signature.SignatureEnvelopeDocumentsResponse getDocuments = service.GetEnvelopeDocuments(envelop.Result.Envelope.Id); if (getDocuments.Status.Equals("Ok")) { Groupdocs.Api.Contract.Signature.SignatureEnvelopeFieldsResponse getFields = service.GetEnvelopeFields(envelop.Result.Envelope.Id, getDocuments.Result.Documents[0].DocumentId, addRecipient.Result.Recipient.Id, null); if (getFields.Status.Equals("Ok")) { Groupdocs.Api.Contract.Signature.SignatureEnvelopeFieldInfo[] fieldInfo = getFields.Result.Fields; if (fieldInfo.Length == 0) { result.Add("error", "You use a wrong file, it's don't content any fields for sign"); return View("Sample21", null, result); } for (int n = 0; n < fieldInfo.Length; n++) { Groupdocs.Api.Contract.Signature.SignatureEnvelopeFieldSettingsInfo fieldsSettings = new Groupdocs.Api.Contract.Signature.SignatureEnvelopeFieldSettingsInfo(); fieldsSettings.Name = fieldInfo[n].Name; fieldsSettings.LocationHeight = Convert.ToDecimal(fieldInfo[n].Locations[0].LocationHeight); fieldsSettings.LocationWidth = Convert.ToDecimal(fieldInfo[n].Locations[0].LocationWidth); fieldsSettings.LocationX = Convert.ToDecimal(fieldInfo[n].Locations[0].LocationX); fieldsSettings.LocationY = Convert.ToDecimal(fieldInfo[n].Locations[0].LocationY); Groupdocs.Api.Contract.Signature.SignatureEnvelopeFieldResponse addFields = service.AddEnvelopeField(envelop.Result.Envelope.Id, getDocuments.Result.Documents[0].DocumentId, addRecipient.Result.Recipient.Id, fieldInfo[n].Id, fieldsSettings); } //Send envelop with callbackUrl url Groupdocs.Api.Contract.Signature.SignatureEnvelopeSendResponse send = service.SendEnvelope(envelop.Result.Envelope.Id, callbackUrl); //Check is envelope send status if (send.Status.Equals("Ok")) { // Generate Embed viewer url with entered file id if (basePath.Equals("https://api.groupdocs.com/v2.0")) { iframe = "https://apps.groupdocs.com/signature2/signembed/" + envelop.Result.Envelope.Id + "/" + addRecipient.Result.Recipient.Id; } if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://dev-apps-groupdocs.dynabic.com/signature/signembed/" + envelop.Result.Envelope.Id + "/" + addRecipient.Result.Recipient.Id; } if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://stage-api-groupdocs.dynabic.com/signature/signembed/" + envelop.Result.Envelope.Id + "/" + addRecipient.Result.Recipient.Id; } if (basePath.Equals("https://realtime-api.groupdocs.com/v2.0")) { iframe = "https://realtime-apps.groupdocs.com/signature/signembed/" + envelop.Result.Envelope.Id + "/" + addRecipient.Result.Recipient.Id; } iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); result.Add("iframe", iframe); //Set data for template result.Add("envelop", envelop.Result.Envelope.Id); result.Add("recipient", addRecipient.Result.Recipient.Id); return View("Sample21", null, result); } //If status failed set error for template else { result.Add("error", send.ErrorMessage); return View("Sample21", null, result); } } else { result.Add("error", getFields.ErrorMessage); return View("Sample21", null, result); } } else { result.Add("error", getDocuments.ErrorMessage); return View("Sample21", null, result); } } else { result.Add("error", addRecipient.ErrorMessage); return View("Sample21", null, result); } } else { result.Add("error", addDocument.ErrorMessage); return View("Sample21", null, result); } } //If envelope wasn't created send error else { result.Add("error", envelop.ErrorMessage); return View("Sample21", null, result); } } // If request return's null return error to the template else { result.Add("error", "Upload is failed"); return View("Sample21", null, result); } } } // If data not posted return to template for filling of necessary fields else { return View("Sample21"); } }