CPaul
CPaul

Reputation: 1

CAML query not retrieving all fields using vb.net VS2019 and Sharepoint online

I was asked to write an app at work that grabs an Employee list from our Sharepoint online site for eventual auto updating. The part I am stuck on is retrieving all of the fields of my employee list.

I would think that my query would simply bring back all of the fields that I see on my sharepoint list. But I admit that at this point that I do not have the best understanding of CAML.

So, I start to loop through each item in my Allitems object to create a datarow for each row of info so I can have a nice little data-table to display to the user. When the line of code runs to find item.("First Name") , I get the message that it is not in the list and that I may need to explicitly query it. Same with all of the other fields with a space. My AllItems object is bringing back all columns with the names that do not have a space (ie. Department, Title, Location, Email) which is strange to me.

Apparently, I do not understand Sharepoint enough and what the fields are called with a space.

I also see some C# examples with another method for the query object : query.ViewFields . I do not have this option for my query object. Also, can someone recommend another Sharepoint imports that would be beneficial here besides the ones that I am using?

Any help and advice would be greatly appreciated. Even an entirely different way of getting this data would be appreciated.

Also, I left only 4 of the datarow fields in the below code since it is failing on "First Name" to keep the code uncluttered. Since that would be the same reason for all of the other fields that are not comming back in my query.

Chris

Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Client

      Try


            Dim cred = New SharePointOnlineCredentials(txtUserName.Text, secureString)
            Dim ctx = New Microsoft.SharePoint.Client.ClientContext(siteUrl)
            ctx.Credentials = cred
            Dim web As Web = ctx.Web
            Dim list As List = web.Lists.GetByTitle(siteList)
            ctx.Load(list)
            ctx.ExecuteQuery()

            Dim query As CamlQuery = CamlQuery.CreateAllItemsQuery()

            query.ViewXml = "<View Scope='RecursiveAll'><Query><ViewFields><FieldRef Name='First Name'/><FieldRef Name='Last Name'/><FieldRef Name='Office Phone'/><FieldRef Name='Email'/><FieldRef Name='Cell Phone'/><FieldRef Name='Department'/><FieldRef Name='Location'/></ViewFields></Query></View>"


            Dim AllItems As ListItemCollection = list.GetItems(query)
            ctx.Load(AllItems)
            ctx.ExecuteQuery()

            If AllItems.Count > 0 Then
                Dim dt As New DataTable
                Dim dRow As DataRow

                Dim dcID As New DataColumn("Id")
                dcID.DataType = Type.GetType("System.String")

                Dim dcFName As New DataColumn("First Name")
                dcFName.DataType = Type.GetType("System.String")

                Dim dcLName As New DataColumn("Last Name")
                dcLName.DataType = Type.GetType("System.String")

                Dim dcTitle As New DataColumn("Title")
                dcTitle.DataType = Type.GetType("System.String")


                dt.Columns.Add(dcID)
                dt.Columns.Add(dcFName)
                dt.Columns.Add(dcLName)
                dt.Columns.Add(dcTitle)

                For Each item As ListItem In AllItems

                    dRow = dt.NewRow()

                    dRow("Id") = item.Id
                    dRow("First Name") = item("First Name")
                    dRow("Last Name") = item("Last Name")
                    dRow("Title") = item("Title")

                    dt.Rows.Add(dRow)

                Next

                DGVList.DataSource = dt

            End If





        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

Upvotes: 0

Views: 998

Answers (1)

CPaul
CPaul

Reputation: 1

Ok, it seems that the CAML was correct. I just did not know the internal names on the Sharepoint site. Once I obtained a list of the internal field names (not the display ones like I naively thought), I no longer ran into any errors. The answer was not so hard once I realized that I was querying fields that did not exist. It makes perfect sense that Visual Studio would yell at me for it.

Upvotes: 0

Related Questions