Wednesday, September 17, 2014

properties.AfterProperties[“Title”] always null in document libraries

Like the title says,

If you are writing an event handler for document libraries / lists, properties.AfterProperties is usually a good way for you to get the new values that the user modified.

Now, we all know the fact that in lists you have to use internal name and in libraries you have to use display name, right?

So, it appears it is not the only trick up their sleeve…

If you ever tried getting the title or name columns in a document library you will quickly notice AfterProperties and BeforeProperties will always return null.

Now, I came across this technet discussion:

And at the very bottom, there is an answer by Helm Ifort that I found very interesting.

It appears the title column value is available in AfterProperties, only you have to use the “vti_title” name for it! (STS 2001 anyone?)

In my book, this is a bug, and an undocumented one at the very least… I hope someone fixes it soon, but for now it is one of those things seasoned SharePoint sharks need to remember.

Hope this helps


Kiran Joshya said...
This comment has been removed by a blog administrator.
Gil Roitto said...

Thanks! Helped me solve the problem quickly. Some code to update title based on filenam. Works for Document Library and Picture Library. For List replace with ["Title"]

public override void ItemAdding(SPItemEventProperties properties)
string currTitle = properties.AfterProperties["vti_title"] as string;
string url = properties.AfterUrl;
var name = url.Substring(url.LastIndexOf('/') + 1);
//NOTE! Name is only copied to Title if title is not set. Will not handle name changes!
if (string.IsNullOrEmpty(currTitle))
properties.AfterProperties["vti_title"] = name;