Got more questions? Find advice on: ASP | SQL | XML | Regular Expressions
in Search
Welcome to WindowsAdvice Sign in | Join | Help

Christian Nordbakk

Some of this, and some of that...

  • Find in files and Windows XP

    I'm so tired of searching for this everytime i need it, so I'll post it here. Hopefully others searching for this too can be helped as well :)

    How to enable content search for unknown filetypes with Windows XPs search functionality: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q309173

     

    Sponsor
  • Filter and Cache Latest Forum Posts

    One thing you will notice if you use my example code for displaying the latest forum posts in a custom UserControl is that announcements and sticky posts are displayed too, and to make it worst, they are sorted by their sticky date (the date a post is set to un-stick) so that they always appear on top.

    I haven’t found a really good solution to this problem, so if anyone knows one, please let me know. In the meantime I’ve come up with a routine that filters out all announcements and sticky posts, and goes like this:

    int pageSize = 10;

    ThreadSet threadSet = Threads.GetThreads(
      -1,
      0,
      pageSize,
      CSContext.Current.User,
      DateTime.MinValue,
      SortThreadsBy.LastPost,
      SortOrder.Descending,
      ThreadStatus.Open,
      ThreadUsersFilter.All,
      false,
      false,
      false,
      false
      );
           
    ArrayList forRemoval = new ArrayList();
    ArrayList forAddition = new ArrayList();
     
    // Loop through the set and filter out announcements and sticky posts
    foreach (object o in threadSet.Threads)
    {
      Thread t = (Thread) o;
     
      if (t.IsAnnouncement || t.IsSticky)
      {
        forRemoval.Add(o);
     
        // We now need to fetch another thread to substitute
        // the one we just removed         
        while (true)
        {
          ThreadSet newThreads = Threads.GetThreads(
            -1,
            pageSize++,
            1,
            CSContext.Current.User,
            DateTime.MinValue,
            SortThreadsBy.LastPost,
            SortOrder.Descending,
            ThreadStatus.Open,
            ThreadUsersFilter.All,
            false,
            false,
            false,
            false
            );

          if (newThreads.Threads.Count > 0)
          {
            Thread newThread = (Thread) newThreads.Threads[0];
         
            // try again if it's a new announcement or sticky post
            if (!newThread.IsAnnouncement && !newThread.IsSticky)
            {
              forAddition.Add(newThread);
              break;
            }
          }
          else
          {
            break;
          }
        }
      }
    }

    // Remove unwanted posts
    foreach (object o in forRemoval)
      threadSet.Threads.Remove(o);

    // Add the substitute posts
    threadSet.Threads.AddRange(forAddition);

    As you can see this code is neither very aesthetically pleasing to look at, nor very efficient (as opposed to not having to do all this anyway). To counter the overhead created by this I’ve implemented caching of the filtered threads using the mechanisms provided by CommunityServer.

    string cacheName = "LatestForumPosts-";
    if (CSContext.Current.RolesCacheKey == null)
      cacheName += "Everyone";
    else
      cacheName += CSContext.Current.RolesCacheKey;

    ThreadSet threadSet;

    if ((threadSet = (ThreadSet) CSCache.Get(cacheName)) == null)
    {

      /***************************************
       Paste in the first code example here
      ****************************************/

      // Cache the filtered set
      CSCache.Insert(cacheName, threadSet, CSCache.MinuteFactor * 15);
    }

    This mostly counters the overhead brought on by the filter routine, by refreshing the list only once every 15 minutes for each user group. It works, but it just feels wrong, you now…

    Sponsor
  • Latest blog posts in CommunityServer

    Just like with recent forum posts, it can be useful to be able to render the latest blog posts on a different page other than the aggregate page. This mini-article will outline the basic steps needed to create a control similar to what is used on the front page on www.windowsadvice.com.

    Again reflector (or the CS source) is your friend in figuring out how it all works in CommunityServer internals. In this particular case, all you need is already in use on the blogs aggregate page (CommunityServer.Blogs.Controls.AggregatePostList). The code below is more or less just a copy of what is already found in CS, so no credits to me.

    The needed references:

    using CommunityServer.Blogs.Components; // BlogThreadQuery among other things
    using CommunityServer.Components; // the ThreadSet object

    The code:

     // Contstruct query for getting threads
    BlogThreadQuery query = new BlogThreadQuery();

    query.BlogPostType = BlogPostType.Post;
    query.BlogThreadType = BlogThreadType.Recent;
    query.IncludeCategories = false;
    query.IsPublished = true;
    query.PageIndex = 0;
    query.PageSize = 10;
    query.SortBy = BlogThreadSortBy.MostRecent;
    query.SortOrder = SortOrder.Descending;
    query.FilterByList = Sections.FilterByAccessControl(
       
    Weblogs.GetWeblogs(CSContext.Current.User,false,true,false),
       Permission.View);

    ThreadSet ts = WeblogPosts.GetBlogThreads(query,true,true);

    ArrayList threads = ts.Threads;

    The threads variable will now contain (up to) the last 10 published posts that the current user has view access to. The ArrayList can be bound to any bindable control, and as always (or, like last time anyway) I’ve provided a simple but working UserControl that you can experiment with yourself. Download it here.

    Let me know if you have any questions, and I will see if I can’t add another episode to this mini-series :)

    Sponsor
  • Latest Forums Posts UserControl in CommunityServer

    One of the things we wanted for WindowsAdvice was for the 10 most recent forum posts to be displayed on the frontpage. CommunityServer doesn’t provide a finished control for this, but does already have the functionality. Here’s a quick post to explain how to find and use this functionality on your own site. I’ve also provided a download to a very basic, but functional UserControl for demonstration.

    In the CommunityServer.Discussions assembly, under the CommunityServer.Discussions.Components namespace there is nifty little class named Threads. This class contains a GetThreads method which, when called will return a set of threads that match the criteria’s you supply as parameters.

    I won’t go into much detail on the parameters beyond the method signature which I’ve copied out of Reflector:

    public static ThreadSet GetThreads(int forumID, int pageIndex, int pageSize, User user, DateTime threadsNewerThan, SortThreadsBy sortBy, SortOrder sortOrder, ThreadStatus threadStatus, ThreadUsersFilter userFilter, bool activeTopics, bool unreadOnly, bool unansweredOnly, bool returnRecordCount);

    A few things about the parameters though are worth mentioning.

    • For the forumID parameter you can supply -1 instead of a legit ID. This will cause threads from all forums to be returned.
    • The user parameter, when supplied with an User object of an actual user (e.g. CSContext.Current.User) will filter the issued query so that only threads which the user actually has access to will be returned.
    • ThreadUsersFilter.All will apply no filter, contrary to what it says :)

    The GetThreads method returns a ThreadSet object that has a property named Threads on it. This property will return you an ArrayList of Thread objects which you can bind to e.g. a Repeater Control in your UserControl.

    Please see the demonstration UserControl for more details on the binding part.

    The cool thing about using the GetThreads method (besides the obvious relief of having to dig through the database schema and procedures to return this result yourself) is that you will get full use of the caching and access control mechanisms that CS provides. No worries about performance or security :)

    I was thinking about writing a longer article on this topic, so please let me know if that would be interesting, and what specifically you would want to know.

    Sponsor
  • Useful registry settings

    I tend to make quite a few changes to a clean install of Windows to make things a bit tidier and run smoother. Below are a list of some of my most belowed registry hacks / settings and scripts, some of which I hope can be useful to you too:

    Disclaimer:
    The files are located at my private site (http://www.anothereon.net), and are as such under my control. I do not, however, give any guarantees with these hacks. If you do not fully understand what implications changes to the registry can impose on your system, are not able to understand the details of what these hacks do, or you aren't prepared to scrap you system if they should mess things up THEN DO NOT DOWNLOAD ANY OF THESE FILES.

    [Registry settings]
    TaskbarIconsOnly - Sizes the taskbar items so that only the icon is visible. I don't like the auto grouping function in XP very much, so this setting allows me to fit a lot more windows in there without clutter.
    OpenAllFoldersAsExplorer - This hack will force all windows you open to display the explorer bar at the left side of the window. I find this one particularly useful since I don't always use the Windows+E shortcut to launce new windows (ThinkPad anyone?).
    CmdToRightClick - Very, very useful, since it inserts a "Cmd from Here" item in the context menu when you right-click a folder. Saves you from having to CD to that folder.
    DisableBaloonTips - Disables the most annoying baloon tips (all baloon tips are annoying baloon tips, right? ) from popping up on the task bar at all times.
    CmdQuickEdit - Enables shortcuts in the command prompt.
    VSNet2003CmdHere - The same as CmdToRightClick but lets you launch a cmd prompt with VisualStudio 2003 variables loaded.

    [Scripts]
    RemoveWindowsMessenger - Completely removes Windows Messenger from you XP installation.
    DisableAVIScan - Prevents Windows from searching AVI files for meta information, something that causes the CPU to pike at a 100% for a long time just by highlighting such a file in explorer.

    The files are all packaged into one archive and can be downloaded here: http://www.anothereon.net/Downloads/XPTweaks.zip

    Sponsor