WSS Practice: Checking in/out Documents, and Versioning

Well, for the WSS application developer exam, you’re supposed to know about programmatically checking in and out documents:

//*** Check out a document
Console.WriteLine("Check out {0}", myItem["Name"]);
myItem.File.CheckOut();
Console.ReadLine();
//*** Check in a Document (Minor Check in)
myItem.File.CheckIn("Checked in Programmatically", SPCheckinType.MinorCheckIn );
Console.WriteLine("Item Checked in");
Console.ReadLine();

One should also be able to list all versions of a document:

//*** List all versions of a document
Console.WriteLine(myItem.Name);
foreach (SPFileVersion ver in myItem.File.Versions)
{
Console.WriteLine(" - v{0} ({1})", ver.VersionLabel, ver.CheckInComment);
}
Console.ReadLine();

And restore a version:

//*** Restore to version 1.0
myItem.File.CheckOut();
myItem.File.Versions.RestoreByLabel("1.0");
myItem.File.CheckIn("Restored by Program to v1.0");
Console.WriteLine("{0} restored to v1.0", myItem.Name);

All pretty straight forward.

WSS Practice: Checking in/out Documents, and Versioning

WSS Practice: Attach files to ListItems

Similar to uploading a file, how would we attach a file to an SPListItem? This is slightly different, as a listitem may have more than one attachment….

Well, my code is:

byte[] bytes = File.ReadAllBytes(@"c:somefile.txt");
myListItem.Attachments.AddNow("somefile.txt", bytes);

Yup, not quite the same. I guess it’s ‘cos for Document Libraries, the document is the item, whereas for normal lists, the items may have zero or more documents attached.

Actually, I must try attaching a file to a Document item. I wonder if you can do that?

Edit: Nope, you can’t. Attachments are disabled, and if you try and enable them you get the SPExceptionAttachments are not allowed for Document Libraries and Surveys“. Which is fair enough.

WSS Practice: Attach files to ListItems

WSS Practice: Uploading a document

Well, I’ve mentioned uploading via HTTP before, but I must confess, I’d not used the object model. Well, it turns out that it’s just the same as uploading an image (just without thumbnails getting generated…
SPList docs = site.Lists["Documents"];
FileStream fs = new FileStream(@"c:SomeDocument.txt",FileMode.Open,FileAccess.Read);
docs.RootFolder.Files.Add(docs.RootFolder.Url + "/SomeDocument.txt",fs, true);

WSS Practice: Uploading a document

WSS Practice: Create a Calendar Event

Another thing on the WSS syllabus is “Add a recurring event to a calendar”. Well, I gave it a go, and failed. It was actually pretty tough. In the end, I found myself referring to ‘How to: Add a Recurring Event to Lists on Multiple Sites‘. It’s a pretty good article, and talks about creating a Meeting Workspace and attaching it too, but the short version of it is my code… Continue reading “WSS Practice: Create a Calendar Event”

WSS Practice: Create a Calendar Event

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…