Using Query String Variables to Populate SharePoint Form Fields

If you have a parent/child relationship between two lists, you will undoubtedly want your users to be able to create child items without having to type in the common key or ID.  I’m not going to go into the full architecture of this here, but here’s an important part of the process.

Let’s say that you’ve built a nice custom page which lists your parent items, perhaps a list of projects, and you have a links next to each projects to ‘Add Status’.  This link points to the page where you provide the user with a form to add an item to the project status list (the child list).  This link probably will look something like this:

http://[servername]/Shared%20Documents/NewProjectStatus.aspx?ProjectID=[n]

Wouldn’t it be great to be able to grab that ProjectID from the Query String and populate the ProjectID column on the child (Project Status) list?  No problem; a little JavaScript will do it.

Open the NewProjectStatus.aspx page in SharePoint Designer, and add the following JavaScript (If you aren’t sure where it goes, try looking for where the script: var navBarHelpOverrideKey = “wssmain”; lives):

_spBodyOnLoadFunctionNames.push("fillDefaultValues");
var vals = new Object();
var navBarHelpOverrideKey = "wssmain";
function fillDefaultValues() {
  var qs = location.search.substring(1, location.search.length);
  var args = qs.split("&");
  for (var i = 0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
  }
  setLookupFromFieldName("ProjectID", vals["ProjectID"]);
}
// setLookupFromFieldName: Set a form field value using its //    fieldname to find it in the page
// Arguments:
//        fieldName:    The name of the list column
//        value:        Set the fieldName to this value
//
function setLookupFromFieldName(fieldName, value) {
  if (value == undefined)
    return;
  var theInput = getTagFromIdentifierAndTitle("input", "", fieldName);
  if (theInput != null) {
    theInput.value = value;
  }
}
// getTagFromIdentifierAndTitle: Find a form field object using its tagName,//     identifier, and title to find it in the page
// Arguments:
//        tagName:    The type of input field (input, select, etc.)
//        identifier:    The identifier for the instance of the fieldName//                       (ff1, ff2, etc.)
//        title:        The title of the list column
//
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);
  for (var i = 0; i < tags.length; i++) {
    var tempString = tags[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
    }
  }
  return null;
}

The form field named ‘ProjectID’ (in this case) will be set to the value passed in on the Query String.  See some of my previous posts showing the JavaScript needed to set other types of form fields like a People Picker.

NOTE (July 12): AutoSponge pointed out a few deficiencies in this post, which I thank him for (see below).  I also should have referenced this post as the original source for this approach.

SharePoint List View Styles

Here’s another one of those things that’s so obvious that you’ve probably never noticed it.  When you configure a view, one of the sections on the page is the Style section (fourth from the bottom in most cases, collapsed).

image

I’d never really looked at them but they all look like they would be useful in various situations:

  • Basic Table

image

  • Boxed, no labels

image

  • Boxed

image

  • Newsletter

image

  • Newsletter, no lines

image

  • Shaded

image

  • Preview Pane — This one is particularly nice, because as you hover over the Title on the left, the right pane shows the details of the list item.

image

  • Default — This is the one you are used to seeing.

image

Saving a Site as a Template: ‘Failure decompressing data from a cabinet file’ Error Workaround

I was trying to save a site as a template today including the content, and I got the error ‘Failure decompressing data from a cabinet file’.

I found a workaround on Ragav Jagannathan’s site: http://ragavj.blogspot.com/2008/02/failure-decompressing-data-from-cabinet.html

…This error "Failure decompressing data from a cabinet file" can be avoided by using the stsadm command line tool of MOSS 2007 and using the "nofilecompression" attribute.For example to export a site template, you would use something like this:

  • stsadm.exe -o export -url http://intranet/test -includeusersecurity -nofilecompression -filename C:\backup

Once you finish your export use the stsadm import command with the same nofilecompression attribute. For example:

  • stsadm.exe -o import -url http://intranet/test -includeusersecurity -nofilecompression -filename C:\backup

Alternatively, you can change the maximum size allowed for a template file:

The standard template size (by default) is set to a max of 10Mb, so it could be failing on this. The following STSADM command allows you to set the template size to approx. 150Mb, though I’ve never tried a template quite that large before! I believe the max is 500Mb. Good luck.

stsadm -o setproperty -propertyname max-template-document-size -propertyvalue 151200000

Append Changes to Existing Text Option on a SharePoint MultiLine Text Column

If you’re like me, you’re constantly finding new useful things in SharePoint that have been sitting right in front of you all along.  I’m working with a smart guy named Bob Nash from CSC at a client right now, and he just showed me a cool thing you can do with a list column.
 
Say you want to track projects that your team is working on in a Custom List and capture activities which occur over the course of the project.  Once you’ve got the list set up with the columns of information that you’d like to track, go to List Settings/General Settings/Versioning settings and turn versioning on.
image
Then create a new column (call it ‘Activity’, for example) and set it to be a ‘Multiple lines of text’.  Because you have versioning turned on, you’ll see an option at the bottom of the column creation screen that allows you to ‘Append Changes to Existing Text’ — click the ‘Yes’ option and save your column.
image
 
Now, whenever anyone edits the item and types new activity information in the ‘Activity’ column, you’ll see the user’s name and a date/time stamp for their entry in the list of all previous entries.  Nice!
image

How to Fix Recurring Meeting Workspace Error: ‘g_InstanceID’ is undefined

I was having a problem with Meeting Workspaces today.  The links to each meeting instance on the left Quick Launch were not working, and every time we clicked on one of them, we would get a JavaScript error: ‘g_InstanceID’ is undefined. 

At first, I assumed that my CSS must be hiding a control that was required for the navigation links to work.  Then I found a post by Mike Monjeau entitled MOSS – How to fix recurring Meeting Workspace error: ‘g_InstanceID’ is undefined that describes the fix for this.

Basically, when you use a master page other than default.master for a Meeting Workspace site, you can get this error.  If you add the following lines into your new master page, you can solve the problem:

  1. Add the following line of code under the <%@ Import Namespace=”Microsoft.SharePoint” %> tag:
    <%@ Register Tagprefix="Meetings" Namespace="Microsoft.SharePoint.Meetings" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2. Add the following line of code after the opening <body> tag:
    <Meetings:PropertyBag runat="server"/>