How I am using a Delegate control, code in a Document library, and jQuery to customize SharePoint 2010

In this post, I showed how to put a delegate control at the top over every page, and in the control add the links to the jQuery libraries. This is nothing new, there are many articles that show how to do this. I took this a step further and I included code to do the following:

  1. Determine the current page’s relative URL (where the delegate control is running) and put that value in a Variable
  2. Take that variable and change the relative URL’s forward slashes (“/”) to underscores (“_”), and change “.aspx” to “.txt”
  3. Look for a file in a document library with a name that matches the variable created above and insert it’s contents into the additional page header

For example if  I am browsing to http://sharepoint.company.com/Lists/Announcements/AllItems.aspx, and there is a file in the desiganted Document library named “_lists_announcements_allitems.txt” then it’s contentens will show up in the addition page header.

Some of the benefits I have found with my methods

  1. All client side code is kept in one place, and you can easly figure out which file is being used on each page
  2. The Document Library can have versioning, so you can rollback changes
  3. Linked CEWP can be kept in the same place
  4. It is easy to edit text, js and css files in SharePoint Designer, rather than in CEWP
  5. Now it is a snap to chop up a NewForum.aspx via jQuery
  6. It is easy to extend the code so that a file is included across all pages, making it easy to add a new js library to the entire site

Here is the code I am using in the Delegate Control (ascx)

string CurrentSiteRelativeURL = CurrentUrl.ToLower().Replace(CurrentWeb.Url.ToLower(), "");
string CustomFileName = CurrentSiteRelativeURL.Replace("/", "_");
string PageSpecificHeaderIncludes = CurrentWeb.Url + "/DocumentLibraryWithCodeInIt/" + CustomFileName.Replace(".aspx", ".txt");

Response.Write("<!--PageSpecificHeaderIncludes:" + PageSpecificHeaderIncludes + "-->");
    if (CurrentWeb.GetFile(PageSpecificHeaderIncludes).Exists == true)
    {
        SPFile tempFile = CurrentWeb.GetFile(PageSpecificHeaderIncludes);
        StreamReader reader = new StreamReader(tempFile.OpenBinaryStream());
        string myText = reader.ReadToEnd();
        reader.Close();
        Response.Write(myText);
    }

Anyone else doing anything like this? Thoughts?

,

Comments are closed.