SharePoint Custom List Definition Recipes
If you want a tutorial on how to create a custom ist definition in SharePoint, check MSDN or Andrew Connell’s post. Here, I’ll just provide several short recipes for some scenarios.
1. How to rename the Title column in the list definition?
Often, you want to rename the Title column in your list definition. Don’t do it, because many internal SharePoint functions requires it. Instead, just change the display name of the column.
You need to do this in the list definition’s schema.xml, not in the content type definition file. In fact, don’t mention the Title column in the content type. If you do, the Title column will appear twice in your list.
Here is what you should modify in your schema.xml:
<Fields>
<Field ID="{82642EC8-EF9B-478F-ACF9-31F7D45FBC31}"
Type="Computed"
Name="LinkTitle"
DisplayName="My Own Title" />
<Field ID="{BC91A437-52E7-49E1-8C4E-4698904B2B6D}"
Type="Computed"
Name="LinkTitleNoMenu"
DisplayName="My Own Title" />
<Field ID="{FA564E0F-0C70-4AB9-B863-0177E6DDD247}"
Type="Text"
Name="Title"
DisplayName="My Own Title"
Required="TRUE" />
<!-- Other fields declaration -->
</Fields>
Keep in mind that you will need those three columns declaration, one for the plain Title data, the others for the Title with link and menu which are displayed in the standard form. You can put your own text inside the DisplayName attribute.
2. What are the required attributes in the List and ListTemplate elements?
The List element in schema.xml is similar to the ListTemplate element in the feature manifest in the sense that they both have a similar set of attributes. A lot of attributes are duplicated and it often cause confusion. Usually, I would put every attributes I needed in both elements. But this is not a good practice, since if you need to change some attributes, you have to do it for both elements. So it’s better to put only the required attributes in each element. What are they?
MSDN actually has listed the required attributes for the List and ListTemplate elements. But, at least in my case, it’s not so accurate. So here is what I found:
Required on the List element:
- BaseType
- Direction
- Name
- Url
The MSDN article suggested that this attribute is optional. But if you don’t include this, you might get the following error when creating an instance of the list:
Exception occurred. (Exception from HRESULT: 0×80020009 (DISP_E_EXCEPTION)) at Microsoft.SharePoint.Library.SPRequestInternalClass.CreateListFromFormPost(String bstrUrl, String& pbstrGuid, String& pbstrNextUrl)
at Microsoft.SharePoint.Library.SPRequest.CreateListFromFormPost(String bstrUrl, String& pbstrGuid, String& pbstrNextUrl)
Required on the ListTemplate element:
- BaseType
- DisplayName
- Name
- Type
C# 4.0 and Dynamic Programming
In Microsoft PDC2008, Anders Hejlsberg — C# lead designer and Microsoft Technical Fellow — introduces the future of C#, dubbed C# 4.0. You can watch his recorded presentation at Channel 9. The following is a brief summary on dynamic programming, one of C# 4.0 new features.
C# Evolution
- C# 1.0: Managed Code
- C# 2.0: Generics
- C# 3.0: Language Integrated Query
- C# 4.0: Dynamic Programming
Nowadays, codes can reside in any kind of form, i.e., .NET objects, JavaScript, Python, Ruby, COM. In order to use the codes written in different form, currently we have some different approaches.
For example, to call a .NET object, we will write the following:
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
Now we try to call a .NET object too, but we don’t know which class exactly having the code:
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
While this is for calling a JavaScript object:
ScriptObject calc = GetCalculator();
object res = calc.Invoke("Add", 10, 20);
int sum = Convert.ToInt32(res);
Observe that there are different procedures to call different objects. With C# 4.0 dynamic keyword, we can use this instead of all those above:
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
The compiler will not check whether there is an “Add” method in the “calc” object. Instead, the DLR will bind the method call to the responsible object on runtime as shown in the following picture:

Anders stated that he did not have any intention to ask all developers to rewrite all their codes to be dynamic. In fact, he is a strong believer of static language strong features: statement completion, refactoring, compilation type check, and performance. But he also embraces the flexibility and productivity of dynamic languages such as Python and Ruby. And there are an increasing numbers of things that your application needs to talk to, that is not statically typed. You may need to talk to a JavaScript code, or maybe a Python or Ruby codes. The question is, should we make it painful to talk to them or make it easier?
For Anders, he wants the later. As LINQ intends to provide a unified way to access all kind of data source, dynamic in C# intends to provide a unified way to talk to all kind of codes, be it statically or dynamically typed.
SharePoint Bending: Remove “Send To” Context Menu Without Modifying Core.JS
This is becoming a pretty common requirement. Some people just don’t want the “Send To” menu appearing in the context menu of the document library. You can do it — customize the SharePoint context menu by — by simply making some modification in core.js as suggested by many.
But modifying SharePoint core files should not be done, because your modifications can be overwritten by future SharePoint updates. So here’s a little trick to remove the “Send To” context menu without modifying core.js:
- Go to the document’s view url, for example:
http://example.com/Documents/Forms/AllItems.aspx. - If it is a web part page, edit the page and add a Content Editor web part. Otherwise you need to edit that page in SharePoint Designer.
- Open the source editor (not the rich editor) for the Content Editor web part, or open the code view (not the design view) of that particular page in SharePoint Designer.
- Either inside the web part or the code view, insert the following script:
<script type="text/javascript">
AddSendSubMenu = function (m,ctx) {}
</script>
That script will redefine the AddSendSubMenu() function with a new implementation. Since the function body is empty, it simply won’t display anything, thus the “Send To” context menu is removed from the context menu.
You can do this on some pages manually, or if you want to apply this to all pages, just insert the code to your master page.
WSS/SharePoint Extension 1.2 for Visual Studio 2008
Finally it’s out, get it while it’s hot.
Now you SharePoint developers no longer need to hesitate to upgrade to Visual Studio 2008 if you haven’t.
This will greatly help development of SharePoint-based solutions when you’re using Visual Studio 2008 by providing the following features:
Visual Studio 2008 Project Templates
- Web Part
- Team Site Definition
- Blank Site Definition
- List Definition
- Empty SharePoint Project
Visual Studio 2008 Item Templates (items that can be added into an existing project)
- Web Part
- Custom Field
- List Definition (with optional Event Receiver)
- Content Type (with optional Event Receiver
- Module
- List Instance
- List Event Handler
- Template
SharePoint Solution Generator
- This stand-alone program generates a Site Definition project from an existing SharePoint site. The program enables developers to use the browser and Microsoft Office SharePoint Designer to customize the content of their sites before creating code by using Visual Studio.
Installing the WSS/SharePoint extension on Windows XP/Vista
If you are using Windows XP/Vista and try to install the extension, it will display the following error:
The product can only be installed if Windows SharePoint Services 3.0 has been installed first.
Instead of replacing your OS with Windows 2003, launching a Virtual PC image preloaded with SharePoint, or trying to install SharePoint on Vista, you can simply create a registry entry to fool the extension installer as if you have installed WSS on your XP/Vista machine. Before following these simple steps, as with any registry modification, do so with caution at your own risk. You can create a registry backup if necessary.
Basically — under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0 registry key — you need to add the following string value: Sharepoint="Installed".
Here is the step-by-step instruction:
- Run
regeditfrom command prompt orStart > Run. - Navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0. - If you cannot find the “12.0″ folder, right click on the
Web Server Extensions, selectNew > Keyfrom the popup menu, and enter12.0as the new key name. - Right click the “12.0″ key/folder, select
New > String Valuefrom the popup menu, and enterSharepointas the name. - Double click the newly created
Sharepointentry, and enterInstalledas the value data. - Now you can try to install the Sharepoint extension for VS 2008.
An easier way to do the above steps is to download the SharePointOnXP.reg file, extract, double click on the file. Then you can try to install the extension.
By the way, if you haven’t done so, you might also want to add SharePoint’s dlls to the GAC. Hence, Visual Studio won’t find troubles when compiling your SharePoint-based projects.
SharePoint Bending: Forefront Forbids Programmatically File Uploads
I had a custom page that needed to be deployed inside a MOSS 2007 site’s document library. The problem was, there were more than 100 sites in the site collection. Manually uploading the file was so time-consuming, so I decided to write a simple console application to upload the file to each site’s document library.
The application simply made use of the Microsoft.SharePoint.SPFileCollection.Add() method to upload the file programmatically to the desired document library. In my development environment, it worked flawlessly. So happily I went to the client and ran the application in the production server. Almost immediately, the application exited without any message. It’s strange because I made it to output the upload status as well as the exception message.
Then I tried to add a more verbose exception handling, by printing out the exception stack as well as the exception type, and I got this following output:
Exception class: Microsoft.SharePoint.SPException
Exception message:
Stack Trace:
at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite, String checkInComment, Boolean checkRequiredFields)
at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite)
See? No exception message. How the heck would I know what happened then? Well, at least I got the idea that the failure was at the Microsoft.SharePoint.SPFileCollection.Add() method execution. I ran the code under the administrator account, the same account used as the site’s administrator, so it shouldn’t be the account problem. I fired up the browser, tried to upload the file manually to the document library using the same administrator account, and it also worked. So definitely this was not a problem related to the permission.
After messing around with the site’s settings and permissions, rewrote my application’s code many times, as well as googled for solutions, I finally found out the real problem. It’s not the code or the permission problem. But it seemed that the production server had Microsoft Forefront Server Security installed. It was configured to scan for viruses on every file uploads and downloads.
So what I did to fix the issue is:
- Open SharePoint
Central Administration - Open the
Operationstab - Click on the
Antiviruslink under theSecurity Configurationsection - Turn off the
Scan documents on uploadsetting
Then I could successfully executed my application to upload the file to every sites.
This is a mere workaround to resolve this problem temporarily. I still need to find a more proper solution so my application can work with Forefront.