1

I need to put together an Event Receiver on Asset Libraries which copies the File Name of the asset being uploaded to the Title Field of the list. I am currently using the following code. The problem with it is :

  1. The title field is empty when the user is prompted to enter the meta data after it is uploaded so no visual indicator is present.

  2. Since I am saving the record before the user clicks save an error is thrown saying someone already saved it.

I would like a seemless experience for the user so the Title field will be present in the meta data form.

public override void ItemAdded(SPItemEventProperties properties){

    base.ItemAdded(properties);

    string itemFileName = properties.ListItem.File.Name;

    SPListItem itemAdded = properties.ListItem;
    itemAdded["Title"] = itemFileName;

    this.EventFiringEnabled = false;

       itemAdded.SystemUpdate();

    this.EventFiringEnabled = true;
        }
0

Will this work? By updating Title in ItemAdding and ItemUpdating events, extra Updates are avoided (without DisableEventFiring), and the "Edit Properties view" will already have the Title filled in.

Some code example to update title based on filename. Works for Document Library and Picture Library and should work for Assets library. For working with Title in Lists, ["Title"] needs to be used.

public override void ItemAdding(SPItemEventProperties properties)
{
    base.ItemAdding(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;
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.