Solved

Event Receiver SPItemEventReceiver for receiving a sharepoint file

Posted on 2013-01-09
7
1,094 Views
Last Modified: 2013-01-10
Hello Experts-

Can this be accomplished:
I have a document that contains in the file name, Type of Document, Total Amount of Document, and Date of document. For example:

TheChesterfieldClub_Equipment_$4,320.00_12-10-2012.PDF

I have in Document properties on the SharePoint Website: Type, TotalAmount, DateOfDoc

Can I populate these fields with SPItemEventReceiver  for ItemAdded ?
(The first part, ChesterFiled is a variable lenght and not a fixed length).
I can put in the title  any type of deliminator to seperate the fields, if that will help.

If not, my other thought was: Since I am uploading a file from a WFC program, it converts it to a PDF before uploading using Crystal Reports export options, I could somehow figure out how to manipulate the meta data before it uploads then somehow, figure out how to populate the Document properties with this metadata.

I like choice #1 first, as it seems like a lot less coding if it can be done.
0
Comment
Question by:BKennedy2008
  • 5
  • 2
7 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 38760590
Hi,

here we go. Just add this code to an EventReceiver bound to the document library (and replace the field names to your situation):
       public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            string tempFileName = properties.ListItem["Name"].ToString();
            int fileExtPos = tempFileName.LastIndexOf(".");
            if (fileExtPos >= 0 )
                tempFileName= tempFileName.Substring(0, fileExtPos);

            string[] fileInfoParts = tempFileName.Split('_');
            properties.ListItem["DocType"] = fileInfoParts[0];
            properties.ListItem["TotalAmount"] = fileInfoParts[1];
            properties.ListItem["DateOfDoc"] = fileInfoParts[2];
            properties.ListItem.Update();

        }

Open in new window


Works in my test dev like a charm.

HTH
Rainer
0
 

Author Comment

by:BKennedy2008
ID: 38760655
Looks awesome, I will give it a try.............
0
 

Author Comment

by:BKennedy2008
ID: 38760664
Do you know of a way to check in the document, once the Event Receiver executes the ItemAdded event?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 38760757
Hi,

that sound like a second question ;-)

The question is, why do you want to avoid/override Check-in?
If you do not want then just set the property "Require Check Out" on the library (Library Settings -> Versioning) to "No".

Otherwise, here the code:
            if (properties.ListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
                properties.ListItem.File.CheckIn("Automatically checkin");

Open in new window

0
 

Author Comment

by:BKennedy2008
ID: 38760773
My Bad about the second question........I have to split, but will be doing this first thing tomorrow. Much appreciated...
0
 

Author Comment

by:BKennedy2008
ID: 38762588
OK-
What did you different that I didn't do?
Here are the steps that I did:
- Added 3 Text fields to the Web Site: DocType, DateOfDoc, and TotalAmount.
- Added in the code below to an EventReceiver.
-Modified the sequence number for elements.xml (Changed to 10006)
-Disabled my other eventreceiver on the website (It also has a ItemAdded that copies the file to local drive) I disabled for troubleshooting.
-Deployed the eventReceiver to the site successfully
-Double checked the fields were correct, and verified the event Receiver was activated on the site.
-Uploaded a file titled "Starlan_Supplies_01-07-2012_$359.95" both ways (Manual through the web, and also through my SharePoint upload program.
-The file is received but nothing gets populated in the DocType, DateOfDoc, and TotalAmount fields.

Did I miss something here?
Here is the code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace ProcessReceivedFiles.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            string tempFileName = properties.ListItem["Name"].ToString();
            int fileExtPos = tempFileName.LastIndexOf(".");
            if (fileExtPos >= 0)
                tempFileName = tempFileName.Substring(0, fileExtPos);

            string[] fileInfoParts = tempFileName.Split('_');
            properties.ListItem["DocType"] = fileInfoParts[0];
            properties.ListItem["DateOfDoc"] = fileInfoParts[1];
            properties.ListItem["TotalAmount"] = fileInfoParts[2];
            properties.ListItem.Update();

        }

    }
}
0
 

Author Closing Comment

by:BKennedy2008
ID: 38763706
Works Like a champ, must have been blury eyed when I created the event Receiver Last night.

Thanks a ton....
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBScript Issues 8 40
excel 2016 program to loop through scripts 6 38
VB Script: Correction backup files and folders v3 11 28
Clear Filter 8 60
Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question