Sunday, August 25, 2013

How to Add or Delete List Items in Sharepoint (Inside Folder)

Creating List Item

Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection   = mySite.Lists(TextBox1.Text).Items
Dim item As SPListItem = listItems.Add()
item("Title") = TextBox2.Text
item("Stock") = Convert.ToInt32(TextBox3.Text)
item("Return Date") = Convert.ToDateTime(TextBox4.Text)
item("Employee") = TextBox5.Text
item.Update()

Deleting List Item

Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection = mySite.Lists(TextBox1.Text).Items
Dim itemCount As Integer = listItems.Count
Dim k As Integer
For k = 0 To itemCount - 1
    Dim item As SPListItem = listItems(k)
    If TextBox2.Text = item("Employee").ToString() Then
        listItems.Delete(k)
    End If
Next k

Deleting List Item By ID


Dim mySite As SPWeb = SPContext.Current.Web

Dim listItems As SPListItemCollection = mySite.Lists(TextBox1.Text).Items
listItems .Items.DeleteItemById(deleteItemId) ' pass id here

Updating List Item


Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"

Dim clientContext As New ClientContext(siteUrl)
Dim oList As List = clientContext.Web.Lists.GetByTitle("Announcements")
Dim oListItem As ListItem = oList.Items.GetById(3)
oListItem("Title") = "My Updated Title."
oListItem.Update()
clientContext.ExecuteQuery()


Creating List Item Inside Folder In a Sharepoint List


Dim a As SPSite = New SPSite(AppSettings("SPUrl"))

Dim websp As SPWeb = a.OpenWeb()   
Using Web As SPWeb = root_web
Dim list As SPList = Web.Lists("ListName")
Web.AllowUnsafeUpdates = True
If list IsNot Nothing Then
Dim _folderitem As SpFolder = List.RootFolder.SubFolder("FolderName")
Dim _item As SPListItem = list.Items.Add(_folderitem.Folder.ServerRelativeUrl, SPFileSystemObjectType.File)
_item("Booking_x0020_Number") = Me.lblBookingNo.Text
_item("Duration") = timeSlots
_item("Booking_x0020_Date") = Date.Now                           
_item.Update()                     
End If
End Using

Retrieving List Item From Folder In  a Sharepoint List


Dim a As SPSite = New SPSite(AppSettings("SPUrl"))

Dim websp As SPWeb = a.OpenWeb()
Using Web As SPWeb = root_web
Dim list As SPList = Web.Lists("ListName")
If list IsNot Nothing Then
Dim _folderitem As SpFolder = List.RootFolder.SubFolder("FolderName")
Dim Query As Spquery = New Spquery()
Query .Folder = _folderitem
Dim Items As SPListItemCollection = _list.GetItems(query)
For Each _ids As SPListItem In Items 
Me.lblBookingNo.Text    =  _item("Booking_x0020_Number") 
Next  
End If
End Using






No comments:

Post a Comment