public ActionResult Sample41() { // 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[] emailsArray = Request.Form["email[]"].Split(','); String[] emails = new String[emailsArray.Length - 1]; String basePath = Request.Form["basePath"]; String fileId = Request.Form["fileId"]; String url = Request.Form["url"]; var file = Request.Files["file"]; String callback = Request.Form["callbackUrl"]; String fileGuId = ""; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); String message = null; // Check is all needed fields are entered if (clientId == null || privateKey == null || emails == null) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample41", null, result); } else { //Add all emails to array if (emailsArray[emailsArray.Length - 1].Equals("")) { for (int n = 0; n < emailsArray.Length - 1; n++) { emails[n] = emailsArray[n]; } } else { emails = new String[emailsArray.Length]; for (int n = 0; n < emailsArray.Length; n++) { emails[n] = emailsArray[n]; } } //path to settings file - temporary save clientId and apiKey like to property file create_info_file(clientId, privateKey, ""); //remove temporary file with callback info String callbackInfo = AppDomain.CurrentDomain.BaseDirectory + "callback_info.txt"; if (System.IO.File.Exists(callbackInfo)) { System.IO.File.Delete(callbackInfo); } //Set base path for API if it's not entered by user if (basePath == "") { basePath = "https://api.groupdocs.com/v2.0"; } // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); //Check is chosen local file if (!file.ContentLength.Equals(0)) { // Upload file with empty description. Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream); // Check is upload successful if (upload.Guid != null) { // Put uploaded file GuId to the result's list fileGuId = upload.Guid; } // If upload was failed return error else { message = "UploadFile returns error"; result.Add("error", message); return View("Sample41", null, result); } } //Check is url entered if (!url.Equals("")) { //Make request to upload file from entered Url String guid = service.UploadUrl(url); if (guid != null) { //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) { fileGuId = storageInfo.Files[i].Guid; } } } else { message = "Get files list is failed"; result.Add("error", message); return View("Sample41", null, result); } } //If file wasn't uploaded return error else { result.Add("error", "Something wrong with entered data"); return View("Sample41", null, result); } } //Check is file guid entered if (!fileId.Equals("")) { fileGuId = fileId; } result.Add("fileId", fileGuId); // Generate Embed Annotation url with file GUID string iframe = null; if (callback == null) { callback = ""; } result.Add("callbackUrl", callback); //Set file sesion callback - will be trigered when user add, remove or edit commit for annotation Groupdocs.Api.Contract.Annotation.SetSessionCallbackUrlResult setCallback = service.SetSessionCallbackUrl(fileGuId, callback); //Generate iframe URL for iframe if (basePath.Equals("https://api.groupdocs.com/v2.0")) { iframe = "https://apps.groupdocs.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://dev-apps-groupdocs.dynabic.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://stage-apps-groupdocs.dynabic.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://realtime-api.groupdocs.com/v2.0")) { iframe = "https://realtime-apps.groupdocs.com/document-annotation2/embed/" + fileGuId; } //Prepare variables string userGuid = null; String email = String.Empty; String[] Email = new String[emails.Length]; int counter = 0; //Get all users from account Groupdocs.Api.Contract.GetAccountUsersResult allUsers = service.GetAccountUsers(); if (allUsers != null) { //Loop for all users foreach (var item in emails) { //Get current user email email = item; //Add all entered by user emails to emails array to collaborators for (int m = 0; m < emails.Length; m++) { Email[m] = emails[m]; } //Loop to get user GUID if user with same email already exist for (int i = 0; i < allUsers.Users.Length; i++) { userGuid = string.Empty; if (email.Equals(allUsers.Users[i].PrimaryEmail)) { userGuid = allUsers.Users[i].Guid; break; } } //If user not exist create it if (userGuid == null || userGuid == string.Empty) { //Create new user Groupdocs.Api.Contract.UserInfo user = new Groupdocs.Api.Contract.UserInfo(); Groupdocs.Api.Contract.RoleInfo roleInfo = new Groupdocs.Api.Contract.RoleInfo(); Groupdocs.Api.Contract.RoleInfo[] roleList = new Groupdocs.Api.Contract.RoleInfo[1]; roleInfo.Id = 3; roleInfo.Name = "User"; roleList[0] = roleInfo; user.FirstName = email; user.LastName = email; user.Roles = roleList; user.PrimaryEmail = email; //Add new user to account Groupdocs.Api.Contract.UserIdentity newUser = service.UpdateAccountUser(user); if (newUser.Guid != null) { userGuid = newUser.Guid; } else { message = "Update account user is failed"; result.Add("error", message); return View("Sample41", null, result); } } System.Threading.Thread.Sleep(2000); //Get all collaborators for selected document Groupdocs.Api.Contract.Annotation.GetCollaboratorsResult getCollaborators = service.GetAnnotationCollaborators(fileGuId); if (getCollaborators != null) { //Check if user already collaborator of the document String urlParameter = null; for (int n = 0; n < getCollaborators.Collaborators.Length; n++) { //If user is collaborator add his GUID to the URl if (getCollaborators.Collaborators[n].PrimaryEmail.Equals(email)) { iframe = iframe + "?uid=" + userGuid; urlParameter = userGuid; iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); break; } } //If this parameters is null that means that user not a collaborator if (iframe.Contains("?uid=") && urlParameter != null) { if (counter == 0) { counter = 1; result.Add("iframe", iframe); } } else { if (counter == 0) { //Add user to collaborators of the document Groupdocs.Api.Contract.Annotation.SetCollaboratorsResult collaborate = service.SetAnnotationCollaborators(fileGuId, Email); if (collaborate != null) { counter = 1; iframe = iframe + "?uid=" + userGuid; iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); result.Add("iframe", iframe); } else { result.Add("error", "Set annotation colaborator is failed"); return View("Sample41", null, result); } } } } else { result.Add("error", "Get annotation colaborators is failed"); return View("Sample41", null, result); } } return View("Sample41", null, result); } else { result.Add("error", "Faild to get all users from account"); return View("Sample41", null, result); } } } // If data not posted return to template for filling of necessary fields else { return View("Sample41"); } }
//### callback for Sample41 public ActionResult sample41_callback() { // Get user info String infoFile = AppDomain.CurrentDomain.BaseDirectory + "user_info.txt"; System.IO.StreamReader userInfoFile = new System.IO.StreamReader(infoFile); String clientId = userInfoFile.ReadLine(); String privateKey = userInfoFile.ReadLine(); userInfoFile.Close(); // Check is data posted if (Request.HttpMethod == "POST") { //Get callback data from JSON System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream); String jsonString = reader.ReadToEnd(); var data = System.Web.Helpers.Json.Decode(jsonString); var serializedData = System.Web.Helpers.Json.Decode(data.SerializedData); String documentGuid = serializedData.DocumentGuid; documentGuid = documentGuid.Replace(" ", ""); String collaboratorGuid = serializedData.UserGuid; collaboratorGuid = collaboratorGuid.Replace(" ", ""); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService("https://api.groupdocs.com/v2.0", clientId, privateKey); //Make request to api for get document info by job id if (documentGuid != "" && collaboratorGuid != "") { //Get all collaborators for the document Groupdocs.Api.Contract.Annotation.GetCollaboratorsResult getCollaborators = service.GetAnnotationCollaborators(documentGuid); //Create ReviewerInfo array Groupdocs.Api.Contract.ReviewerInfo[] reviewers = new Groupdocs.Api.Contract.ReviewerInfo[getCollaborators.Collaborators.Length]; for (int n = 0; n < getCollaborators.Collaborators.Length; n++) { //Set reviewer rights to view only getCollaborators.Collaborators[n].AccessRights = Groupdocs.Common.AnnotationReviewerRights.CanView; //Add riviewer to ReviewerInfo array reviewers[n] = getCollaborators.Collaborators[n]; } //Make request to API to update reviewer rights Groupdocs.Api.Contract.Annotation.SetReviewerRightsResult setRights = service.SetReviewerRights(documentGuid, reviewers); //path to settings file - temporary save clientId and apiKey like to property file String callbackInfo = AppDomain.CurrentDomain.BaseDirectory + "callback_info.txt"; //open file in rewrite mode FileStream fcreate = System.IO.File.Open(callbackInfo, FileMode.Create); // will create the file or overwrite it if it already exists //String filePath = AppDomain.CurrentDomain.BaseDirectory + "user_info.txt"; System.IO.StreamWriter infoFileStreamWriter = new StreamWriter(fcreate); //w = System.IO.File.CreateText(filePath); infoFileStreamWriter.WriteLine("User rights was set to view only"); //save clientId infoFileStreamWriter.Flush(); infoFileStreamWriter.Close(); } return View("sample41_callback"); } //If data not posted return to template for filling of necessary fields else { return View("sample41_callback"); } }
public ActionResult Sample38() { // 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 firstName = Request.Form["firstName"]; String lastName = Request.Form["lastName"]; String basePath = Request.Form["basePath"]; String fileId = Request.Form["fileId"]; String url = Request.Form["url"]; var file = Request.Files["file"]; String fileGuId = ""; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); result.Add("email", email); result.Add("firstName", firstName); result.Add("lastName", lastName); String message = null; // Check is all needed fields are entered if (clientId == null || privateKey == null || email == null || firstName == null || lastName == null) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample38", null, result); } else { // Create string array with emails String[] collaborators = new String[1]; collaborators[0] = email; //Set base path for API if it's not entered by user if (basePath == "") { basePath = "https://api.groupdocs.com/v2.0"; } // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); //Check is chosen local file if (!file.ContentLength.Equals(0)) { // Upload file with empty description. Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream); // Check is upload successful if (upload.Guid != null) { // Put uploaded file GuId to the result's list fileGuId = upload.Guid; } // If upload was failed return error else { message = "UploadFile returns error"; result.Add("error", message); return View("Sample38", null, result); } } //Check is url entered if (!url.Equals("")) { //Make request to upload file from entered Url String guid = service.UploadUrl(url); if (guid != null) { //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) { fileGuId = storageInfo.Files[i].Guid; } } } else { message = "Get files list is failed"; result.Add("error", message); return View("Sample38", null, result); } } //If file wasn't uploaded return error else { result.Add("error", "Something wrong with entered data"); return View("Sample38", null, result); } } //Check is file guid entered if (!fileId.Equals("")) { fileGuId = fileId; } result.Add("fileId", fileGuId); // Generate Embed Annotation url with file GUID string iframe = null; if (basePath.Equals("https://api.groupdocs.com/v2.0")) { iframe = "https://apps.groupdocs.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://dev-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://dev-apps-groupdocs.dynabic.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://stage-api-groupdocs.dynabic.com/v2.0")) { iframe = "https://stage-apps-groupdocs.dynabic.com/document-annotation2/embed/" + fileGuId; } if (basePath.Equals("https://realtime-api.groupdocs.com/v2.0")) { iframe = "https://realtime-apps.groupdocs.com/document-annotation2/embed/" + fileGuId; } string userGuid = null; //Get all users from GroupDocs account Groupdocs.Api.Contract.GetAccountUsersResult allUsers = service.GetAccountUsers(); if (allUsers != null) { //Check is user allready exist in GroupDocs Account for (int i = 0; i < allUsers.Users.Length; i++) { if (email.Equals(allUsers.Users[i].PrimaryEmail)) { //If user exist take his GUID userGuid = allUsers.Users[i].Guid; break; } } //If user not exist create him if (userGuid == null) { //Create User info object Groupdocs.Api.Contract.UserInfo user = new Groupdocs.Api.Contract.UserInfo(); //Create Role info object Groupdocs.Api.Contract.RoleInfo roleInfo = new Groupdocs.Api.Contract.RoleInfo(); //Create array of roles. Groupdocs.Api.Contract.RoleInfo[] roleList = new Groupdocs.Api.Contract.RoleInfo[1]; //Set user role Id. Can be: 1 - SysAdmin, 2 - Admin, 3 - User, 4 - Guest roleInfo.Id = 3; //Set user role name. Can be: SysAdmin, Admin, User, Guest roleInfo.Name = "User"; roleList[0] = roleInfo; //Set nick name as entered first name user.NickName = firstName; //Set first name as entered first name user.FirstName = firstName; //Set last name as entered last name user.LastName = lastName; user.Roles = roleList; //Set email as entered email user.PrimaryEmail = email; //Creating of new User user - object with new user info Groupdocs.Api.Contract.UserIdentity newUser = service.UpdateAccountUser(user); // If request return's null return error to the template if (newUser.Guid != null) { //Get GUID of new user userGuid = newUser.Guid; } else { message = "Failed to create new user"; result.Add("error", message); return View("Sample38", null, result); } } //Get all collaborators for document Groupdocs.Api.Contract.Annotation.GetCollaboratorsResult getCollaborators = service.GetAnnotationCollaborators(fileGuId); if (getCollaborators != null) { //Check is user allready in collaborators for (int n = 0; n < getCollaborators.Collaborators.Length; n++) { //If user allready in collaborators sign iframe URL with his Client ID if (getCollaborators.Collaborators[n].FirstName.Equals(firstName)) { iframe = iframe + "?uid=" + userGuid; iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); break; } } //If user is not in collaborators add him as collaborator if (iframe.Contains("?uid=")) { result.Add("iframe", iframe); return View("Sample38", null, result); } else { // Make request to set annotation collaborators Groupdocs.Api.Contract.Annotation.SetCollaboratorsResult collaborate = service.SetAnnotationCollaborators(fileGuId, collaborators); // Check is request return data if (collaborate != null) { //Sign iframe URL iframe = iframe + "?uid=" + userGuid; iframe = Groupdocs.Security.UrlSignature.Sign(iframe, privateKey); // Return primary email to the template result.Add("collaborator", collaborate.Collaborators[0].PrimaryEmail); //Return iframe URl to the template result.Add("iframe", iframe); return View("Sample38", null, result); } // If request return's null return error to the template else { message = "Failed to add new collaborator"; result.Add("error", message); return View("Sample38", null, result); } } } else { message = "Faild to get all collaborators for document"; result.Add("error", message); return View("Sample38", null, result); } } else { message = "Faild to get all users from account"; result.Add("error", message); return View("Sample38", null, result); } } } // If data not posted return to template for filling of necessary fields else { return View("Sample38"); } }