Share via

Trouble adding SSRS Resource

Harvey Kravis 96 Reputation points
2021-11-10T01:47:56.11+00:00

I am trying to provide a user interface that allows the user to add resource files to SSRS in an ASP.NET Core application. The user cannot manipulate the existing folder structure, only upload files. I can use the REST api to retrieve the folder structure, but when I go to post a new resource I get a 400 error response, Bad Request. Apparently this means my json is malformed but I can't figure out what's wrong. I wish the examples in https://app.swaggerhub.com/apis/microsoft-rs/SSRS/2.0#/Resources/AddResource were a little more robust.

Here is the class I use:

public class SSRSFile
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Path { get; set; }
    public string Type { get; set; }
    public bool Hidden { get; set; }
    public Int64 Size { get; set; }
    public string ModifiedBy { get; set; }
    public string ModifiedDate { get; set; }
    public string CreatedBy { get; set; }
    public string CreatedDate { get; set; }
    public string ParentFolderId { get; set; }
    public string ContentType { get; set; }
    public string Content { get; set; }
    public bool IsFavorite { get; set; }
}

Here is the method I am using to post:

public async Task<IActionResult> ImportToSSRSAsync(
      string Name
    , string Path
    , string ParentFolderId
    , Int64 Size
    , string ModifiedBy
    , string ModifiedDate
    , string Content
    )
{
    string SSRSUserName = HttpContext.Session.GetString("SSRSUserName");

    string SSRSPassword = HttpContext.Session.GetString("SSRSPassword");

    string SSRSURL = HttpContext.Session.GetString("SSRSURL");

    var credentials = new NetworkCredential(SSRSUserName, SSRSPassword);

    var client = new HttpClient(new HttpClientHandler() { Credentials = credentials });

    SSRSFile newImportFile = new SSRSFile
    {
        Name = Name,
        Path = Path,
        Id = Guid.NewGuid().ToString(),
        Description = "none",
        Type = "Resource",
        Hidden = false,
        Size = 0,
        ModifiedBy = ModifiedBy,
        ModifiedDate = ModifiedDate,
        CreatedBy = ModifiedBy,
        CreatedDate = ModifiedDate,
        ParentFolderId = ParentFolderId,
        ContentType = "text/plain",
        Content = Content,
        IsFavorite = false
    };

    try
    {
        var importFile = System.Text.Json.JsonSerializer.Serialize(newImportFile);
        HttpContent requestContent = new StringContent(importFile, Encoding.UTF8, "application/json");
        var uri = SSRSURL + "/Resources";
        var response = await client.PostAsync(uri, requestContent).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
    }
    catch (Exception e)
    {
        throw new Exception("An error occurred importing to SSRS: " + e.Message + ". ||| ");
    }

And this is the code that calls the method in my view:

    var text1 = "abcdef";
    await $.get("ABIReportsSSRS/ImportToSSRS", { Name: name, Path: fullpath, ParentFolderId: parentfolderid, Size: 0, ModifiedBy: "hlkra", ModifiedDate: "2017-04-13T15:51:04Z", Content: text1 }, function (data) {
        location.href = "ABIReportsSSRS";
    })

Some values are hardcoded for debugging purposes.

I know my uri is correct because it runs in a browser. I've checked and double-checked the data getting passed, and the names of the variables.

Any help would be appreciated.

SQL Server Reporting Services
SQL Server Reporting Services

A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.

Developer technologies | ASP.NET Core | Other

Answer accepted by question author

Harvey Kravis 96 Reputation points
2021-12-09T01:00:47.433+00:00

I was able to figure that the problem had to do with the content I was passing. I was passing a plain ascii string but it needs to be converted to a byte array and then a base64url string.

        byte[] array = Encoding.UTF8.GetBytes(fileinfo1.Content);
        string con = Convert.ToBase64String(array);

Where fileinfo1.Content contains the text in the file.

So it is now working for text files up to around 3MB. I might need to do some chunking to accommodate larger files, but that's a problem for another day.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.