Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Event Receiver SPItemEventReceiver for receiving a sharepoint file

Posted on 2013-01-09
7
Medium Priority
?
1,189 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 2000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

963 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