Archives

Archives / 2006 / July
  • SharePoint 2007: using ASP.NET server side code in your pages

    Remember the problems you had in SharePoint 2003 pages because it was not possible to plug in a simple piece of server side script in your pages? That you always had to write custom controls to accomplish this? Those times could be over, as longs as you approach this with great care.

    In the web.config file in the SharePoint virtual directory contains the following section:

      <SharePoint>
        <SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
          <PageParserPaths>
          </PageParserPaths>
        </SafeMode>
        :
      </SharePoint>

    By default the node <PageParserPaths> is empty. You can add <PageParserPath> nodes to specify the virtual paths where you want to allow server side scripts:

    <PageParserPaths>
            <PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
    </PageParserPaths>

    Where CompilationMode is one of the following values:

    Always The page should always be compiled (default value)
    Auto ASP.NET will not compile the page, if possible. 
    Never The page or control should never be dynamically compiled.

    I assume that the AllowServerSideScript and IncludeSubFolders flags speak for themselves.

    Be careful with the virtual paths you specify in your PageParserPaths. Anyone that can modify or add a page to the virtual path can insert code that will be executed server side with no restrictions.

    A good location to specify as a PageParserPath is the location where you store your masterpages, for example /_catalogs/masterpage. You can now add server side script to your masterpages, which makes it available in all pages using this masterpage.

    <PageParserPaths>
            <PageParserPath VirtualPath="/_layouts/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
    </PageParserPaths>

    There is no documentation available on this functionality. I found two references in the Microsoft SharePoint documentation that handled with variations: http://msdn.microsoft.com/en-us/library/ms562040.aspx and http://msdn.microsoft.com/en-us/library/ms551625.aspx.

    Maurice Prather also describes the PageParserPath functionality in this blog post.

    Thanks to Stramit for pointing me in the right direction in this blog post on SharePoint navigation.

  • SharePoint 2007 Navigation - part 3

    Stramit wrote a real good blog post on SharePoint 2007 TreeView navigation. He is used our good friend Reflector to dive into the SPTreeView and SPHierarchyDataSourceControl to find out what you can accomplish with the SharePoint TreeView navigation controls. He explains how to create a custom masterpage where he modifies the properties of the SPHierarchyDataSourceControl to configure what to display in the TreeView, like Lists, Document Libraries, Sub webs. He also explains how to set to root context from where navigation is displayed to the root web of the site collection by using custom server side script in the masterpage. But hé, you can’t use serverside code in your masterpage I hear you think… wrong!! With some tricks you can get it working. Read Stramits blog post if you are into the navigation game. Good work Stramit!

    Some of my earlier SharePoint navigation related posts:

    SharePoint 2007 navigation dissected – part 1
    SharePoint 2007 navigation - part 2

  • SharePoint 2007 navigation - part 2

    I while ago I started with a blog post serie on SharePoint 2007 navigation. I got to part one so far. Time is my enemy! We have been working on some great things with respect to SharePoint navigation lately like completely modified navigation elements and custom SiteMapProviders. I hope I have the time to elaborate on that in some future blog posts. I the mean time I point you to some nice posts on SharePoint navigation I ran into. Kudos to their authors!

    • Modifying the WSS3 tree navigation control explains how to turn on a tree navigation structure to get results like this:
    • On the SharePoint 2007 documentation on MSDN there is a section called How to: Customize the Display of Quick Launch which describes how to replace the Menu Control with the TreeView Control.
    • Another section in the SharePoint 2007 documentation on MSDN  called Using a Custom Data Source for Navigation describes how to use a delegate feature to replace the QuickLaunchDataSource delegate control in the default master page by defining your custom navigation data source that uses a SiteMapProvider. Cool but advanced stuff!

     

  • SharePoint: WSS versus OSS, a blogger from Iran

    Farvashan, a SharePoint Solution provider and blogger from Iran started blogging at http://www.sharepointblogs.com/farvashan. Its first post is about WSS (Windows SharePoint Services) versus OSS (Office SharePoint Server). WSS the free extension of Windows Server 2003 and OSS that needs additional licenses and is built on top of WSS. He argues that you should only use OSS if you use more than 30% of its features, and that WSS is a powerful application platform you can build your applications on. I totally agree with him. Have a look at the about link for some screendumps of his projects and links to live SharePoint based sites in a language I don’t understand but which looks great.

  • SharePoint and printing the contents of a web part

    When you have complex web parts with all kind of navigational structures around then there is often the need with customers to only print the content of that web part, without all surrouding information.Marian Lishman wrote a nice blog post on how to do exactly that. The approach is actually really neat: collect some information on your HTML page by collecting the HTML data from an innerText property of a specified tag, and write the collected content (together with some additional markup if needed) to a new window that you popup, and automatically print the contents of this popup window. This approach would allow you to write headers and footer, and could be extended to print for example everything within your web part zones.

  • Get the url to the virtual root for the current request

    Sometimes you get into the situation that you have to build a Url within the current request to a page that you know the virtual path for, but it has to be on the same server as the current request. The problem is to find the url for the virtual root of the request. So for the request https://server:1234/myfolder/mypage.aspx we need to find https://server:1234.

    Often you see code like:

    Uri requestUri = Context.Request.Url;
    string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);

    This can be done much easier with the following code:

    string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

  • .Net 2.0 smart String Split() functionality

    There is a new version of the String Split() function, a split with the option to remove empty entries is the resulting string array. This new function solves your problems with lazy encoded multi-value strings. I know, this is one of the zillion things that were improved in .Net 2.0, and a lot of people probably know this one for ages. For me it was completely new however, and if it is new for me, there must be someone out there who didn’t know this as well;-)

    The case:

    Often you create a multi-value string separated with a special character at one end of your application, and you retrieve them at the other end of you application by splitting the string on the special character.

    Example: at one side you have list of users that you encode as a string;

    List of users: "serge", "dinah", "scott", "dean". Encode as: "serge;dinah;scott;dean"

    There are two special cases you have to take into account, either on constructing the multi-value string, or on splitting the multi-value string into its values:

    • Empty entries
    • A trailing special character

    Assume you have the following array of strings: "one","", "three","","five". You can either encode it the lazy and simple way, or the complex way. In the simple way you leave it up to the receiving side to skip empty entries and the trailing special character. In the complex way you deliver clean data on the producing side by skipping empty entries and not writing the special character after the last entry.

    Producing Lazy and Simple:

    Resulting string: "one;;three;;five;"

    string result;
    foreach (string s in stringArray)
    {
        result += s + ";";
    }

    UPDATE: As Danny de Haas pointed out to me this can ofcourse be done even lazier:

    string result = String.Join(";", stringArray);


    Producing Complex:

    Resulting string: "one;three;five"

    string result;
    for (int i=0; i<stringArray.Length; i++)
    {
        if (!String.IsNullOrEmpty(stringArray[i]))
        {
            result += stringArray[i];
            if (i != stringArray.Length – 1)
            {
                 result += ";";
            }
        }
    }

    The above code is maybe not the smartest code, but you get the drift.

    Receiving:

    If you receive a string and you want to take into account that the string could be produced in the lazy and simple way you had to do extra processing on the array you get from result.Split(‘;’) to remove empty entries resulting from encoded empty entries or the trailing special character when working with the .Net 1.1 framework. The .Net 2.0 framework now provides an overload of the String Split() function that is smart enough to handle this case:

    string[] resultArray = result.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);

    This call takes care of the empty entries and the empty entry at the end produced due to a trailing special character.

    See http://msdn.microsoft.com/en-us/library/system.string.split.aspx for more information in the Microsoft documentation on all overloads of the split function, and http://msdn.microsoft.com/en-us/library/system.stringsplitoptions.aspx for more information on the StringSplitOptions.

    Name Description
    String.Split (Char[]) Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array.

    Supported by the .NET Compact Framework.

    String.Split (Char[], Int32) Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array. A parameter specifies the maximum number of substrings to return.
    String.Split (Char[], StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified Char array. A parameter specifies whether to return empty array elements.
    String.Split (String[], StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified String array. A parameter specifies whether to return empty array elements.
    String.Split (Char[], Int32, StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified Char array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
    String.Split (String[], Int32, StringSplitOptions) Returns a String array containing the substrings in this string that are delimited by elements of a specified String array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.