sharepoint 2010 use attachment name to update name column

Hi <o:p></o:p>

When a attachment is added as part of a new item to list or document library, how can I set up SP
so it automatically pulls out the name, date etc from the attachment name to auto populates columns I have added (name, date)?The attacment name format is always: Name_date


September 2nd, 2015 10:43am

Hello Michuk,

In a document library, there is no attachment. We upload documents. Code for document library and list could be different because list can have more than one attachment.

You can create an event receiver to split the attachment name and update the columns you want. You can put your code in ItemAdded method. It will run after you press save button.

Here is a sample code for your reference, it is using Server Object Model. It will update FileName column to attachment file name. You need to modify it if you have more than 1 attachment.

public override void ItemAdded(SPItemEventProperties properties)

{

        SPListItem lstItem = properties.ListItem;

     string listAttachmentName = lstItem.Attachments[0]; // get first attachment name for list

     string[] listAttachemtNameSplit = listAttachmentName.Split('_'); //split the attachment   name for list

     lstItem["FileName"] = listAttachemtNameSplit[0]; //first item in array is attachment file name

         lstItem.Update();  

}

 

Here are some information about event receivers:

https://msdn.microsoft.com/en-us/library/office/gg252010(v=office.14).aspx

https://msdn.microsoft.com/en-us/library/office/gg749858(v=office.14).aspx

 

Thank you





  • Edited by ali tun 1 hour 45 minutes ago
Free Windows Admin Tool Kit Click here and download it now
September 4th, 2015 1:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics