示例#1
0
        private string _CreateUser(string name, string fullName, string siteRole, string authSetting)
        {
            //ref: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_usersgroups.htm#add_user_to_site
            var sb          = new StringBuilder();
            var xmlSettings = new XmlWriterSettings();

            xmlSettings.OmitXmlDeclaration = true;
            var xmlWriter = XmlWriter.Create(sb, xmlSettings);

            xmlWriter.WriteStartElement("tsRequest");
            xmlWriter.WriteStartElement("user");
            xmlWriter.WriteAttributeString("name", name);
            xmlWriter.WriteAttributeString("fullName", fullName);
            xmlWriter.WriteAttributeString("siteRole", siteRole);
            xmlWriter.WriteAttributeString("authSetting", authSetting);
            xmlWriter.WriteEndElement(); //</user>
            xmlWriter.WriteEndElement(); //</tsRequest>
            xmlWriter.Close();

            var xmlText = sb.ToString(); //Get the XML text out

            //Create a web request
            var urlCreateUser = _onlineUrls.Url_CreateUser(OnlineSession);
            //var urlCreateUser = "******";
            var webRequest = CreateLoggedInWebRequest(urlCreateUser, "POST");

            SendRequestContents(webRequest, xmlText);

            //Get the response
            var    response      = GetWebResponseLogErrors(webRequest, "create user");
            var    location      = response.Headers.Get("Location");
            string split         = "users/";
            string tableauUserId = location.Substring(location.IndexOf(split) + split.Length);

            return(tableauUserId);
        }