views:

448

answers:

1

Hi there. Sorry if this has been asked elsewhere. I have looked but can't find any definitive answers.

I'm writing an app for Sharepoint 2010 that needs to create folders in a document library; one for each "job" that the app processes, as a place to put the job output. But, I'm having problems with folder name collisions. Each "job" is encoded as an xml file in another sharepoint list. For example, it might contain an xml file called "from docx to pdf.xml". So far I have the app creating subfolders in the output list using the job files name minus the extension. So, a folder called "from docx to pdf" in this case. But, some time later, the app might have to re-process the exact same job. I want to be able to have another subfolder in the same list as the first, with the exact same name visible to the user in a browser...

Can you do this in Sharepoint lists? It seems ordinary SPListItems have Name, DisplayName and Title properties. Obviously, one of these must be unqiue, so that Sharepoint can uniquely identify that item. but which is it? And does the same apply to SPFolder items in a list? I guess here I want to have something like duplicated folder display names, but unique internal names. Any ideas on how to do this? So far, my crappy method goes something like this:

 private SPFolder CreateSubFolder(SPList list, string visibleFolderName)
    {           
        // create a new folder under the root folder
        SPListItem newFolder = list.AddItem("", SPFileSystemObjectType.Folder, visibleFolderName);            
        newFolder.Update();               
        return newFolder.Folder;
    }

This obviously doesn't work. Any ideas on how to alter to have the same visible name, but diff internal names (perhaps using Guids...)?:D Thanks in advance.

A: 

Like in ordinary file systems, folders under the same sub folder must be unique. Thus, the last parameter of the Add() method should be unique, because it indicates the folder name.

You can safely asssign a duplicate title after creating the folder, using this piece of code:

SPListItem newFolder = list.Items.Add("", SPFileSystemObjectType.Folder, uniqueFolderName);
newFolder["Title"] = "New Folder"; // Can be duplicated
newFolder.Update();

Now you will have folders with same titles but different names. Still, when you try to browse these folders from Windows Explorer or SharePoint default list view, it will show you the folder name (which is unique), not the titles (which you want). So you need to create a custom view and display the title field instead of the folder name.

denni
Excellent! Thank you most kindly! MS normally overkills the documentation for it's technologies, but finding an explanation of the point you have elucidated is next to impossible (at least for SP2010). :D
Actually, this doesn't seem to work... But I think it's because SPFolders don't have a Title property, where as SPListItem does. Basically, I can't do what I wanted unless I created a subclass of SPFolder.. if that's even possible. Either way, question answered. Cheers.
If you noticed from my sample, actually the newFolder variable is an SPListItem object, not an SPFolder object, so yes, you can set the Title property.By the way, you can create your own ContentType which derives the Folder content type. But sadly, you can't subclass SPFolder from the API since it doesn't have public constructors.
denni