Thursday, November 09, 2017

Using C#.Net DocuSign REST API get Account ID in Web App

I was working on integrating DocuSign data into my Web App. The requirement was pretty simple, for a given account just display the documents which are awaiting for signature by others. First step was to get the account id(s) of the Admin account of DocuSign.

I was searching a solution on net and found it very difficult to gather the information and working code.

Here is the code which works well and tested in console application. I am sure you can use this code display in the data in web App also. you can add DocuSign package using NuGet in your solution.




Note: You just need to add DocuSign.eSign dll with strong key if using this code in Web App or SharePoint.

Use Integrator Key and BasePath if you are using Demo account. in my case i used Demo account. For Production purpose you need not have to mentioned this basePath.



        static void Main(string[] args)
        {
            loginApi(userName, password);
        }

        static public string loginApi(string usr, string pwd)
        {
            // we set the api client in global config when we configured the client
            ApiClient client = new ApiClient(basePath: "https://demo.docusign.net/restapi");
            Configuration cfg = new Configuration(client);
            //ApiClient apiClient = Configuration.Default.ApiClient;
            string authHeader = "{\"Username\":\"" + usr + "\", \"Password\":\"" + pwd + "\", \"IntegratorKey\":\"" + integratorKey + "\"}";
            //Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);
            cfg.AddDefaultHeader("X-DocuSign-Authentication", authHeader);

// we will retrieve this from the login() results
            string accountId = null;

            // the authentication api uses the apiClient (and X-DocuSign-Authentication header) that are set in Configuration object
            //AuthenticationApi authApi = new AuthenticationApi();
            AuthenticationApi authApi = new AuthenticationApi(cfg);
            LoginInformation loginInfo = authApi.Login();

            // find the default account for this user
            foreach (LoginAccount loginAcct in loginInfo.LoginAccounts)
            {
                if (loginAcct.IsDefault == "true")
                {
                    accountId = loginAcct.AccountId;
                    break;
                }
            }
            if (accountId == null)
            { // if no default found set to first account
                accountId = loginInfo.LoginAccounts[0].AccountId;
            }

            return accountId;
        }


            

No comments: