0
votes

I am using sharepoint 2010 client object model to create folders and uploading files.

I am able to create the folders but when I try to upload a file I am getting "401 unauthorized error". below is the code snippet.

          Try
            Dim lstrFileName As String = Path.GetFileName(lstrSourceFilePath)
            Dim lobjFileStream As New FileStream(lstrSourceFilePath, FileMode.Open)



                Dim lstrFileRelativeURL As String = "/" & pstrFolderURL & "/" & lstrFileName

                 MSSPClient.File.SaveBinaryDirect(lobjClientContext,
                 lstrFileRelativeURL, lobjFileStream, True)

So I tried an alternative method for uploading the file. The code snippet is:

                    Dim lobjSourceFileCreateInfo As New FileCreationInformation()
                    lobjSourceFileCreateInfo.Content = System.IO.File.ReadAllBytes(lstrSourceFilePath)
                    lobjSourceFileCreateInfo.Overwrite = True


                    lobjSourceFileCreateInfo.Url = lstrFileRelativeURL


                    Dim lobjWebObj As Web = lobjClientContext.Web


                    Dim llstListOfDoc As List = lobjWebObj.Lists.GetByTitle(pstrListName)
                    Dim lobjFiletoBeUploaded As MSSPClient.File = llstListOfDoc.RootFolder.Files.Add(lobjSourceFileCreateInfo)

                    lobjClientContext.Load(lobjFiletoBeUploaded)
                    lobjClientContext.ExecuteQuery()

This time I am getting the error like: "Value doesn't fall within the expected range"

I am really confused with this. I am using admin accounts for uploading the files and accessing the server. Interesting thing is I am able to upload the file using the font end.

Any help is greatly appreciated.

1
I have resolved the above problem by using complete URL given as value to lobjSourceFileCreateInfo.Url instead of relative URL.Rupesh

1 Answers

0
votes

You should add your credential in the first method before the MSSPClient.executeQuery();

MSSPClient.Credentials = new NetworkCredential("LoginID", "LoginPW", "LoginDomain");

MSSPClient.File.SaveBinaryDirect(lobjClientContext,lstrFileRelativeURL, lobjFileStream, True)

MSSPClient.executeQuery();