/// <summary> /// Initial set up for the application. We read the config file and create the instance of Oauth and addressbook object. /// If it's post back from getting Auth code then perform operations. /// </summary> protected void Page_Load(object sender, EventArgs e) { ReadConfigFile(); oauth = new OAuth(endPoint, scope, apiKey, secretKey, authorizeRedirectUri, refreshTokenExpiresIn, bypassSSL); this.serializer = new JavaScriptSerializer(); if (Session["cs_rest_AccessToken"] != null && this.addressbook == null) { this.addressbook = new AddressBook(this.endPoint, Session["cs_rest_AccessToken"].ToString()); } if ((string)Session["cs_rest_appState"] == "GetToken" && Request["Code"] != null) { this.oauth.authCode = Request["code"].ToString(); try { if (oauth.GetAccessToken(OAuth.AccessTokenType.AuthorizationCode) == true) { StoreAccessTokenToSession(oauth.accessTokenJson); this.addressbook = new AddressBook(this.endPoint, this.accessToken); Operation operation = (Operation)Session["cs_rest_ServiceRequest"]; switch (operation) { case Operation.CreateContactOperation: showCreateContact = "true"; try { if (null == addressbook.createContact(Session["JSONstring"].ToString())) { this.contact_error = this.addressbook.apiService.errorResponse; } else { this.create_contact = new Success(); this.create_contact.location = this.addressbook.apiService.apiResponse.location; } } catch (Exception ex) { this.contact_error = ex.Message; } break; case Operation.UpdateContactOperation: showUpdateContact = "true"; try { if (!addressbook.updateContact(Session["contactid"].ToString(), Session["JSONstring"].ToString())) { this.contact_error = this.addressbook.apiService.errorResponse; } else { this.success_contact = new Success(); this.success_contact.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.contact_error = ex.Message; } break; case Operation.DeleteContactOperation: showDeleteContact = "true"; try { if (!addressbook.deleteContact(Session["contactid"].ToString())) { this.contact_error = this.addressbook.apiService.errorResponse; } else { this.success_contact = new Success(); this.success_contact.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.contact_error = ex.Message; } break; case Operation.GetContactsOperation: showGetContacts = "true"; try { this.qContactResult = addressbook.getContacts(Session["querystring"].ToString()); if (null == this.qContactResult) { this.contact_error = this.addressbook.apiService.errorResponse; } //else //{ //try //{ //this.qContactResult = serializer.Deserialize<QuickContactObj.RootObject>(addressbook.JSONstring); // this.qContactResult = addressbook.getContacts(Session["querystring"].ToString()); //} //catch (Exception ex) //{ // this.contact_error = ex.Message; //} //} } catch (Exception ex) { this.contact_error = ex.Message; } break; case Operation.UpdateMyInfoOperation: showUpdateMyInfo = "true"; try { if (!addressbook.updateMyInfo(Session["JSONstring"].ToString())) { this.myinfo_error = this.addressbook.apiService.errorResponse; } else { this.update_myinfo = new Success(); this.update_myinfo.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.myinfo_error = ex.Message; } break; case Operation.GetMyInfoOperation: showGetMyInfo = "true"; try { string result = addressbook.getMyInfo(); if (null == result) { this.myinfo_error = this.addressbook.apiService.errorResponse; } else { //try //{ this.myInfoResult = serializer.Deserialize<ContactObj.RootObject>(result); //} } } catch (Exception ex) { this.myinfo_error = ex.Message; } break; case Operation.CreateGroupOperation: showCreateGroup = "true"; try { if (null == addressbook.createGroup(Session["JSONstring"].ToString())) { this.group_error = this.addressbook.apiService.errorResponse; } else { this.create_group = new Success(); this.create_group.location = this.addressbook.apiService.apiResponse.location; } } catch (Exception ex) { this.group_error = ex.Message; } break; case Operation.UpdateGroupOperation: showUpdateGroup = "true"; try { if (!addressbook.updateGroup(Session["groupid"].ToString(), Session["JSONstring"].ToString())) { this.group_error = this.addressbook.apiService.errorResponse; } else { this.success_group = new Success(); this.success_group.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.group_error = ex.Message; } break; case Operation.DeleteGroupOperation: showDeleteGroup = "true"; if (!addressbook.deleteGroup(Session["groupid"].ToString())) { this.group_error = this.addressbook.apiService.errorResponse; } else { this.success_group = new Success(); this.success_group.last_modified = this.addressbook.apiService.apiResponse.lastModified; } break; case Operation.GetGroupsOperation: showGetGroups = "true"; try { this.groupResult = addressbook.getGroups(Session["querystring"].ToString()); if (null == this.groupResult) { this.group_error = this.addressbook.apiService.errorResponse; } //else //{ // try //{ //this.groupResult = serializer.Deserialize<GroupObj.RootObject>(addressbook.JSONstring); //} //} } catch (Exception ex) { this.group_error = ex.Message; } break; case Operation.GetGroupContactsOperation: showGetGroupContacts = "true"; try { String result = addressbook.getGroupContacts(Session["groupid"].ToString()); if (null == result) { this.manage_groups_error = this.addressbook.apiService.errorResponse; } //else //{ // try //{ //} this.contactIdResult = serializer.Deserialize<ContactIdObj.RootObject>(result); } catch (Exception ex) { this.manage_groups_error = ex.Message; } break; case Operation.AddContctsToGroupOperation: showAddContactsToGroup = "true"; try { if (!addressbook.addContactToGroup(Session["groupid"].ToString(), Session["contactids"].ToString())) { this.manage_groups_error = this.addressbook.apiService.errorResponse; } else { this.manage_groups = new Success(); this.manage_groups.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.manage_groups_error = ex.Message; } break; case Operation.RemoveContactsFromGroupOperation: showRemoveContactsfrmGroup = "true"; try { if (!addressbook.removeContactsFromGroup(Session["groupid"].ToString(), Session["contactids"].ToString())) { this.manage_groups_error = this.addressbook.apiService.errorResponse; } else { this.manage_groups = new Success(); this.manage_groups.last_modified = this.addressbook.apiService.apiResponse.lastModified; } } catch (Exception ex) { this.manage_groups_error = ex.Message; } break; case Operation.GetContactGroupsOperation: showGetContactGroups = "true"; try { String result = addressbook.getContactGroups(Session["contactid"].ToString()); if (null == result) { this.manage_groups_error = this.addressbook.apiService.errorResponse; } else { // try // { //this.contactGroupResult = serializer.Deserialize<GroupObj.RootObject>(result); //} //} this.contactGroupResult = serializer.Deserialize<GroupObj.RootObject>(result); } } catch (Exception ex) { this.manage_groups_error = ex.Message; } break; } ResetRequestSessionVariables(operation); } else { if (oauth.getAuthCodeError != null) { this.oauth_error = "GetAuthCodeError: " + oauth.getAuthCodeError; } if (oauth.GetAccessTokenError != null) { this.oauth_error = "GetAccessTokenError: " + oauth.GetAccessTokenError; } this.ResetTokenSessionVariables(); return; } } catch (Exception ex) { this.oauth_error = "GetAuthCodeError: " + ex.Message; this.ResetTokenSessionVariables(); } } }
private string getContactJSON(Operation operation) { int maxNumberOfEntries = 3; string[] pPref = new String[maxNumberOfEntries]; string[] ptype = new String[maxNumberOfEntries]; string[] pnumber = new String[maxNumberOfEntries]; if (operation == Operation.CreateContactOperation) { pPref = getValueFromRequest("createPhonePref"); ptype = getValueFromRequest("createPhoneType"); pnumber = getValueFromRequest("createPhoneNumber"); } else if (operation == Operation.UpdateContactOperation) { pPref = getValueFromRequest("updatePhonePref"); ptype = getValueFromRequest("updatePhoneType"); pnumber = getValueFromRequest("updatePhoneNumber"); } else if (operation == Operation.UpdateMyInfoOperation) { pPref = getValueFromRequest("myInfoPhonePref"); ptype = getValueFromRequest("myInfoPhoneType"); pnumber = getValueFromRequest("myInfoPhoneNumber"); } string[] phone_list = new String[ptype.Length]; if (ptype.Length != pnumber.Length) { throw new Exception("Number of fields does not match the number of values"); } var phone = new List<ContactObj.Phone>(); for (int i = 0; i < ptype.Length; i++) { bool pref = (pPref[i].Equals("True")) ? true : false; phone.Add(new ContactObj.Phone() { type = ptype[i], number = pnumber[i], preferred = pref }); } var phones = new ContactObj.Phones(); phones.phone = phone; var empty_phone = new List<ContactObj.Phone>(); empty_phone.Add(new ContactObj.Phone() { type = string.Empty, number = string.Empty, preferred = true }); var empty_phones = new ContactObj.Phones(); empty_phones.phone = empty_phone; string[] im_type = new String[maxNumberOfEntries]; string[] im_im = new String[maxNumberOfEntries]; string[] im_pref = new String[maxNumberOfEntries]; if (operation == Operation.CreateContactOperation) { im_pref = getValueFromRequest("createIMPref"); im_type = getValueFromRequest("createIMType"); im_im = getValueFromRequest("createIMUri"); } else if (operation == Operation.UpdateContactOperation) { im_pref = getValueFromRequest("updateIMPref"); im_type = getValueFromRequest("updateIMType"); im_im = getValueFromRequest("updateIMUri"); } else if (operation == Operation.UpdateMyInfoOperation) { im_pref = getValueFromRequest("myInfoIMPref"); im_type = getValueFromRequest("myInfoIMType"); im_im = getValueFromRequest("myInfoIMUri"); } string[] im_list = new String[im_type.Length]; if (im_type.Length != im_im.Length) { throw new Exception("Number of fields does not match the number of values"); } var im = new List<ContactObj.Im>(); for (int i = 0; i < im_type.Length; i++) { bool pref = (im_pref[i].Equals("True")) ? true : false; im.Add(new ContactObj.Im() { type = im_type[i], imUri = im_im[i], preferred = pref }); }; var ims = new ContactObj.Ims(); ims.im = im; var empty_im = new List<ContactObj.Im>(); empty_im.Add(new ContactObj.Im() { type = string.Empty, imUri = string.Empty, preferred = true }); var empty_ims = new ContactObj.Ims(); empty_ims.im = empty_im; string[] email_pref = new String[maxNumberOfEntries]; string[] email_type = new String[maxNumberOfEntries]; string[] email_email = new String[maxNumberOfEntries]; if (operation == Operation.CreateContactOperation) { email_pref = getValueFromRequest("createEmailPref"); email_type = getValueFromRequest("createEmailType"); email_email = getValueFromRequest("createEmailAddress"); } else if (operation == Operation.UpdateContactOperation) { email_pref = getValueFromRequest("updateEmailPref"); email_type = getValueFromRequest("updateEmailType"); email_email = getValueFromRequest("updateEmailAddress"); } else if (operation == Operation.UpdateMyInfoOperation) { email_pref = getValueFromRequest("myInfoEmailPref"); email_type = getValueFromRequest("myInfoEmailType"); email_email = getValueFromRequest("myInfoEmailAddress"); } string[] email_list = new String[email_type.Length]; if (email_type.Length != email_email.Length) { throw new Exception("Number of fields does not match the number of values"); } var email = new List<ContactObj.Email>(); for (int i = 0; i < email_type.Length; i++) { bool pref = (email_pref[i].Equals("True")) ? true : false; email.Add(new ContactObj.Email() { type = email_type[i], emailAddress = email_email[i], preferred = pref }); } var emails = new ContactObj.Emails(); emails.email = email; var empty_email = new List<ContactObj.Email>(); empty_email.Add(new ContactObj.Email() { type = string.Empty, emailAddress = string.Empty, preferred = true }); var empty_emails = new ContactObj.Emails(); empty_emails.email = empty_email; string[] weburl_type = new String[maxNumberOfEntries]; string[] weburl_weburl = new String[maxNumberOfEntries]; string[] weburl_pref = new String[maxNumberOfEntries]; if (operation == Operation.CreateContactOperation) { weburl_pref = getValueFromRequest("createWeburlPref"); weburl_type = getValueFromRequest("createWeburlType"); weburl_weburl = getValueFromRequest("createWeburl"); } else if (operation == Operation.UpdateContactOperation) { weburl_pref = getValueFromRequest("updateWeburlPref"); weburl_type = getValueFromRequest("updateWeburlType"); weburl_weburl = getValueFromRequest("updateWeburl"); } else if (operation == Operation.UpdateMyInfoOperation) { weburl_pref = getValueFromRequest("myInfoWeburlPref"); weburl_type = getValueFromRequest("myInfoWeburlType"); weburl_weburl = getValueFromRequest("myInfoWeburl"); } string[] weburl_list = new String[weburl_type.Length]; if (weburl_type.Length != weburl_weburl.Length) { throw new Exception("Number of fields does not match the number of values"); } var weburl = new List<ContactObj.WebUrl>(); for (int i = 0; i < weburl_type.Length; i++) { bool pref = (weburl_pref[i].Equals("True")) ? true : false; weburl.Add(new ContactObj.WebUrl() { type = weburl_type[i], url = weburl_weburl[i], preferred = pref }); } var weburls = new ContactObj.Weburls(); weburls.webUrl = weburl; var empty_weburl = new List<ContactObj.WebUrl>(); empty_weburl.Add(new ContactObj.WebUrl() { type = string.Empty, url = string.Empty, preferred = true }); var empty_weburls = new ContactObj.Weburls(); empty_weburls.webUrl = empty_weburl; //address string[] address_pref = new String[maxNumberOfEntries]; string[] atype = new String[maxNumberOfEntries]; string[] apoBox = new String[maxNumberOfEntries]; string[] aaddressLine1 = new String[maxNumberOfEntries]; string[] aaddressLine2 = new String[maxNumberOfEntries]; string[] acity = new String[maxNumberOfEntries]; string[] astate = new String[maxNumberOfEntries]; string[] azip = new String[maxNumberOfEntries]; string[] acountry = new String[maxNumberOfEntries]; if (operation == Operation.CreateContactOperation) { address_pref = getValueFromRequest("createAddressPref"); atype = getValueFromRequest("createAddressType"); apoBox = getValueFromRequest("createAddressPoBox"); aaddressLine1 = getValueFromRequest("createAddressLineOne1"); aaddressLine2 = getValueFromRequest("createAddressLineTwo"); acity = getValueFromRequest("createAddressCity"); astate = getValueFromRequest("createAddressState"); azip = getValueFromRequest("createAddressZip"); acountry = getValueFromRequest("createAddressCountry"); } else if (operation == Operation.UpdateContactOperation) { address_pref = getValueFromRequest("updateAddressPref"); atype = getValueFromRequest("updateAddressType"); apoBox = getValueFromRequest("updateAddressPoBox"); aaddressLine1 = getValueFromRequest("updateAddressLineOne1"); aaddressLine2 = getValueFromRequest("updateAddressLineTwo"); acity = getValueFromRequest("updateAddressCity"); astate = getValueFromRequest("updateAddressState"); azip = getValueFromRequest("updateAddressZip"); acountry = getValueFromRequest("updateAddressCountry"); } else if (operation == Operation.UpdateMyInfoOperation) { address_pref = getValueFromRequest("myInfoAddressPref"); atype = getValueFromRequest("myInfoAddressType"); apoBox = getValueFromRequest("myInfoAddressPoBox"); aaddressLine1 = getValueFromRequest("myInfoAddressLineOne1"); aaddressLine2 = getValueFromRequest("myInfoAddressLineTwo"); acity = getValueFromRequest("myInfoAddressCity"); astate = getValueFromRequest("myInfoAddressState"); azip = getValueFromRequest("myInfoAddressZip"); acountry = getValueFromRequest("myInfoAddressCountry"); } int entryCount = atype.Length; string[] address_list = new String[entryCount]; var address = new List<ContactObj.Address>(); for (int i = 0; i < entryCount; i++) { bool pref = (address_pref[i].Equals("True")) ? true : false; address.Add(new ContactObj.Address() { type = atype[i], poBox = apoBox[i], preferred = pref, addressLine1 = aaddressLine1[i], addressLine2 = aaddressLine2[i], city = acity[i], state = astate[i], zipcode = azip[i], country = acountry[i] }); } var addresses = new ContactObj.Addresses(); addresses.address = address; var empty_address = new List<ContactObj.Address>(); empty_address.Add(new ContactObj.Address() { type = string.Empty, poBox = string.Empty, preferred = true, addressLine1 = string.Empty, addressLine2 = string.Empty, city = string.Empty, state = string.Empty, zipcode = string.Empty, country = string.Empty }); var empty_addresses = new ContactObj.Addresses(); empty_addresses.address = empty_address; //var contact = new List<ContactJSON.Contact>(); var contact_obj = new ContactObj.Contact(); var photo = new ContactObj.Photo(); photo.encoding = "BASE64"; var empty_photo = new ContactObj.Photo(); empty_photo.encoding = "BASE64"; empty_photo.value = ""; if (JsonConvert.SerializeObject(phones) != JsonConvert.SerializeObject(empty_phones)) { contact_obj.phones = phones; } if (JsonConvert.SerializeObject(ims) != JsonConvert.SerializeObject(empty_ims)) { contact_obj.ims = ims; } if (JsonConvert.SerializeObject(addresses) != JsonConvert.SerializeObject(empty_addresses)) { contact_obj.addresses = addresses; } if (JsonConvert.SerializeObject(emails) != JsonConvert.SerializeObject(empty_emails)) { contact_obj.emails = emails; } if (JsonConvert.SerializeObject(weburls) != JsonConvert.SerializeObject(empty_weburls)) { contact_obj.weburls = weburls; } if (JsonConvert.SerializeObject(photo) != JsonConvert.SerializeObject(empty_photo)) { contact_obj.photo = photo; } var contactRoot = new ContactObj.RootObject(); if (operation == Operation.CreateContactOperation) { string imgString = getImageto64base(attachPhoto.Value); photo.value = imgString; if (firstName.Text != "") { contact_obj.firstName = firstName.Text; } if (lastName.Text != "") { contact_obj.lastName = lastName.Text; } if (prefix.Text != "") { contact_obj.prefix = prefix.Text; } if (suffix.Text != "") { contact_obj.suffix = suffix.Text; } if (nickname.Text != "") { contact_obj.nickName = nickname.Text; } if (organization.Text != "") { contact_obj.organization = organization.Text; } if (jobTitle.Text != "") { contact_obj.jobTitle = jobTitle.Text; } if (anniversary.Text != "") { contact_obj.anniversary = anniversary.Text; } if (gender.Text != "") { contact_obj.gender = gender.Text; } if (hobby.Text != "") { contact_obj.hobby = hobby.Text; } if (assistant.Text != "") { contact_obj.assistant = assistant.Text; } contactRoot.contact = contact_obj; } else if (operation == Operation.UpdateContactOperation) { string imgString = getImageto64base(attachPhotoUpd.Value); photo.value = imgString; //if (formattedNameUpd.Text != "") //{ contact_obj.formattedName = ""; //} if (firstNameUpd.Text != "") { contact_obj.firstName = firstNameUpd.Text; } if (lastNameUpd.Text != "") { contact_obj.lastName = lastNameUpd.Text; } if (prefixUpd.Text != "") { contact_obj.prefix = prefixUpd.Text; } if (suffixUpd.Text != "") { contact_obj.suffix = suffixUpd.Text; } if (nicknameUpd.Text != "") { contact_obj.nickName = nicknameUpd.Text; } if (organizationUpd.Text != "") { contact_obj.organization = organizationUpd.Text; } if (jobTitleUpd.Text != "") { contact_obj.jobTitle = jobTitleUpd.Text; } if (anniversaryUpd.Text != "") { contact_obj.anniversary = anniversaryUpd.Text; } if (genderUpd.Text != "") { contact_obj.gender = genderUpd.Text; } if (hobbyUpd.Text != "") { contact_obj.hobby = hobbyUpd.Text; } if (assistantUpd.Text != "") { contact_obj.assistant = assistantUpd.Text; } contactRoot.contact = contact_obj; } else if (operation == Operation.UpdateMyInfoOperation) { string imgString = getImageto64base(attachPhotoMyInf.Value); photo.value = imgString; if (firstNameMyInf.Text != "") { contact_obj.firstName = firstNameMyInf.Text; } if (lastNameMyInf.Text != "") { contact_obj.lastName = lastNameMyInf.Text; } if (prefixMyInf.Text != "") { contact_obj.prefix = prefixMyInf.Text; } if (suffixMyInf.Text != "") { contact_obj.suffix = suffixMyInf.Text; } if (nicknameMyInf.Text != "") { contact_obj.nickName = nicknameMyInf.Text; } if (organizationMyInf.Text != "") { contact_obj.organization = organizationMyInf.Text; } if (jobTitleMyInf.Text != "") { contact_obj.jobTitle = jobTitleMyInf.Text; } if (anniversaryMyInf.Text != "") { contact_obj.anniversary = anniversaryMyInf.Text; } if (genderMyInf.Text != "") { contact_obj.gender = genderMyInf.Text; } if (hobbyMyInf.Text != "") { contact_obj.hobby = hobbyMyInf.Text; } if (assistantMyInf.Text != "") { contact_obj.assistant = assistantMyInf.Text; } contactRoot.myInfo = contact_obj; } return JsonConvert.SerializeObject(contactRoot, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); }
public void getMyInfo_Click(object sender, EventArgs e) { showGetMyInfo = "true"; checkAccessToken(Operation.GetMyInfoOperation); try { string result = addressbook.getMyInfo(); if (null == result) { this.myinfo_error = this.addressbook.apiService.errorResponse; } else { this.myInfoResult = serializer.Deserialize<ContactObj.RootObject>(result); } } catch (Exception ex) { this.myinfo_error = ex.Message; } }