Storing Data within Web Parts

We have a customer who wants a custom web part similar to the Summary Links web part – that is, editors can add links in this web part (along with some other metadata). For various reasons, a ListView web part and a Links library weren’t what they were after.

Now, the Summary Links web part seems to me to be a bit unusual. Most web parts access and use data stored elsewhere – lists, web services, databases, etc.. However, the Summary Links web part stores it’s data internally. How does it do that, and can I do the same? Continue reading “Storing Data within Web Parts”

Storing Data within Web Parts

Search a single list, and don't use the OSSSearchResults.aspx page…

So, we’ve got a customer who’d hit the problem of the OSSSearchResults page that I’d mentioned before. To recap:

  • Global Searches in MOSS use a Search Center page for showing the results. These are nice, configurable, and can be made do quite a lot.
  • Contextual Searches (such as a particular list or site) use the standard WSS OSSSearchResults.aspx page. This isn’t nice and configurable, and changes will affect many sites.

Now, what would be great would be if we could have the contextual scope, but the global results page. Well, we can. Continue reading “Search a single list, and don't use the OSSSearchResults.aspx page…”

Search a single list, and don't use the OSSSearchResults.aspx page…

Page hit counter in MOSS

So, a customer of ours is upgrading to SharePoint 2007. One of the things they were looking for is a hit counter, so they can see the number of times a page was requested. This was easy for them to do in SharePoint 2003 – they’d just use FrontPage, and insert a Hit Counter web component. I’d never heard of such a thing build into 2007, but over morning coffee, I decided to give it a try. Much to my surprise, it works… …sort of.

There is a hit counter (albeit with an awful set of styles). It does work too:

Hit counter in SharePoint

That’s one of the better styles, believe it or not. So, what’s the problem with this?

Well, in normal pages, nothing. It counts; ’nuff said. However, publishing pages are a bit different. They store metadata in a ‘Page Content Type‘ and then use Page Layouts to render that data. What this means is that many things that appear to the user as separate pages actually use just one ASPX file for rendering their content – and this is where the hit counter doesn’t work. It counts the number of hits on the ASPX page – so instead of getting a number of hits on that page of content, you get the number of hits on content using that page layout. Which sucks a bit.

I’ll look into this a bit more soon, and see what I find. It can’t be that hard – apart from the whole ‘Page Layout’ thing, it’s not exactly rocket science.

Page hit counter in MOSS

Feature Stapling for Branding

As I’ve mentioned before, I reckon that from now on I’ll do all SharePoint branding through features alone – not using themes or the ‘choose master page’ page. Which is fine, and useful too – one of the questions that has been raised recently is how to automatically apply the branding when a new site is provided. Well, feature stapling is the way to do that.

For those who don’t know, feature stapling is creating a feature which associates another feature with a Site Definition. When a site is provided by that site template the associated feature is activated. So, for example, we might have a BrandingFeature feature, which does all of our setting alternate CSS, setting master pages, etc., and then use a BrandingStapler feature to associate that with some (or all) of our site definitions.

I won’t bother repeating the ‘how to’ of it, as there are plenty of good posts about it.

Feature Stapling for Branding

WSS Practice: Create a list, columns and view programmatically

Next task on my prep – create a list programmatically. Then I want to add some columns to it, and show these on the default view. Actually, it’s mostly pretty easy (apart from that last part). Continue reading “WSS Practice: Create a list, columns and view programmatically”

WSS Practice: Create a list, columns and view programmatically

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