Adding List items into a List Folder

Adding a list folders were look simple, but adding items inside list folders has got little different way. I was little confused when seeing the list.Items.Add() having a only below 3 over loadings.

 

1) SPListItem item = list.Items.Add()

2) SPListItem item = list.Items.Add(string folderUrl, SPFileSystemObjectType underlyingObjectType)

3) SPListItem item = list.Items.Add(string folderUrl, SPFileSystemObjectType underlyingObjectType, string leafName)

 

The SPFileSystemObjectType enum has got only File, Folder, Invalid & Web . As my list is not a document library I was thinking that I cannot use any of these enum options. But I was wrong, when I saw MSDN I found that SPFileSystemObjectType .File is the option to be used. Following few lines of code will let you know how you to add items inside a Folder.

 

Note: Text that are in RED to be replaced with appropriate values.

 

using (SPSite site = new SPSite (SITE URL ))

{

  using (SPWeb web = site.OpenWeb())

  {

     SPList list = web.Lists[LIST NAME ];

    if (list != null )

    {

       web.AllowUnsafeUpdates = true ;

       // create a list item entry into the folder

SPListItem item = list.Items.Add(“/Lists/LIST NAME/FOLDER NAME ”,       SPFileSystemObjectType .File, null );

item["Title" ] = TITLE ;

      item["Field1" ] = value ;

      item["Field2" ] = value ;

      item["Field3" ] = value ;

      item.Update();              

    }

  }

}

 

If you know the list folder name, then use the below method, if you have the list older object, instead of string /Lists/LIST NAME/FOLDER NAME you can use itemFolder.ServerRelativeUrl .

One last final word, use relative URL when ever adding a folder items; this is the very common mistake done by lot of people, which may eat couple of trouble shooting hours to fix.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值