Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Saving Favorites View modes: 
User avatar
Member
Member
Chanan - 2/4/2011 2:26:00 PM
   
Saving Favorites
Hello,

A feature on our site would require saving an article as a favorite for the user. On their profile they would be able to see their favorite articles. How can this be done in Kentico?

User avatar
Kentico Support
Kentico Support
kentico_radekm - 2/7/2011 8:18:03 AM
   
RE:Saving Favorites
Hello.

You can do a little customization and ensure requested behavior like this:

1. Extend required document type by 2 additional columns. Boolean data type (will store info whether given document is marked as favourite or not) and Text data type (will store user´s name). This can be done in Site Manager -> Development -> Document types -> Fields tab.

2. Create a custom web part (http://devnet.kentico.com/docs/devguide/developing_web_parts.htm) or a user control, which will be placed on page template used by documents you want mark as favourite. In code-behind of this web part / user control you will ensure, that when user click „Add to favourites“ bookmark, proper data will be saved into document type´s coupled data table.

So, you will get current node (on which user clicked this option) via this code:

// tree provider

CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

// get document with specified site, alias path and culture

CMS.TreeEngine.TreeNode node = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, CMS.CMSHelper.CMSContext.CurrentDocument.NodeAliasPath,
CMS.CMSHelper.CMSContext.CurrentDocument.DocumentCulture);


Then you can save proper values into additional columns like this:

Node.SetValue(„<columnName>“,value).

Where columnName is code-name of given field, and value you can get from CMSContext class (CMS.CMSHelper.CMSContext.CurrentUser.UserName).

Then, if you want to show documents which are marked as „favourite“ by particular user, you can use simple repeater web part with where condition, and filter result as per user name and „isFavourite“ flag.

Best Regards,
Radek Macalik

User avatar
Member
Member
Chanan - 2/7/2011 8:35:17 AM
   
RE:Saving Favorites
Thanks Radek,

I am not sure I understand part of your answer and that could be why I didn't figure out how to do this myself.

If I add Favorite and Username column to Article type of example, wont each user overwrite each others data? Don't we need a Many-Many type relationship?

Regards,
Chanan

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/6/2011 1:05:25 PM
   
RE:Saving Favorites
Hello.

Yes, you are right. In this case, you will not extend document type´s table directly, but you can create a new table instead, and store required data within.

You can use Custom tables module for this purpose and define 2 columns - documentID and userID. This info is enough to get relationship between favourite document and given user. DocumentID is available in CMS.CMSHelper.CMSContext.CurrentDocument class and userID in CMS.CMSHelper.CMSContext.CurrentUser class. These values can be saved into given custom table in code-behind of your custom web part, like I mentioned in step 2 in my previous post.

Then, on user profile page, you can use any viewer web part (repeater with custom query, custom table repeater, and so on) and show data for current user only (you can use where condition with current user´s ID).

However, this is just a general example showing some basic principle of one particular solution. You can modify it according to your needs and take an inspiration from this example only. For example, instead of storing documentID you can store document URLs directly. This can be done in code-behind of your custom web part, where you can get given URL via CMS.CMSHelper.CMSContext.GetDocumentUrl(documentID) method, where documentID you can get from CMSContext class. Possibilities of customization are wide, it mainly depends on your particular needs and consideration.

Best Regards,
Radek Macalik