public IOrganizationService GetOrgService(string orgname)
        {
            try
            {
                var customerCredentials = new CustomerCredentialsHandler().GetCredentials(orgname);
                var discoveryUri        = customerCredentials.discoveryurl; //ConfigurationManager.AppSettings["DiscoveryUri"];

                IServiceManagement <IDiscoveryService> serviceManagement =
                    ServiceConfigurationFactory.CreateManagement <IDiscoveryService>(
                        new Uri(discoveryUri));
                AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;

                // Set the credentials.
                AuthenticationCredentials authCredentials = GetCredentials(serviceManagement, endpointType, customerCredentials);

                Uri serviceUri = new Uri(customerCredentials.orgurl); //ConfigurationSettings.AppSettings["OrganizationUri"]);

                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, authCredentials.ClientCredentials, null);
                proxy.EnableProxyTypes();
                IOrganizationService service = (IOrganizationService)proxy;

                return(service);
            }
            catch (SoapException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Saving documents into sharepoint online
        /// </summary>
        /// <param name="regardingRef"></param>
        /// <param name="resulturi"></param>
        /// <param name="docName"></param>
        /// <param name="service"></param>
        internal void SaveInSP(EntityReference regardingRef, string resulturi, string docName, string orgname, int lcid, IOrganizationService service)
        {
            try
            {
                folderRelativeUrl = "";
                SharePointSite    = null;
                var crmconfig  = new CallBackCrmHandler().GetCRMConfig(service);
                var spUsername = crmconfig.SPUser;
                var spPassword = crmconfig.SPpassword;

                if (spPassword == null || spPassword == null)
                {
                    var customerCredentials = new CustomerCredentialsHandler().GetCredentials(orgname);
                    spUsername = customerCredentials.username;
                    spPassword = customerCredentials.password;
                }

                var pdfBytes = new CallBackSignicatHandler().ReadAsyncFile(resulturi);
                var currentSharePointFolderEntity = GetDocumentLocation(regardingRef, "regardingobjectid", service);
                var sharePointUrl = GetDefaultSPSiteUrlFromCRMSiteCollectionEntity(service);

                HandleRelativeUrl(currentSharePointFolderEntity, service);

                var urlSplit   = folderRelativeUrl.Split('/').ToList();
                var listTitle  = urlSplit.First();
                var folderName = urlSplit[1];
                docName = CleanName(docName);

                using (ClientContext clientContext = new ClientContext(sharePointUrl))
                {
                    SecureString passWord = new SecureString();
                    foreach (char c in spPassword.ToCharArray())
                    {
                        passWord.AppendChar(c);
                    }
                    clientContext.Credentials = new SharePointOnlineCredentials(spUsername, passWord);
                    Web web = clientContext.Web;
                    clientContext.Load(web);
                    clientContext.Load(web.Lists);
                    clientContext.ExecuteQuery();

                    MemoryStream mStream = new MemoryStream();
                    mStream.Write(pdfBytes.Result.file, 0, pdfBytes.Result.file.Length);

                    FileCreationInformation flciNewFile = new FileCreationInformation();
                    //using ContentStream property for large files
                    mStream.Seek(0, SeekOrigin.Begin);
                    flciNewFile.ContentStream = mStream;
                    flciNewFile.Url           = sharePointUrl + "/" + folderRelativeUrl + docName + ".pdf";
                    flciNewFile.Overwrite     = true;

                    List accountList = GetAccountList(listTitle, web, clientContext, crmconfig, service);
                    if (accountList == null)
                    {
                        return;
                    }

                    var folder = accountList.RootFolder.Folders.GetByUrl(folderName);

                    var uploadFile = folder.Files.Add(flciNewFile);

                    clientContext.Load(uploadFile);
                    clientContext.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
                return;

                throw new Exception(ex.Message);
            }
        }