
Introduction
DotNetNuke's portal system is one of its most compelling features. DNN Portals can be either Parent or Child. The different is from the url.
Parent Portal (example):
http://www.asphostportal.com
http://dnn.asphostportal.com
Child Portal (example):
http://www.asphostportal.com
http://asphostportal.com/dnn (As we have utilized "/" after area name, its called child portal)
Sometimes we have to create custom modules where we have to programmatically create portals. In this article we will plot how to make a a child portal programmatically. The technique needs few parameters to be passed which will be obliged to make the child portal. All these parameters have some settled reason. The part of individual parameters are unmistakably clarified in this technique.
/// <summary>
/// Creates new child portal
/// </summary>
protected void CreateNewPortal(string strPortalName, string strPassword, string strConfirmPassword,
string strFirstName, string strLastName, string strUserName,
string strQuestion, string strAnswer, string strEmail)
{
try
{
PortalController.PortalTemplateInfo template = LoadPortalTemplateInfoForSelectedItem();
bool blnChild;
string strPortalAlias;
string strChildPath = string.Empty;
var closePopUpStr = string.Empty;
var objPortalController = new PortalController();
//Set Portal Name
strPortalName = strPortalName.ToLowerInvariant();
strPortalName = strPortalName.Text.Replace("http://", "");
//Validate Portal Name
if (!Globals.IsHostTab(PortalSettings.ActiveTab.TabID))
{
blnChild = true;
strPortalAlias = strPortalName;
}
else
{
blnChild = true;
strPortalAlias = blnChild ? strPortalName.Substring(strPortalName.LastIndexOf("/") + 1) : strPortalName;
}
string message = String.Empty;
ModuleMessage.ModuleMessageType messageType = ModuleMessage.ModuleMessageType.RedError;
if (!PortalAliasController.ValidateAlias(strPortalAlias, blnChild))
{
message = Localization.GetString("InvalidName", LocalResourceFile);
}
//Validate Password
if (strPassword != strConfirmPassword)
{
if (!String.IsNullOrEmpty(message)) message += "<br/>";
message += Localization.GetString("InvalidPassword", LocalResourceFile);
}
string strServerPath = Globals.GetAbsoluteServerPath(Request);
//Set Portal Alias for Child Portals
if (String.IsNullOrEmpty(message))
{
if (blnChild)
{
strChildPath = strServerPath + strPortalAlias;
if (Directory.Exists(strChildPath))
{
message = Localization.GetString("ChildExists", LocalResourceFile);
}
else
{
if (!Globals.IsHostTab(PortalSettings.ActiveTab.TabID))
{
strPortalAlias = Globals.GetDomainName(Request, true) + "/" + strPortalAlias;
}
else
{
strPortalAlias = strPortalName;
}
}
}
}
//Get Home Directory
string homeDir = "";
//Validate Home Folder
if (!string.IsNullOrEmpty(homeDir))
{
if (string.IsNullOrEmpty(String.Format("{0}\\{1}\\", Globals.ApplicationMapPath, homeDir).Replace("/", "\\")))
{
message = Localization.GetString("InvalidHomeFolder", LocalResourceFile);
}
if (homeDir.Contains("admin") || homeDir.Contains("DesktopModules") || homeDir.ToLowerInvariant() == "portals/")
{
message = Localization.GetString("InvalidHomeFolder", LocalResourceFile);
}
}
//Validate Portal Alias
if (!string.IsNullOrEmpty(strPortalAlias))
{
PortalAliasInfo portalAlias = PortalAliasController.GetPortalAliasLookup(strPortalAlias.ToLower());
if (portalAlias != null)
{
message = Localization.GetString("DuplicatePortalAlias", LocalResourceFile);
}
}
//Create Portal
if (String.IsNullOrEmpty(message))
{
//Attempt to create the portal
var objAdminUser = new UserInfo();
int intPortalId;
try
{
// These parameters are required to assign admin to the portal
objAdminUser.FirstName = strFirstName;
objAdminUser.LastName = strLastName;
objAdminUser.Username = strUsername;
objAdminUser.DisplayName = strFirstName + " " + strLastName;
objAdminUser.Email = strEmail;
objAdminUser.IsSuperUser = false;
objAdminUser.Membership.Approved = true;
objAdminUser.Membership.Password = strPassword;
objAdminUser.Membership.PasswordQuestion = strQuestion;
objAdminUser.Membership.PasswordAnswer = strAnswer;
objAdminUser.Profile.FirstName = strFirstName;
objAdminUser.Profile.LastName = strLastName;
intPortalId = objPortalController.CreatePortal(tbTitle.Text,
objAdminUser,
tbDescription.Text,
"",
template,
homeDir,
strPortalAlias,
strServerPath,
strChildPath,
blnChild);
//Clears the cache
DotNetNuke.Common.Utilities.DataCache.ClearPortalCache(PortalId, false);
}
catch (Exception ex)
{
intPortalId = Null.NullInteger;
message = ex.Message;
}
// Sends email on portal creation
SendMailOnPortalCreation(intPortalID, strEmail, strPortalAlias, objAdminUser);
}
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "", message, messageType);
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
The motivation behind this technique to send email on effective creation of child portal. The sending of email obliges legitimate SMTP settings in the DNN site.
private void SendMailOnPortalCreation(int intPortalID, string strEmail, string strPortalAlias, UserInfo objAdminUser)
{
try
{
if (intPortalId != -1)
{
//Create a Portal Settings object for the new Portal
PortalInfo objPortal = objPortalController.GetPortal(intPortalId);
var newSettings = new PortalSettings { PortalAlias = new PortalAliasInfo { HTTPAlias = strPortalAlias }, PortalId = intPortalId, DefaultLanguage = objPortal.DefaultLanguage };
string webUrl = Globals.AddHTTP(strPortalAlias);
try
{
if (!Globals.IsHostTab(PortalSettings.ActiveTab.TabID))
{
message = Mail.SendMail(PortalSettings.Email,
strEmail,
PortalSettings.Email + ";" + Host.HostEmail,
Localization.GetSystemMessage(newSettings, "EMAIL_PORTAL_SIGNUP_SUBJECT", objAdminUser),
Localization.GetSystemMessage(newSettings, "EMAIL_PORTAL_SIGNUP_BODY", objAdminUser),
"",
"",
"",
"",
"",
"");
}
else
{
message = Mail.SendMail(Host.HostEmail,
strEmail,
Host.HostEmail,
Localization.GetSystemMessage(newSettings, "EMAIL_PORTAL_SIGNUP_SUBJECT", objAdminUser),
Localization.GetSystemMessage(newSettings, "EMAIL_PORTAL_SIGNUP_BODY", objAdminUser),
"",
"",
"",
"",
"",
"");
}
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
closePopUpStr = (PortalSettings.EnablePopUps) ? "onclick=\"return " + UrlUtils.ClosePopUp(true, webUrl, true) + "\"" : "";
message = string.Format(Localization.GetString("UnknownSendMail.Error", LocalResourceFile), webUrl, closePopUpStr);
}
var objEventLog = new EventLogController();
objEventLog.AddLog(objPortalController.GetPortal(intPortalId), PortalSettings, UserId, "", EventLogController.EventLogType.PORTAL_CREATED);
//Redirect to this new site
if (message == Null.NullString)
{
//webUrl = (PortalSettings.EnablePopUps) ? UrlUtils.ClosePopUp(true, webUrl, false) : webUrl;
Response.Redirect(webUrl, true);
}
else
{
closePopUpStr = (PortalSettings.EnablePopUps) ? "onclick=\"return " + UrlUtils.ClosePopUp(true, webUrl, true) + "\"" : "";
message = string.Format(Localization.GetString("SendMail.Error", LocalResourceFile), message, webUrl, closePopUpStr);
messageType = ModuleMessage.ModuleMessageType.YellowWarning;
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
Conclusion:
By utilizing the above procedure simply make the child portals in DNN. Assuredly, this post describes the procedure clearly and and useful for you