Ok, from now on, I’ll write SharePoint related posts here in English. However, English is not my native language, so I beg your pardon in advance for all those grammatical mistakes, style errors etc. I believe the use of commas is the most horrible part of it.
In Sharepoint it’s very easy to believe that a document library is actually a custom list with some “extras”. You can add property fields to a document library the same way you add them to a usual list, the AllItems.aspx form looks quite similar for both usual lists and document libraries etc. Even in the API code model you can work with a SPList and leave SPDocumentLibrary for use only in special cases.
This impression is a bit misleading, however. When it comes to work with an SPFile class instance, you discover that every file is associated with an SPListItem instance.
To read custom properties defined by a document library, we don’t need to use the SPListItem object, we can use the Properties property of SPFile class. For instance, if there is a field in document library named “FileOwnerDepartment”, we can code like this:
Dim oWeb As SPWeb
Dim oFile As SPFile
Dim sDept As String
oWeb = SPControl.GetContextWeb(context)
oFile = oWeb.GetFile("fileurl")
sDept = CStr(oFile.Properties("FileOwnerDepartment"))
To set a value of a custom property we have to use the SPFile.Item property, set the property of the SPListItem object and then update the SPListItem:
oFile.Item.Item("FileOwnerDepartment") = "someOtherValue"
oFile.Item.Update()
Due to the fact that Title property is mandatory in every list (generic list definition from onet.xml), there is a property in SPFile class named Title (as described in MSDN – The Title property of the SPFile class gets the display name of the file.)
If we try to access the title through Properties
property of a file, we get a Nothing
value. If we use the Title property, we get the actual title. That is, this script outputs two lines of “True”:
Console.WriteLine(oFile.Properties("Title") Is Nothing)
Console.WriteLine(Not oFile.Title Is Nothing)
Even though Title property is read-only (which can make you think there’s no legal way to change the title of a document), you can update it the same way you update any other field:
oFile.Item("Title") = "newTitle"
oFile.Item.Update()
The interesting part is that you can also update the title of a file using the localized name (there are language-packs for SharePoint, too) of title field. For instance, in Latvian version of SharePoint we have “Nosaukums” instead of “Title”. That is, both oFile.Item("Title")
and oFile.Item("Nosaukums")
actually change the same property – title.