28 Dezember 2007

Get or set the content type of a list item programmatically

Keith asked my last week, how you can get the content type of a list item. It's quite easy:

First you have the get the Guid of the field "ContentType". With this Guid you can access the properties bag of an instance of a SPListItem. There the content type is stored as a string.
SPListItem item = ...
Guid fieldGuid = item.Fields.GetFieldByInternalName("ContentType").Id;
item[fieldGuid] = "Document";
item.Update();

WebPartZone Adapter

Some weeks ago, the Accessibility Kit for SharePoint (AKS) was released. Unfortunately the AKS didn't solve all our problems. If you try to build a public website with Micorsoft Office SharePoint Server 2007 (MOSS) and pay attention to a clean structure of your HTML code (say: no table), perhaps this piece of code you help you!

When you place WebParts on your public website, then these WebParts are rendered in a HTML TABLE. This is because WebParts are placed in a WebPartZone and these are rendered with tables.

This behavior can be fixed with a ControlAdapter. Our ControlAdapter render the WebPartZone in a HTML DIV, if the user (logged-in or anonymous) is viewing the page. If the author edits the page, then the WebPartZone is rendered the "traditional" way. With this trick, we have a clean and modern HTML structure and the author have still all the (drag&drop-) functionality while editing the page.

Copy the following C# code in a file called AKS_WebPartZone_Adapter.cs and place it in the AppCode directory under your server root directory (like C:\Inetpub\wwwroot or C:\Inetpub\wwwroot\wss\VirtualDirectories\80).


Now you have to edit the compat.browser file which in the AppBrowser directory located in your server directory and add the following piece of code after the opening tag:
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.WebParts.WebPartZone"
adapterType="AKSAdapters.AKS_WebPartZone_Adapter" />
</controlAdapters>
</browser>