Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Event handler document name override

  Asked By: Keri    Date: Jan 20    Category: Sharepoint    Views: 2588

I have written an event handler at the request of one of my users to provide the
following functionality:

When they upload files to a specific document library they will be using a
specific 16 character file name for each employee. There could be multiple
records for each employe that need to added. So if the filename already exists
in the document library they want to add an extension onto the 16 character file
name. So, a group of files for one employee could be as follows:

Initial file = 16 character filename
Second file = 16 character filename + _1
Third file = 16 character filename + _2
etc...

This is my first time writing an event handler and using C#. I was hoping
someone that has experience with event handlers wouldn't mind taking a look at
my code below to tell me if I'm on the right track.


public override void ItemAdding(SPItemEventProperties properties)
{

//Declare variables.
string strName = string.Empty;
string strNameSplit = string.Empty;
int intcount = 0;
string strNewName = strName + "_" + intitemcount.ToString;

//Look to see if the filename of the uploaded file already exists
foreach (DictionaryEntry entry in properties.AfterProperties)
{
if (entry.Key.Equals("Name"))
{
strName = entry.Value.ToString();
strNameSplit = strName.Substring(0, 16);
}
}

SPList list = properties.OpenWeb().Lists[properties.ListId];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='" +
list.Fields["Name"].InternalName + "' /><Value Type='Text'>" + strNameSplit +
"</Value></Eq></Where>";

if (list.GetItems(query).Count > 0)



//If filename already exists add the count value + 1 to the end of
the filename.
{
intitemcount = list.GetItems(query).Count + 1;
list.Fields["Name"].InternalName.ToString = strNewName;

ItemAdding.update;


}
}

}
}

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Kristy Hicks     Answered On: Jan 20

Why do u want to put it in ItemAdding instead of ItemAdded?

 
Answer #2    Answered By: Amanda Brown     Answered On: Jan 20

Is that not the right place to put it? I need to change the file  name before it
gets added  to the document  library; so when the user clicks the Upload button
and selects the file(s).

 
Answer #3    Answered By: Carey Everett     Answered On: Jan 20

So I tried rewriting my code  and now when I build I get an "No suitable method
to override  on my ItemAdded override method. Not sure what I'm doing wrong. I
tried Googling the error and it didn't help. I also went through 20 pages of
Google results to help figure out what I'm doing right/wrong in my code and I'm
not clear. Can someone help me out?


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;


namespace HRRenameUploadDocs
{
class RenameDocuments
{
public override void ItemAdded(SPItemEventProperties properties)
{

//Declare variables.
string strName = string.Empty;
string strNameSplit = string.Empty;
int intcount = 0;
string strNewName = strName + "_" + intitemcount.ToString;
string strid = string.Empty;

try
{
//Look to see if the filename  of the uploaded file  already
exists
foreach (DictionaryEntry entry  in properties.AfterProperties)
{
if (entry.Key.Equals("Name"))
{
strName = entry.Value.ToString();
strNameSplit = strName.Substring(0, 16);
strid = strName.Substring(6, 11);
}
}

SPList list  = properties.OpenWeb().Lists[properties.ListId];
SPQuery query  = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='" +
list.Fields["Name"].InternalName + "' /><Value Type='Text'>" + strNameSplit +
"</Value></Eq></Where>";

if (list.GetItems(query).Count > 0)



//If filename already exists add  the count  value + 1 to the end
of the filename.
{
intitemcount = list.GetItems(query).Count + 1;
list.Fields["Name"].InternalName.ToString = strNewName;
list.Fields["SSN"].InternalName.ToString = strid;
}

else
{
list.Fields["Name"].InternalName.ToString = strName;
list.Fields["SSN"].InternalName.ToString = strid;
}


}
catch (Exception ex)
{
properties.ErrorMessage = string.Format("An error occurred
during execution of ItemAdding event: {0}.", ex.Message);
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
}
finally
{
//Stop other events from firing while this method executes
this.DisableEventFiring();

// save the new values

properties.ListItem.SystemUpdate();

//Enable event  firing again
this.EnableEventFiring();
}
}
}
}

 
Answer #4    Answered By: Anuj Lakhe     Answered On: Jan 20

You forgot to set your class to inherit from SPItemEventReciever and mark
the class as Public.

 
Answer #5    Answered By: Lee Dickerson     Answered On: Jan 20

It may sound like overkill but you should try uninstalling the solution
completely and doing an IISRESET. Then redeploy the solution. Sometimes I think
it ghosts a copy of a DLL on the server...or something like that.

 
Didn't find what you were looking for? Find more on Event handler document name override Or get search suggestion and latest updates.




Tagged: