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.
hey buddy,
Thanx verymuch once again….
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
you a lamerz
Thanks a lot!, it works like a charm. BTW, your english is perfect.
oFile.Item(“Title”) = “newTitle”
This line doesn’t compile as there is no Item method for the oFile object.
Item is a property and can be accesed using oFile.Item[“Title”] and it is read only!
I have not yet found any proper solution to set the title…
Sorry, I just realized you were using VB.NET not C#.
However, still the title doesn’t get changed and that property is read-only.
The code is definitely in Vb.net.
You can try oFile.Item.Item[“Title”] – because “oFile.Item” is an expression of type SPListItem, but “oFile.Item.Item” is a property which references the value of a particular property within that listitem returned by “oFile.Item”. Sorry, this sounds complex, i know.
Yes, and the other remark is that the code was written for SPPS 2003.
I just accomplished this after a bunch of trial and error. Basically just change the “BaseName” value of the SPListItem.
C# SPListItem item = CallSomeMethod();
item[“BaseName”] = “New Base Name”;
item.SystemUpdate();
Hope that helps.
Thank you very much for this, I had a problem with that, now it’s fixed :D
Thanks @ marlobello – was trying to do this for so long!! your a legend :)
Hmmmm it still isn’t working for sp2010…seems problematic this one