WSS Practice: Upload image to a Pictures Library

I was trying to upload images to a Pictures Library, and getting thumbnailing working, and so on. I found that thumbnailing wasn’t working, so I kept trying to create and upload my own thumbnail, which was pretty dumb. SharePoint will do that for you – but you’ve got to upload your file with a correct extension for an image format… Continue reading “WSS Practice: Upload image to a Pictures Library”

WSS Practice: Upload image to a Pictures Library

Fast access items in an SPListCollection

What’s the fastest way to access an SPList in an SPListCollection? Well, I can see two alternative ways of getting the list, so I thought I’d test them…

First off, we could get the item by just trying to get it and catching the exception if we fail:

SPList listVariable;
try {
listVariable = mySite.Lists["ListIWant"];
} catch (Exception e) {}

Or we could loop through the lists:

SPList listVariable;
foreach( SPList tempList in mySite.Lists){
if(tempList.Title == "ListIWant") {
listVariable = tempList;
break;
}
}

So, to test this, I built a little test application on the console.

What I found was that loop was always faster. I didn’t try with really large numbers of lists (I only tried up to 20), but I did find that for any probability of the list existing, looping through all the lists was much, much faster (at least 4 times, when the probability of the list we’re looking for was 1; i.e. the list definitely existed!)

There did seem to be a slight load time the first time we accessed the SPWeb.Lists collection, but you have to do that for either method.

Fast access items in an SPListCollection

WSS Practice: Finally used SPJobDefinition…

One task that I’ve been threatening to do for ages is to build a SharePoint Timer Job. Often a solution needs a bit of code to run periodically, but so far it wasn’t something that I’d tried. Well, I had a go today following Andrew Connell’s MSDN article, and it was very good. Here are my notes though…

Continue reading “WSS Practice: Finally used SPJobDefinition…”

WSS Practice: Finally used SPJobDefinition…

Make SharePoint open Excel files on a specific Worksheet

We’ve a customer who wants Excel Workbooks in one of their libraries to always open on a specific worksheet. Normally, Excel opens from SharePoint showing whatever the last selected tab was, but they’d like theirs to always open on the first worksheet.

I was curious, so I set up a document library with a template XSLX file. In it, the 4th worksheet was the active one when I saved the workbook as the template.

I created a new document in the library with that template. When Excel opened, it showed me the 4th tab, as expected. I saved the document, and downloaded a copy. I reopened the document from SharePoint, selected the first tab, saved the document and downloaded a second copy.

So, now I have 2 XLSX file for the same workbook, but with different tabs selected. I changed their extensions to .zip, and unzipped them. Next, I ran WinMerge on the folders they unzipped to to see what the differences were.

There weren’t many really – most were related to ‘last saved time’ and things like that. There were three XML files in the archive that seemed relevant – 2 for the worksheets (the one that was selected and the one that is now), and the Workbook xml.

Here are the differences for one of the WorkSheets:

Yup, the difference is the tabSelected=1 attribute. If it there, that’s the selected tab.

Or is it? The WorkBook xml also contains a difference:

This is a little more complicated – activeTab seems to be the index number into the Sheets node, if you treat it as a zero-based array. And if the first tab is selected, there doesn’t seem to be an activeTab element at all. Still, not too bad, I’m sure I could work with that.

So, how would this help? Well, if the customer is really keen that the first tab is always selected, then we could write an event receiver that captures a document uploaded to their library and then:

  • Checks it really is an Excel 2007 file
  • If it is, opens it up
  • Edits the XML we’ve just seen
  • Resaves the file.

Pretty straight forward, really.

Make SharePoint open Excel files on a specific Worksheet

Merged Data Sources for the DataView web part

I’d a question from a reader (gee, I have readers!) about doing content rollup using the DataView web part. He was asking if he could use the DataView web part even though the things he was pulling into his Data Source didn’t all have the same columns. Would this work? Well, one way to find out… Continue reading “Merged Data Sources for the DataView web part”

Merged Data Sources for the DataView web part

How to get an SPWeb object from a URL

One of the problems with SharePoint is that it’s very difficult to figure out what site is specified by a URL. After all, the URL to a particular page contains:

  • the Server
  • possibly (but not necessarily) a managed path and site collection
  • possibly (but not necessarily) a site
  • possibly (but not necessarily) a folder (such as ‘/lists/’)
  • possibly (but not necessarily) a list/library name
  • possibly (but not necessarily) a folder in a Library
  • the item itself.

Suffice to say, with all those optional bits, decomposing a URL to find the site is really hard. There is, however, a slightly obscure way of find this. You can create a site collection (SPSite) with a full URL, and then simply call OpenWeb() without any parameters to return you the site (SPWeb):

string path = "http://example/examplesite/_layouts/settings.aspx";
try
{
using (SPSite siteCollection = new SPSite(path))
{
using (SPWeb site = siteCollection.OpenWeb())
//Do something with the site
}
}
}

I found this when looking at the MSDN docs for SPSite.OpenWeb(). Check out the examples in there.

It’s a little weird that the SPSite object remembers information about how it was opened like that. But it is useful to know.

How to get an SPWeb object from a URL

People Picker shows Disabled user accounts

Something I didn’t know about – the people picker shows disabled user accounts – and ‘Tales from the field’ has a solution (on a per web app basis). Just in case the blog ever goes down:

This default behavior can be changed on a per web application basis to return only enabled user accounts. In the example below I have configured this for the web application http://moss using stsadm. This command configures MOSS 2007 to use a custom LDAP query.

stsadm -o setproperty -pn peoplepicker-searchadcustomfilter -url http://moss -pv (!userAccountControl=514)

If you are interested in putting together more granular filters I strongly recommend the following guide on LDAP, LDAP Query Basics – http://technet.microsoft.com/en-us/library/aa996205(EXCHG.65).aspx

Nice one Brendan

People Picker shows Disabled user accounts

CQWP: XSL to show you the fields on an item

A modification on the XSL I’d used previously – this gives a more readable presentation, and displays the values:

<xsl:template name="ShowFields" match="Row[@Style='ShowFields']" mode="itemstyle">
<div style="border:1px #aaa solid;background-color:#eee;margin:5px;padding:5px">
<xsl:for-each select="@*">
<xsl:value-of select="name()" /> = <xsl:value-of select="." /><br />
</xsl:for-each>
</div>
</xsl:template>

CQWP: XSL to show you the fields on an item