Tag: tips and tricks

  • New SharePoint library not showing in Quick Launch after selecting Yes in Library settings

    New SharePoint library not showing in Quick Launch after selecting Yes in Library settings

    A client reported this issue to me this week.  They had added a new Document Library to a Team Site, and had assigned specific permissions to it.  However, it was never being displayed on the Quick Launch navigation.

    We checked the obvious things:

    1. Yes, the Display this document library on the Quick Launch? setting was set to Yes.
    2. Yes, the user had permissions to the site and document library.
    3. We hid and then displayed the Quick Launch using the Site Settings -> Tree View options.

    While initially we thought this might be some weird behavior in permissions, I came across this post that mentioned the Current Navigation settings for the site.  So I checked there.

    I found that the Current Navigation settings were fine, set to display the navigation items below the current site.

    What I also found, however, was that the Structural Navigation: Sorting option was set to Sort Manually.  When I looked in the list of items available for editing and re-sorting the navigation items, the new Library was not shown.

    navigation-sort

    So I flipped the setting to Sort Automatically, and voila, the library appeared in the Navigation items display.  As soon as I saved the navigation settings with the automatic sort, the library also appeared on the Quick Launch menu.

    It appears that if the manual sort option is in force, then it is based on a snapshot of the navigation items, and you’d have to manually add the newly added library to the Navigation hierarchy.

    Hope that helps someone out there!

  • My Favorite Extension Methods: IsGuid

    In this third installment of my Favorite Extension methods series, I will show how to quickly evaluate a string to determine if it is the string representation of a Guid.

    I came upon the need for this method because I am using ASP.NET MVC for an application that uses a Guid to identify a record. In a case where my controller method is to retrieve a record, the controller’s parameter is a string.

    <Extension()> _
    Public Function IsGuid(ByVal value as String) As Boolean
      If String.IsNullOrEmpty(value) Then
        Return False
      Else
        Try
          Dim guid As New Guid(value)
          Return True
        Catch Ex As Exception
          Return False
        End Try
    End Function

    Using this extension method, I can ensure that a string represents a Guid and I won’t encounter any errors when trying to Cast that value into a Guid.

    If Not recordGuid.IsGuid Then Throw New ArgumentException("RecordGuid must contain a Guid string.")
  • My favorite extension methods: HtmlEncode shortcut

    This is the second installment in my series highlighting my personal favorite .NET extension methods.  We’ve already talked about a String.Format shortcut.  Now we’re on to making the HtmlEncode function readily accessible.
    Any time you are showing user-entered content as text on a web page, it is important to HtmlEncode the string so as to prevent execution of any rogue scripts.  The following is a two part implementation of the .NET HtmlEncode that makes this useful function much more accessible.
    Part 1 is a wrapper around the base .NET function with an overload to pass in your own value to display if the value to encode is empty.
    Public Shared Function HtmlEncode(ByVal value As String) As String
            Dim context As HttpContext = System.Web.HttpContext.Current
            If Not context Is Nothing Then
                Dim Server As HttpServerUtility = context.Server
                If value Is Nothing OrElse Trim(value) = "" Then
                    Return "{none}"
                Else
                    Dim sText As String = Server.HtmlEncode(value)
                    Dim sCRLF As String = vbCrLf
                    sText = Replace(sText, sCRLF, "<br>")
                    Return sText
                End If
            Else
                Throw New Exception("This function must be called from an ASP.Net application.")
            End If
        End Function
    
        Public Shared Function HtmlEncode(ByVal value As String, ByVal mapEmptyTo As String) As String
            Dim context As HttpContext = System.Web.HttpContext.Current
            If Not context Is Nothing Then
                Dim Server As HttpServerUtility = context.Server
                If value Is Nothing OrElse Trim(value) = "" Then
                    Return mapEmptyTo
                Else
                    Dim sText As String = Server.HtmlEncode(value)
                    Dim sCRLF As String = vbCrLf
                    sText = Replace(sText, sCRLF, "<br>")
                    Return sText
                End If
            Else
                Throw New Exception("This function must be called from an ASP.Net application.")
            End If
        End Function
    The second part is an extension method to let you call the HtmlEncode function from any string, again with an overload to let you pass in your own empty value text.
       <Extension()> _
       Public Function HtmlEncode(ByVal value As String) As String
           Return Functions.HtmlEncode(value, "{none}")
       End Function
       <Extension()> _
       Public Function HtmlEncode(ByVal value As String, ByVal mapEmptyTo As String) As String
           Return Functions.HtmlEncode(value, mapEmptyTo)
       End Function
  • My favorite Extension methods: String.Format shortcut

    This is the first entry in a short series highlighting some of my favorite extension methods.

    It’s a generally accepted best practice to use the String.Format() method to assemble string values that merge text and variables.

    Using an Extension Method (.NET 3.5 or greater) makes accessing the String.Format function even easier.

    Now, for any string value, you can simply use the following

    "The quick brown fox {0}".Fmt("jumped over the lazy dog.")

    Here’s the code for the extension method and various overloads:

    <Extension()>_
    Public Function Fmt(ByVal format As String, ByVal arg0 As Object) As String
      Return String.Format(format, arg0)
    End Function
    
    <Extension()>_
    Public Function Fmt(ByVal format As String, ByVal arg0 As Object, ByVal arg1 As Object) As String
      Return String.Format(format, arg0, arg1)
    End Function
    
    <Extension()>_
    Public Function Fmt(ByVal format As String, ByVal arg0 As Object, ByVal arg1 As Object, ByVal arg2 As Object) As String
      Return String.Format(format, arg0, arg1, arg2)
    End Function
    
    <Extension()>_
    Public Function Fmt(ByVal format As String, ByVal ParamArray args() As Object) As String
      Return String.Format(format, args)
    End Function