by Keyvan Nayyeri via Keyvan Nayyeri on 10/6/2006 5:19:23 PM
As long as you work with Community Server, can find new great features that you didn't know about before. One of them that most users miss and make their life harder is the ability to mark a post, section or user as favorite. This helps you to come back to any stuff you like in future.
You'll agree with me about the necessity to know about development of this part when I give you a real story: Last week one of Community Server forum users asked us about the ability to mark a blog post as favorite. Currently this feature isn't available by default but after reading this post you should be able to develop your own mechanism to mark a blog post or a blog itself as favorite and our news man can give a better answer to that guy!
CommunityServer.Components.Favorite class is code representation of a favorite and CommunityServer.Components.Favorites namespace and its static methods come handy to add, remove or retrieve favorites (there is no edit option).
As a background you need to know there are three types of favorites in Community Server. You can mark a post, a user or a section as favorite.
In order to add a new favorite to your application you should create a new instance of Favorite object and add it to application via Favorites.AddFavorite() method.
private void AddToFavorites(Post post)
{
CSContext context = CSContext.Current;
Favorite favorite = new Favorite();
favorite.ItemID = post.PostID;
favorite.SettingsID = context.SettingsID;
favorite.FavoriteType = FavoriteType.Post;
favorite.ApplicationType = ApplicationType.Forum;
favorite.UserID = context.User.UserID;
Favorites.AddFavorite(favorite);
}
You can also remove a favorite from application by calling Favorites.DeleteFavorite() method and passing a favorite identifier and FavoriteType enumerator to it (there is another overload to use user and item identifiers instead of favorite identifier).
private void DeleteFavoritePost(Favorite favorite)
Favorites.DeleteFavorite
(favorite.FavoriteType, favorite.FavoriteID);
You can also retrieve an individual Favorite object or a list of Favorite objects for a specific user via Favorites.GetFavorite() or Favorites.GetFavorites() methods.
private bool IsMarkedAsFavorite(int SectionID)
IList favorites = Favorites.GetFavorites
(FavoriteType.Section, CSContext.Current.User.UserID);
foreach (Favorite favorite in favorites)
if (favorite.ItemID == SectionID)
return true;
return false;
Other options are to get all favorite posts, sections or users for a specific user by passing a user ID to Favorites.GetPosts(), Favorites.GetSections() and Favorites.GetUsers() methods and getting a list of favorite objects.
private bool IsUserFavoritePost(int PostID)
IList favoritePosts = Favorites.GetPosts
(CSContext.Current.User.UserID);
foreach (Favorite favoritePost in favoritePosts)
if (favoritePost.ItemID == PostID)
But you may be interested to see who has marked you as a favorite. So just call Favorites.GetUsersWatching() method and pass a user ID (your ID in this case) and get a list of CommunityServer.Components.User objects.
private bool IsMyFan(int UserID)
IList fans = Favorites.GetUsersWatching
foreach (User fan in fans)
if (fan.UserID == UserID)
Now playing: Pink Floyd - Shine On You Crazy Diamond (Part I)
Original Post: CS Dev Guide: Favorites
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.