Changing the title of an SPFile object.
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.
February 3rd, 2006 at 02:41
hey buddy,
thanx a lot for the sugestion as i was looking for the same since last 3 days. and its a gr8 help to me.
Thanx verymuch once again….
June 21st, 2006 at 11:51
Hi,
Let me explain the full scenario. I am trying to make an application which will copy some files from one Team site to the Portal Server. Now, i have changed the context menu and added a new item to it - Upload to portal. while cilcking on it, it goes to a page which shows the document listings of portal and asks which doc lib we want to post the document to. Now, while clicking on it, i want to check the meta data for the destination Doc Lib. If there are some mandatory metadata, then the which are not there on the Source DocLib, it will ask the user about it, and on clcking upload it will upload the file and associate that metadata to it. Do you have some idea about it?
Please give me a reply if you have solution to it.
Regards,
Manik Mittal
October 20th, 2006 at 04:45
you a lamerz
September 26th, 2008 at 06:25
Thanks a lot!, it works like a charm.
BTW, your english is perfect.