Solved

Event Receiver SPItemEventReceiver for receiving a sharepoint file

Posted on 2013-01-09
7
1,072 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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