Solved

Event Receiver SPItemEventReceiver for receiving a sharepoint file

Posted on 2013-01-09
7
1,042 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now