Monday, April 13, 2009

JavaScript in SharePoint 2007 / MOSS Data View XSL

This sound obvious, but looks like lots of people do not know this. YES, you can use JavaScript in XSL. And YES, you can use JavaScript in SharePoint Data View XSL. And it is fairly easy. Here is a small sample.

Let’s say we want to know how old each item we view in Data View is. As the version of XSL used in SharePoint is “1.0”, we don’t really have a wide scope of date functions. Actually we do have only “FormatDate” function, and even this one comes from a special (ddwrt) Microsoft namespace.

Anyhow, instead of working with XSL, trying to create some crazy Template to handle this task, we can simply use JavaScript.

I assume here that you worked with SharePoint Designer 2007 and Data Views before.

You’ll have to add a JavaScript function first. You can add/link it to the page, page layout or master page, does not matter. I will use slightly modified script from “The JavaScript Source”.

<script type="text/javascript">
 var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
 function countup(dat) {
  var dt = new Date(Date.parse(dat));
  var yr =dt.getYear();
  var m =dt.getMonth();
  var d =dt.getDate();
  var today=new Date();
  var todayy=today.getYear();
  if ((navigator.appName=="Microsoft Internet Explorer") && (todayy<2000))
   todayy="19"+todayy;
  if (navigator.appName == "Netscape")
   todayy=1900 + todayy;
  var todaym=today.getMonth();
  var todayd=today.getDate();
  var todaystring=montharray[todaym]+" "+todayd+", "+todayy;
  var paststring=montharray[m]+" "+d+", "+yr;
  var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1);
  document.write(difference + " day(s)");
 }
</script>

Now we need to call this JavaScript function from Data View. Just click into one of the respectful cells in your Data View, and switch to Code View. You’ll see something like this:

<td class="ms-vb">
 <xsl:value-of select="ddwrt:FormatDate(string(@Modified), 1033, 5)" />
</td>

Now we’ll edit it, adding the JavaScript Call:

<td class="ms-vb">
 <script language="javascript">countup("<xsl:value-of select="ddwrt:FormatDate(string(@Modified),1033,1)" />");</script>
</td>

Vuala!


PS: I did not add any screen shots here assuming that it is fairly easy, but if you need some, just leave some comments.

SharePoint 2007 / MOSS URL Zones are limited

Ever wondered if there is any way to add additional zone to the list of URL Zones in SharePoint Central Administration? Sorry guys, no way. It is actually a predefined enumeration defined in Microsoft.SharePoint.dll

public enum SPUrlZone
{
    Default,
    Intranet,
    Internet,
    Custom,
    Extranet
}

Sunday, April 12, 2009

SharePoint 2007 / MOSS Page Refers Old Layout

Recently I was working a lot with SharePoint 2007 / MOSS Publishing Pages and Layouts. I noticed a weird behavior when I was moving some of the pages from dev environment to production. Sometimes the link to page layout is getting stuck and still pointing to an old (dev) environment. It was not obvious to find a way to fix it…

First I tried just exporting those pages to my local disk and editing it in notepad and putting it back. This approach did work, but was a little bit too manual…

After some head scratching I finally found a Page Settings page. When your page editing toolbar is visible (you clicked on “Site Actions” ->“Edit Page”), you can click on Page dropdown and select “Page Settings and Schedule”. On that page you can control Page Title and Description, Publishing Schedule, Page Layout, Audience Targeting and Page Contact. Alternatively you can just browse to it http://your-sharepoint-site/_layouts/PageSettings.aspx?Page=<Page ID>.

I had a few situations when it was still not working, and I was getting an error trying to change layout settings. You can always edit it in notepad in that case, as I described earlier.

Wednesday, April 8, 2009

Thicket “_files” folder and SharePoint 2007 (MOSS)

I got into this one when I was asked to put some Microsoft Publisher generated files into SharePoint 2007 site. Basically I got “index.html” file and “index_files” folder with some images and html files inside. I did put the index file into the root, then created the Document Library named “index_files”. Also I uploaded all the files into it without any problems using “Open in Windows Explorer” functionality. Everything actually started to work well. Then I was surprised that I don’t see this document library in SharePoint Designer. When I opened it from the browser – it was empty. But it was still showing all the files when opened with Windows Explorer. When I tried to change document library settings to work with content types, the whole settings page started to throw error.

I did some research and it looks like SharePoint 2007 has some native support of so called “thicket” folder (“_files” folder). SharePoint does not allow creating such a folder when you don’t have a corresponding (parent) file. When you try to create such folder using SharePoint Designer, it appends its name with an underscore at the end. SharePoint web interface hides content of such folder, while keeping those files accessible from Windows Explorer or when you browse/refer it directly.

My advise – try to avoid thickets in SharePoint. Those are hard to manage there.