使用 C# 集成在 Microsoft navision 2018 中创建用户

问题描述 投票:0回答:0

有谁知道如何使用 C# 在 Nav 2018 中创建用户? 我已经尝试使用“Odata”端点,但我只设法公开 getUser,当我尝试发布时它不起作用。 我也尝试通过 powershell 但失败了。有人经历过这个吗?

尝试这样 我在 Navision Client 上创建的端点“/users”

string serviceUrl = "https://navision01baseurl:port/dynamicsnav110/OData/Company('CampanyName')/users";

string soapHeader = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                <soap:Body>";

string soapBody = @"<Create xmlns=""urn:microsoft-dynamics-schemas/codeunit/users"">
                              <FirstName>Name</FirstName>
                              <LastName>Last Name</LastName>
                              <UserName>name.user</UserName>
                              <Password>pass@123</Password>
                            </Create>";

string soapFooter = @"</soap:Body>
                        </soap:Envelope>";

string soapMessage = soapHeader + soapBody + soapFooter;

using (var httpClient = new HttpClient())
{


    string username = "navUser";
    string password = "navUserPassword";
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);

    try
    {
        var request = new HttpRequestMessage(HttpMethod.Post, serviceUrl)
        {
            Content = new StringContent(soapMessage, Encoding.UTF8, "application/soap+xml")
        };
    
        var response = await httpClient.SendAsync(request);

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Console.ReadLine();

像这样:

var client = new HttpClient();

 string username = "navUser";
 string password = "navUserPassword";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));


// Set the authorization header.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

var user = new
{
    UserName = "JohnDoe",
    Password = "password123",
    FirstName = "John",
    LastName = "Doe",
    Email = "[email protected]",
    Department = "Sales",
    Role = "Sales Representative"
};

var json = JsonConvert.SerializeObject(user);

var response = await client.PostAsync("https://navision01baseurl:port/dynamicsnav110/OData/Company('CampanyName')/UserSetup", new StringContent(json, Encoding.UTF8, "application/json"));

if (response.StatusCode == HttpStatusCode.Created)
{
    var userResponse = JsonConvert.DeserializeObject<UserResponse>(response.Content.ReadAsStringAsync().Result);
    Console.WriteLine("User created successfully.");
    Console.WriteLine("User ID: {0}", userResponse.Id);
}
else
{
    Console.WriteLine("Error creating user: {0}", response.StatusCode);
}
c# navision
© www.soinside.com 2019 - 2024. All rights reserved.