Automatically download a document when the page loads

I had a requirement to start a download of a file automatically once my custom application page loads. After trying a couple of approaches, I found this approach which works consistently across browsers. It also turns out to only be a few lines of code. I’ve refactored it into the method below.

// <summary>

/// Adds an HtmlMeta tag to page that tells the browser to download the given file automatically when the page loads

/// </summary>

/// <param name="secondsToWait">Determines how many seconds will pass before the automatic download will begin</param>

/// <param name="fileURL">The URL of the file you want to download</param>

private void CreateFileDownloadMetaTag(int secondsToWait, string fileURL)

{

HtmlMeta meta = new HtmlMeta();

meta.HttpEquiv = "Refresh";

meta.Content = String.Format("{0};URL='{1}'", secondsToWait, fileURL);

this.Page.Header.Controls.Add(meta);

}

Cascading lookups in SharePoint using jQuery

One common request in SharePoint is the ability to cascade lookups when completing a list form. For example, let’s say you have two drop down fields on your form. One is the State and the other is the City. You want the user to be able to select a State and then the City drop down should automatically show the valid cities for that state.

In the past we have used a couple of approaches to solving this problem. Either building custom forms or using a 3rd party utility such as the Bamboo List Selector. Building a custom form just to provide this functionality is overkill and can easily cause more problems than it is worth. The Bamboo List Selector works fine, but requires deploying to the server and a bit of configuration.

Today, with the help of jQuery, we have another option that requires no code deployment and very minimal configuration. SPServices jQuery Library for SharePoint Web Services uses jQuery along with SharePoint’s Web Services to make the solution very easy.

Here is a quick overview of the steps:

  1. Download the JQuery Library. This should be a JavaScript (.js) file that you can save locally.
  2. Download the SPServices jQuery Library for SharePoint Web Services. This should include a couple of files but the one you should be concerned with is the jquery.SPServices-{version}.min.js. It is a compressed version of the JavaScript and is about half the size as the full version.
  3. Take these two JavaScript (.js) files and upload them into a SharePoint Document Library. It is recommended that this be a library on the root site and all users need to have at least read permissions to it.
  4. Now you need to setup your data if it is not already. You should have at least three lists. Here is a quick example.
    1. Create a “States” list with only the Title column. You can add a few states to get started.
    2. Create a “Cities” list with the Title column and an additional lookup column that does a lookup to the States list. Add a few cities for each state.
    3. Create another list that has a lookup to the States list and another lookup column to the Cities list. (The Title field in both)
    4. The next step is to the add the actual jQuery that will make all of this functional. It is recommended that this be added directly to the page using SharePoint Designer or some other means. But a Content Editor web part works as well. Just keep in mind that a Content Editor web part is more accessible to users.
      1. In the main list click on New and you will be taken to the “New Item” form. You will need to add a Content Editor Web Part to this page. To do this through the UI you need to add “ToolPaneView=2” at the very end of the URL in the address bar.
      2. Add the following script to the Content Editor Web Part. The first two lines are references to the JavaScript files you uploaded earlier. The actual configuration is well documented so I won’t try and repeat what it does or how it works. In a nutshell, you’re saying the Cities list defines your relationship and the State Column is the parent and the City (Title) column is the child.

“javascript”

type=

“text/javascript”

src=

“../../Documents/jquery-1.4.2.min.js”

>

“javascript”

type=

“text/javascript”

src=

“../../Documents/jquery.SPServices-0.5.4.min.js”

>

“javascript”

type=

“text/javascript”

>

$(document).ready(

function

() {

$().SPServices.SPCascadeDropdowns({

relationshipList:

“Cities”

,

relationshipListParentColumn:

“State”

,

relationshipListChildColumn:

“Title”

,

parentColumn:

“State”

,

childColumn:

“City”

,

debug:

true

});

});

When you save your changes, your state drop down should filter the city drop down.

LookupExampleLookupExample2

jQuery takes most of the complexity out of writing JavaScript and works great in this type of scenario. The SPServices jQuery library provides some additional functionality as well so be sure to check out the CodePlex site for more information.

Determine if a File Type will be blocked programmatically

If you are writing custom code that uploads a document into a SharePoint Document Library, you must abide by the same rules as if uploading the document through the SharePoint UI. Therefore, if the user tries to upload a document that is of a restricted file type, they will receive an error.

“The following file(s) have been blocked by the administrator: filename”.

You can control which files are blocked from Central Administration. Browse to Operations > Blocked File Types (Under the Security Configuration Group)

When uploading files programmatically, you do not have to write any code to check which file types are allowed before uploading a file. SharePoint will return the same error if you leave your code as-is and you try to upload a blocked file type. Be sure to handle this exception properly so your user knows what is going on.

If you wish to check beforehand you can do so by reading through SPContext.Current.Site.WebApplication.BlockedFileExtensions

Here is a rough example:

public static bool IsFileTypeBlocked(string extension)
{
System.Collections.ObjectModel.Collection<String> types = SPContext.Current.Site.WebApplication.BlockedFileExtensions;
bool blocked = false;

foreach(string type in types)
{
if (extension.Equals(type))
{
blocked = true;
break;
}
}
return blocked;
}