Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

adding Items to folder in sharepoint list programatically ??

I am currently doing it this way.. the folder gets created and the list item is also created but only with the default fields values..

  public static void FolderExists(SPWeb web, string folderName, SPList list, SPListItem listItem)
  {
            //Check folder exist in the SPWeb or not 

        SPListItem listFolder = null;
        bool folderExists = false; 
        // Check to see if folder already exists, if not create it
        for (int i = 0; i < list.Folders.Count; i++)
        {
            if (list.Folders[i].Folder.Name == folderName)
            {
                listFolder = list.Folders[i];
                listItem = list.Items.Add(listFolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);
                web.AllowUnsafeUpdates = true;
                listItem.Update();
            }
        }

        // The folder does not exist so we create it and add the item

        if (!folderExists)
        {
            listFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
            listFolder.Update();
            listFolder["Title"] = folderName;
            listItem = list.Items.Add(listFolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);
            web.AllowUnsafeUpdates = true;
            listItem.Update();
            list.Update();
        }
    }
share|improve this question

3 Answers 3

Well you're missing the code to set the fields to anything but the default

The following lines in your code creates a new empty item and saves it

 listItem = list.Items.Add(listFolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);        
 web.AllowUnsafeUpdates = true;        
 listItem.Update();  

You need to change that into something that copies the values from your passed in listitem:

var newListItem = list.Items.Add(listFolder.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null); 
web.AllowUnsafeUpdates = true;
foreach (var field in listItem.Fields)
{
    if (field.ReadOnlyField
     || field.Id == SPBuiltInFieldId.Attachments)
        continue;
    newListItem[field.Id] = listItem[field.Id];
}
newlistItem.Update(); 
share|improve this answer

There is easier way how to check whether folder exists or not:

var path = "dir/subdir/subsubsub/";
if (!list.FolderExists(path))
{
  //create folder - imho you have to create each folder 
  //in path separately, keep it on mind when calling create folder
  list.CreateFolder(path);
}

Here comes the code to add file to document library

SPFile newFile = folder.Files.Add(fileName, file.Data, properties);
if (newFile == null)
  throw new InvalidOperationException("Error saving file");
newFile.Item["Title"] = fileName;
newFile.Item.Update();
newFile.Update();    
share|improve this answer

This code is very incorrect way to loop through the folder items in sharepoint .Please use CAML query to loop folder in SP. If there are 150000 records, the page will take 90sec to load.Also never use list.Items.Add() instead use listitems.add().

Use the code while finding a folder :

SPQuery query = new SPQuery(); 
query.Query = "<Where><And><Eq><FieldRef  Name='LinkTitle'/><Value Type='Text'>" +folderName + "</Value></Eq><Eq><FieldRef   Name='FSObjType'/><Value Type='Lookup'>1</Value></Eq></And></Where>";  
SPListItemCollection items = list.GetItems(query);
if (items.Count == 0){Create a folder}
else{
SPListItem listItem = list.AddItem(folderItem.Folder.ServerRelativeUrl,SPFileSystemObjectType.File, null);}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.