Header

Thursday, 13 September 2012

How to create list Programmatically/VB.NET

This time i have created one list using VB.Net code
For this one i have created on function and passing one Foobar list that containg information that i need to upload in list .
In this function i m checking that if list is there then delete it and create new one .

Public Function GetSplist(ByVal ds As Generic.List(Of Foobar)) As SPList
        Dim web As SPWeb = SPContext.Current.Web
        web.AllowUnsafeUpdates = True
        'creating one object of splist with nothing value bcz this has no constractur
        Dim newList As SPList = Nothing
        Try
            If Not IsNothing(web.Lists("TempNewList")) Then
                Dim mylist As SPList = web.Lists("TempNewList")
                mylist.Delete()
                web.Update()
            End If
        Catch ex As Exception
        End Try
        'Here i am creating one list guid
        Dim newListGuid As Guid = web.Lists.Add("TempNewList", "This is my temp list, it will be removed when used", SPListTemplateType.GenericList)
        newList = web.Lists(newListGuid)
        If Not IsNothing(newList) Then
            newList.Fields.Add("SNo", SPFieldType.Text, False)
            newList.Fields.Add("FileRefNo", SPFieldType.Text, False)
            newList.Fields.Add("NavUrl", SPFieldType.Text, False)
         End If
        newList.Update()
        Dim newItem As SPListItem = Nothing
        For Each _item As Foobar In ds
            newItem = newList.Items.Add()
            newItem("SNo") = _item.SNo
            newItem("FileRefNo") = _item.FileRefNo
            newItem("NavUrl") = _item.NavUrl
            newItem.Update()
        Next
        web.AllowUnsafeUpdates = False
        Dim _mylist As SPList = newList
        Return _mylist
    End Function


Thanks And Regards,
Shailendra Kumar Singh

No comments:

Post a Comment