This is sequel post of the Using the EPiServer Community API outside IIS where I described how you can call the EPiServer CommunityAPI inside a standalone console application. This post will go a little further and show you how you can import images in the same maner.
The issue
In my atttempt to create a lot of users with attached my pages profiles i stumbled across a issue when adding portrait images. It turnes out that the ImageGallery module in the EPiServer Community Framework refers directly to the System.Web.Hosting.HostingEnvironment, instead of wrapping it inside a custom object as the EPiServer CMS does it with the EPiServer.Web.Hosting.GenericHostingEnvironment class. The HostingEnvironment is thereby not initialized and calls to e.g. HostingEnvironment.MapPath() will fail.
The solution
The only way around this is to create a fake web application context. I did not attempt to figure out this on my own, so I hoped somebody had done this before. After some searching i finally found a nice hack onPhil Haack's blog. He have created a HttpSimulator class that is intended to be used when performing unit tests. The HttpSimulator sets private field values using reflection values in the same way i've injected the community configuration.
To enable adding images do the follwing:
- Download the HttpSimulator
- Unzip and copy the following files into your console application
- HttpSimulator.cs
- ReflectionHelper.cs
- SimulatedHttpRequest.cs
- Wrapp all of your community api calls inside the following code block
using (new Subtext.TestLibrary.HttpSimulator("/", Directory.GetCurrentDirectory()).SimulateRequest())
{
//your community api calls here
}
Example:
The following example creates/gets a test user and creates a new new profile image for that user
//Create web application context + http context
using (new Subtext.TestLibrary.HttpSimulator("/", Directory.GetCurrentDirectory()).SimulateRequest())
{
//*******************************
// Initializing the community api
//*******************************
//Read app config file
System.Configuration.Configuration config = null;
config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Inject configuration into the EPiServer Common + EPiServer Community framework
Type type = null;
FieldInfo field = null;
type = typeof(EPiServer.Common.Configuration.EPiServerCommonSection);
field = type.BaseType.GetField("m_config", BindingFlags.Static | BindingFlags.NonPublic);
field.SetValue(null, config);
type = typeof(EPiServer.Community.Configuration.EPiServerCommunitySection);
field = type.BaseType.GetField("m_config", BindingFlags.Static | BindingFlags.NonPublic);
field.SetValue(null, config);
//Initializing the framework
EPiServer.Common.Web.Global.OnBeginRequest();
if (EPiServer.Community.CommunitySystem.CurrentContext == null)
{
throw new Exception("The community context is not initialized");
}
//****************************
// Using the community api
//****************************
//the test user to add a profile image
string username = "TestUser";
//try to find the test user
EPiServer.Common.Security.IUser user = EPiServer.Community.CommunitySystem.CurrentContext.DefaultSecurity.GetUserByUserName(username);
if (user == null)
{
//the test user was not found -> lets create a new user
user = EPiServer.Community.CommunitySystem.CurrentContext.DefaultSecurity.NewUser;
user.UserName = username;
user.Alias = "Java Developer";
user.GivenName = "Test";
user.SurName = "User";
user.PassWord = "SomePassword123";
user.EMail = "testuser@mysite.com";
user.BirthDate = new DateTime(1900, 1, 1);
EPiServer.Common.Data.DatabaseHandler.RunInTransaction(delegate
{
//This will create the user with mypage profile
user = EPiServer.Community.CommunitySystem.CurrentContext.DefaultSecurity.AddUser(user);
});
}
//Get hold of the users mypage profile
EPiServer.Community.MyPage.MyPage profile = EPiServer.Community.MyPage.MyPageHandler.GetMyPage(user);
//make the profile object writable
profile = (EPiServer.Community.MyPage.MyPage)profile.Clone();
if (profile.Portrait == null)
{
//the user profile does not have a image -> lets add a new one
string imagePath = @"Images\JavaDeveloper.jpg";
using (FileStream file = File.Open(imagePath, FileMode.Open))
{
EPiServer.Community.ImageGallery.Image portraitImage
= new EPiServer.Community.ImageGallery.Image("ProfileImage.jpg",
"A random java developer",
file);
portraitImage.PublishState = EPiServer.Common.Publishing.PublishState.Published;
portraitImage.Uploader = profile.User;
profile.Portrait = portraitImage;
EPiServer.Common.Data.DatabaseHandler.RunInTransaction(delegate
{
EPiServer.Community.MyPage.MyPageHandler.UpdateMyPage(profile);
});
}
}
}
And here is the end result:
