On my application I have my own API that saves content in different repositories. One of them is the Sensenet.
I use the Client API to store the content on Sensenet, but I'm having a problem when I initialize the Client Context:
ClientContext.Initialize(new[]
{
new ServerContext
{
Url = siteUrl,
Username = userName,
Password = userPassword
}
});
When I start my Application, the first call on the methods initializes well, but on the second call and so on it launches an exception...
I believe that the solution can pass by using ClientContext.Current... But I don't know how I should implement it...
The client context's Initialize method should be called only once per app domain. This is why you get an exception on the second call.
In your case (as you call Sense/Net from another web application) you should call it only once somewhere in your app start process. As a different example: from a command line tool it should be called somewhere in the Main method.
The Initialize method does not do much currently, just stores the provided server information. This is useful if you want to call Sense/Net with a single admin user from an importer tool for example, so that you do not have to provide user credentials on every call.
But if you want to call methods with different user credentials (to make use of Sense/Net's permission system), you can simply provide server information on every call. For example:
var server = new ServerContext
{
Url = "http://example.com",
Username = "johnsmith",
Password = "password"
};
// see the server parameter here!
var content = await Content.LoadAsync(1234, server);
content["MyField"] = "value";
// no server parameter, we already know it
await content.SaveAsync();