User Registered Event Handlers tutorial Question

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
12/23/2010 5:57:20 AM
Gravatar
Total Posts 50

User Registered Event Handlers tutorial Question

Hi,

im following the User Registered Event Handlers tutorial . 

i created a new class library project called "phytech.Business.WebHelpers" and in it created a class file called "CreateUserDirectory.cs" 

i copied the class code from the  tutorial  and renamed the class name to "public class CreateUserDirectory : UserRegisteredHandlerProvider"

but the tutorial says i must also change the namespace name , but if i change it to

 namespace phytech.Business.WebHelpers.UserRegisteredHandlers

the code indicates that the " : UserRegisteredHandlerProvider" is not recognized and its missing a reference.

if i change it back to "mojoPortal.Business.WebHelpers.UserRegisteredHandlers" then it build fine.

am i doing something wrong, do i need to add a reference somewhere ? copy the project files ?  or is it a mistake in the tutorial ? 

Thanks

12/23/2010 6:22:54 AM
Gravatar
Total Posts 18439

Re: User Registered Event Handlers tutorial Question

yes, you are doing it wrong, you need to use your own namespace. To make it see your handler you need to create an xml file to declare it as indicated at the bottom of the document.

http://www.mojoportal.com/user-registered-event-handlers.aspx

this xml file is how mojoPortal knows about your handler and your namespace and your assembly/dll

Seems like you are trying to create folders per user, there is already logic to handle that automatically but it will create the folder by the userid not the user name (which might change since admin can edit user name). If you add a user to roles indicted under Administration > Site Settings > Security > Permissions > Roles that can upload files but only to a user specific location, then the user will be able to upload files but only to that user specific location.

Hope it helps,

Joe

12/23/2010 7:32:01 AM
Gravatar
Total Posts 50

Re: User Registered Event Handlers tutorial Question

Hi Joe, yes,  i want to create a directory for each authenticated user. i did as you wrote and added the "authenticated users" group to the Administration > Site Settings > Security > Permissions > Roles that can upload files but only to a user specific location.

now i created a new user from the administration "add new user". where can i see the newly created folder ?

basically i have an FTP client interface and i want to use it to upload files to this user directory, so what i'll do is turn it into a FTP virtual directory from the web server FTP management. i just need to know where this folder tree is created.

Thanks again, this will save me a lot of time.

12/23/2010 9:19:30 AM
Gravatar
Total Posts 18439

Re: User Registered Event Handlers tutorial Question

The user specific folders will be created under /Data/Sites/[SiteID]/userfiles/[UserID]

but the folder is not created until the first time the user tries to use the file manager or upload files via the editor. If you want to create them when the user is created you would need to implement the userRegistered handler to do that.

Writing this without testing but something like this snippet inside a UserRegisteredEventHandler:

using mojoPortal.Web.Framework;

public override void UserRegisteredHandler(object sender, UserRegisteredEventArgs e)
{
 //if (sender == null) return;
 if (e == null) return;
 if (e.SiteUser == null) return;

 string userBasePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Data/Sites/" + e.SiteUser.SiteID.ToInvariantString() + "/userfiles/ )
 if(!Directory.Exists(userBasePath)) { Directory.CreateDirectory(userBasePath); }

 string userPath = userBasePath + e.SiteUser.UserID.ToInvariantString();

 if(!Directory.Exists(userPath)) { Directory.CreateDirectory(userPath); }
}

probably would want to wrap it in try catch and log any error in the catch

Hope it helps,

Joe

 

12/23/2010 2:07:28 PM
Gravatar
Total Posts 50

Re: User Registered Event Handlers tutorial Question

ok, these are the steps i took so far:

1. continuing the GuestBook tutorial (after i have created the 3 tiers - phytech.Web.UI, phytech.Business, phytech.Data.MSSQL)

 i added a class library project called phytech.Business.WebHelpers .

2. changed the class name to "CreateUserDirectory.cs"

3. replaced the code with the code you suggested.

using System;
using System.Configuration.Provider;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using mojoPortal.Web.Framework;
using mojoPortal.Business;
using log4net;


namespace phytech.Business.WebHelpers.UserRegisteredHandlers {

    public class CreateUserDirectory : UserRegisteredHandlerProvider
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(CreateUserDirectory));

public CreateUserDirectory()
{ }

public override void UserRegisteredHandler(object sender, UserRegisteredEventArgs e)
{

.....    

}}}

 

4.created the folder structure "Setup\ProviderConfig\userregisteredhandlers\"  and added the xml file "CreateDirectoryUserregisteredhandler.config" with this code:

<?xml version="1.0" encoding="utf-8" ?>
<UserRegisteredEventHandlers>
  <providers>
    <add name="CreateUserDirectory" type="phytech.Business.WebHelpers.UserRegisteredHandlers.CreateUserDirectory, phytech.Business.WebHelpers"
description="class for creating a user folder when a user is created" />

  </providers>
</UserRegisteredEventHandlers>

5. added these lines to the "phytech.Web.UI" project - post build event:

xcopy /s /y "$(ProjectDir)Setup\ProviderConfig\userregisteredhandlers\*.config" "$(SolutionDir)Web\Setup\ProviderConfig\userregisteredhandlers\"

xcopy /s /y "$(ProjectDir)bin\phytech.Business.WebHelpers.dll" "$(SolutionDir)Web\bin\"

6. Build phytech.Web.UI

im getting this error during the build (10 out of 11 projects build fine except for this one):

------ Build started: Project: phytech.Business.WebHelpers, Configuration: Debug Any CPU ------
C:\...\...\...\mojoportal\_sts_Phytech_Projects\phytech.Business.WebHelpers\CreateUserDirectory.cs(14,40): error CS0246: The type or namespace name 'UserRegisteredHandlerProvider' could not be found 

i tried adding the mojoportal.business.WebHelpers reference to the phytech.Business.WebHelpers, but it didn't help.

can you tell what it is im doing wrong ?

Thanks

Boaz

12/23/2010 2:23:12 PM
Gravatar
Total Posts 18439

Re: User Registered Event Handlers tutorial Question

you were right to add the reference to mojoPortal.Business.WebHelpers, you also need to add

using mojoPortal.Business.WebHelpers; 

in your class.

fyi, you could put this class in your pytech.Web.UI project, it doesn't have to be in a separate class library.

Hope it helps,

Joe

12/23/2010 3:13:00 PM
Gravatar
Total Posts 50

Re: User Registered Event Handlers tutorial Question

yeh, thanks for the tip.

i also needed to add -

using mojoPortal.Business.WebHelpers.UserRegisteredHandlers;   for it to work

and change

e.SiteUser.SiteID.ToInvariantString()  to

e.SiteUser.SiteId.ToInvariantString() 

now it build ! great, i'll move forward and see if it works.

Boaz

12/25/2010 11:47:59 AM
Gravatar
Total Posts 50

Re: User Registered Event Handlers tutorial Question

Works like a charm, thanks for the help Joe.

A tip if someone ever uses this function

System.Web.Hosting.HostingEnvironment.MapPath  won't build unless you add

using System.Web;

using System.Web.Hosting;

and for this to build you need to add a reference to System.Web from the .Net tab (even though the "using Sysetm.Web;" part doesn't show any error)

You must sign in to post in the forums. This thread is closed to new posts.