Solved

Event Receiver SPItemEventReceiver for receiving a sharepoint file

Posted on 2013-01-09
7
1,130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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