Question

I'm trying to build a for-loop in Visual Basic that will go over each item in a sharepoint database list and stop it from inheriting permissions from its parent. I'm getting a Null Exception because my query object is null. I've searched and all the code snippets i found didnt define a value for the query and wrote the code exactly like i did, why is this a problem.

Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Module Module1
    Sub Main()
        Dim siteUrl As String = "https://cws1.conti.de/content/00009504"
        Dim clientContext As New ClientContext(siteUrl)
        Dim oList As List = clientContext.Web.Lists.GetByTitle("TestBI")
        Dim query As CamlQuery
        query.ViewXml = "<Where><Eq><FieldRef Name='Account' /><Value Type='Text'></Value></Eq></Where>"
        Dim oListItems As ListItemCollection = oList.GetItems(query)
        For numItems As Integer = 0 To 2
            oListItems(numItems).BreakRoleInheritance(True, False)
        Next

        clientContext.ExecuteQuery()
    End Sub
End Module

I've never programmed VB before and i'm not familiar with automating anything on Sharepoint so I would really appreciate any help.

Was it helpful?

Solution

you need to assign an instance of an object to the query variable before you can use it. Right now, you only declare a variable. Change the line:

Dim query AS CamlQuery

to

Dim query AS new CamlQuery

Also, CamlQuery expects a full <View> tag. So change your CAML to <View><Query><Where><Eq><FieldRef Name='Account' /><Value Type='Text'></Value></Eq></Where></Query></View> and you should be good to go.

If you wish to retrieve all items from the list, use the CamlQuery.CreateAllItemsQuery() method.

Dim query AS CamlQuery = CamlQuery.CreateAllItemsQuery()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top