Click here to Skip to main content
14,603,017 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello folks, at my wits end on this one.

Receiving "Access denied. You do not have permission to perform this action or access this resource." error but only when attempting to acquire FileVersionCollection. Works fine if I omit the oclientContext.Load(fileversions).

Why would credentials work for the file, but not when loading the FileVersionCollection?

What I have tried:

Using oclientContext As ClientContext = New ClientContext("<sharepointsite>")
            Dim f As Microsoft.SharePoint.Client.File
            Dim fileversions As FileVersionCollection

                f = oclientContext.Web.GetFileByServerRelativeUrl("/" + e.CommandArgument.ToString())
                fileversions = f.Versions
                oclientContext.Load(fileversions) //Works fine if I omit this

            Catch sp_ex As Microsoft.SharePoint.Client.ServerException
                Context.Response.ContentType = "text/plain"
                If sp_ex.ServerErrorTypeName = "System.IO.FileNotFoundException" Then
                    f = Nothing
                    Context.Response.Write("File Not Found")
                End If
                Exit Sub

            Catch ex As Exception
                f = Nothing
                Context.Response.ContentType = "text/plain"
                Exit Sub

            End Try

        End Using
Updated 16-Jun-17 8:41am
Member 13250972 12-Jun-17 8:58am
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's perfectly possible to have permission to read, modify and delete a file, but not have permission to view the versions. For example, "Restricted Read" gives you permission to view and open items, but not to view the versions.

Understanding permission levels in SharePoint - Office Support[^]

You'll need to check with your SharePoint administrator to see what permissions are granted to the account your code is executing as.
Member 13250972 13-Jun-17 15:05pm
Admin set permissions to "Read"; which was previously set to "Restricted Read"...but no joy.

Admin said it could take a while to change, but I've waited about two hours.
Rate this:
Please Sign up or sign in to vote.

Solution 2

As with most problems that take forever to figure out, the solution is simple. (Though I found plenty of very complex ways of addressing the same thing; but didn't work)

The problem was subsites.
Need to include the subsite in the ClientContext.

In my case, I needed to determine the subsite dynamicaly from the fileurl which is stored in the e.CommandArgument of a control.

Dim subsite As String = Regex.Match(e.CommandArgument.ToString(), ".*?(?=\/)", RegexOptions.IgnoreCase).ToString()
        Using oclientContext As ClientContext = New ClientContext("https://<sharepointsite>/" + subsite)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100