Ejemplo n.º 1
0
        public static TableauServerConnection SignIn()
        {
            string APIVersion = "";
            string Username   = "";
            string Password   = "";
            string server     = "";
            string siteName   = "";

            var tsConn = new TableauServerConnection(server, siteName, APIVersion, Username, Password);

            if (tsConn.SignIn())
            {
                return(tsConn);
            }
            else
            {
                throw new Exception("Sign in to Tableau Server failed");
            }
        }
        static void GetUserandADGroupsInfo(TableauServerConnection tsConn)
        {
            if (tsConn.SignIn())
            {
                //Get Site ID
                var siteID = GetSiteID(tsConn, tsConn.siteName);

                //Get_Group IDs
                var groups_response = callAPI(tsConn, "/sites/" + siteID + "/groups/");
                var groups          = GenericXMLParser <Group> .ParseResponse(groups_response, "group");

                var filteredGroups = groups.Where(g => g.Name != "All Users").ToList();

                var userInfo_Plus = new List <UserInfo_Plus>();
                foreach (var group in filteredGroups)
                {
                    //Get Users --Use page-size param, default 100,  max 1000 users - if any group has > 1000 then rewrite code
                    var users_response = callAPI(tsConn, "/sites/" + siteID + "/groups/" + group.Id + "/users?pageSize=1000&pageNumber=1");
                    var users          = GenericXMLParser <UserInfo_ViaGroupCall> .ParseResponse(users_response, "user");

                    var query = from user in users
                                select new UserInfo_Plus
                    {
                        Id       = user.Id,
                        Name     = user.Name,
                        SiteRole = user.SiteRole,
                        ADGroup  = @group.Name
                    };
                    var result = query.ToList();
                    userInfo_Plus.AddRange(result);
                }

                //Get UserInfo_Full for entire site
                if (GetPageCount(tsConn, "/sites/" + siteID + "/users?pageSize=1000") > 1)
                {
                    throw new NotImplementedException("Logic not implemented for Sites with >1000 users");
                }
                else
                {
                    var userinfo_Full_response = callAPI(tsConn, "/sites/" + siteID + "/users?pageSize=1000&pageNumber=1");
                    var fullUsers = GenericXMLParser <UserInfo_Full> .ParseResponse(userinfo_Full_response, "user");

                    //Merge CWIDS into MyUser class
                    var query = from uip in userInfo_Plus
                                join fu in fullUsers
                                on uip.Id equals fu.Id
                                select new UserInfo_Plus
                    {
                        Id        = uip.Id,
                        ADGroup   = uip.ADGroup,
                        Name      = uip.Name,
                        SiteRole  = uip.SiteRole,
                        CWID      = uip.Name,
                        LastLogin = fu.LastLogin
                    };

                    var userlist = query.ToList();

                    using (var writer = new StreamWriter("output.csv"))
                        using (var csv = new CsvWriter(writer))
                        {
                            csv.WriteRecords(userlist);
                        }
                }
            }
        }