Luciano Valinho
Luciano Valinho

Reputation: 59

Sensenet: Upload Files through Sensenet Client API and Set Modified User

I have a requirement that consists on uploading files through other system to sensenet.

I'm trying to use the Sensenet Client API to upload files but I'm having difficult using the examples documented on the follow links: Client Library (the code runs well but the file doesn't appear on Sensenet) Common API Calls (I'm having trouble to compile the code... to instantiate the BinaryData object)

Beside this, I need for each uploading file define the "Modified By" that I specify in my code and not the user that I use to authenticate me in the API.

Upvotes: 0

Views: 115

Answers (1)

Zoltan Gyebrovszki
Zoltan Gyebrovszki

Reputation: 243

I think rewriting the ModifiedBy field is an edge case (or a small hack) but it is possible without any magic (see the code). The easiest way is a POST followed by a PATCH, that is perfectly managed by the SenseNet.Client (the code uses a local demo site):

    static void Main(string[] args)
    {
        ClientContext.Initialize(new[]
            {new ServerContext {Url = "http://localhost", Username = "admin", Password = "admin"}});

        var localFilePath = @"D:\Projects\ConsoleApplication70\TestFileFromConsole1.txt";
        var parentPath = "/Root/Sites/Default_Site/workspaces/Document/londondocumentworkspace/Document_Library";
        var fileName = "TestFileFromConsole1.txt";
        var path = parentPath + "/" + fileName;
        var userPath = "/Root/IMS/BuiltIn/Demo/ProjectManagers/alba";

        using (var stream = new FileStream(localFilePath, FileMode.Open))
            Content.UploadAsync(parentPath, fileName, stream).Wait();
        Console.WriteLine("Uploaded");

        Modify(path, userPath).Wait();
        Console.WriteLine("Modified");

        Console.Write("Press <enter> to exit...");
        Console.ReadLine();
    }

    // Rewrites the ModifiedBy field
    private static async Task Modify(string path, string userPath)
    {
        var content = await Content.LoadAsync(path);
        content["ModifiedBy"] = userPath;
        await content.SaveAsync();
    }

Upvotes: 1

Related Questions