private ClientContext GetClientContext(string location, bool isLibrarySubmit = false) { var appInstance = SqlCredentialManager.GetMatchingInstanceByUrl(location); var isValidInstance = appInstance != null && !String.IsNullOrWhiteSpace(appInstance.Username) && !String.IsNullOrWhiteSpace(appInstance.Password); var webUrl = isLibrarySubmit ? GetWebUrl(location) : location; if (!isValidInstance) { return(SpManager.GetSharePointContext(HttpContext, webUrl)); } var credentials = GetCredentials(webUrl, appInstance); var clientContext = new ClientContext(webUrl); clientContext.Credentials = credentials; return(clientContext); }
private ICredentials GetCredentials(string spUrl) { var appInstance = SqlCredentialManager.GetMatchingInstanceByUrl(spUrl); var isValidInstance = appInstance != null && !String.IsNullOrWhiteSpace(appInstance.Username) && !String.IsNullOrWhiteSpace(appInstance.Password); if (!isValidInstance) { return(null); } return(GetCredentials(spUrl, appInstance)); }
public ActionResult CallRestService(string url) { var success = false; var message = string.Empty; var resultBody = string.Empty; if (!String.IsNullOrWhiteSpace(url)) { var appInstance = SqlCredentialManager.GetMatchingInstanceByUrl(url); var isCredentialsExist = appInstance != null && !String.IsNullOrWhiteSpace(appInstance.Username) && !String.IsNullOrWhiteSpace(appInstance.Password); using (var webClient = new WebClient()) { try { if (isCredentialsExist) { webClient.Credentials = new NetworkCredential(appInstance.Username, appInstance.Password, appInstance.Domain); } resultBody = webClient.DownloadStringDetectEncoding(url); success = true; } catch (Exception ex) { message = String.Format("{0} : {1}", ex.GetType().ToString(), ex.Message); } } } else { message = "Url cannot be blank."; } var restResult = new RestResult { Message = message, ResultBody = resultBody, Success = success }; return(Json(restResult, JsonRequestBehavior.AllowGet)); }