Adding file of the same name into the same list and unable to update name of the SPFile

Posted by BeraCim on Stack Overflow See other posts from Stack Overflow or by BeraCim
Published on 2010-04-10T08:46:17Z Indexed on 2010/04/10 8:53 UTC
Read the original article Hit count: 326

Filed under:
|

Hi all:

I'm having difficulties adding file of the same name in the same list and subsequently updating/changing/modifying the name of a SPFile.

Basically, this is what I'm trying to do:

string fileName = "something"; // obtained from a loop -- loop omitted here.
SPFile file = folder.Files.Add(fileName, otherFile.OpenBinary());

The Add method will generate a runtime exception when it finds another file of the same name in the same list/folder. So I thought of changing the file name later in the process:

string newGuid = Guid.NewGuid().ToString();
SPFile file = folder.Files.Add(newGuid, otherFile.OpenBinary());
// some other processing... afterwards, rename the file
file.name = fileName;
file.Item.Update();

A few minute of googling indicated I need to either move the file around, or update the name field by using ["Name"] instead.

I was wondering are there any other better ways to get around this problem?

Thanks.

© Stack Overflow or respective owner

Related posts about sharepoint

Related posts about sharepoint2007