Ok so I have a request from a user and I'm not sure how I can achieve this...I'm thinking workflows but not sure.
Basically what she wants is to have a Document Library where she can upload documents. Each document will have a file name like this "ClientA - Customization.docx". Now you can imagine that a she will upload many documents that will have an identical name so what we want to do is number the files automatically. So if she uploads the first document, SharePoint will add a number after the name so the file will be called "ClientA - Customization - 1.docx" and then if she uploads a second document and it will be called "ClientA - Customization - 2.docx".
So basically SharePoint needs to check if the name of the current document exists in the list and if it does add a number next to it, but the number must be 1 greater than the current highest document so it will just increase.
Is there any way to do this? I looked into workflows but couldn't really get anywhere since I could not find an action to change the file name. Any ideas?
Thank you!!!
EDIT: I have gotten this far by posting on forums and adding my own code but I can't seem to get it working, is he code ok? Basically it does not rename anything that I upload to the document library so maybe my event receiver is not attached? At the bottom is also the code I am using in a console app to attach the EventReceiver to my Document Library.
//--This code is in the EventReceiver
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPListItem item = properties.ListItem;
if (item["Name"] == null)
return; //or better yet, log
string oldFileName = item["Name"].ToString();
int positionOfPeriod = oldFileName.LastIndexOf(".");
string tempFileName = oldFileName.Substring(0, positionOfPeriod);
SPQuery query = BuildArbitraryQuery(properties.List, "Name", tempFileName, true);
int count = properties.List.GetItems(query).Count;
String fileName, fileExtension;
if (positionOfPeriod == -1)
{
fileName = oldFileName;
fileExtension = "";
}
else
{
fileName = oldFileName.Substring(0, positionOfPeriod);
fileExtension = oldFileName.Substring(positionOfPeriod);
}
string newFileName = fileName + "-xx" + count.ToString() + fileExtension;
item["Name"] = newFileName;
Console.WriteLine("New File Name: " + newFileName);
try
{
properties.Web.AllowUnsafeUpdates = true;
EventFiringEnabled = false;
item.Update();
}
finally
{
properties.Web.AllowUnsafeUpdates = false;
EventFiringEnabled = true;
}
}
public static SPQuery BuildArbitraryQuery(SPList list, string columnDocumentName, string value, bool deepSearch)
{
if (list == null)
throw new ArgumentNullException("You cannot pass a null list to Helper.BuildArbitraryQuery.");
if (!list.Fields.ContainsField(columnDocumentName))
throw new ArgumentException("The SharePoint List \"" + list.Title + "\" does not contain the Field \"" + columnDocumentName + "\".");
string internalName = list.Fields[columnDocumentName].InternalName;
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name=\"" + internalName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";
if (deepSearch)
query.ViewAttributes += "Scope='RecursiveAll'";
return query;
}
//this is the code in the console app to add the EventReceiver to the doc library
using (SPSite site = new SPSite("http://servername:port/teams/dps/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Requests"];
SPEventReceiverDefinition def = list.EventReceivers.Add();
def.Assembly = "DocumentLibrary_ClassLib, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=611205b34d18f14d";
def.Class = "DocumentLibrary_ClassLib.EventReceiver";
def.Type = SPEventReceiverType.ItemAdded;
def.Update();
Console.WriteLine("Registered");
Console.Read();
}
}
*I really want this working so I am giving my reputation points away to someone that can help me get it working, pleeeaase :)
