1

While loading ClientContext in Provider Hosted Application for SharePoint Online 2013 to read file in InfoPath's XML and generate another XML file to be uploaded in another Document Library.

Tried Multiple Solution listed below.

  1. FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef.ToString());

gives error

at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.File.OpenBinaryDirect(ClientContext context, String serverRelativeUrl) at SharePointAppWithAzureWeb.Services.RemoteEventReceiver1.ProcessOneWayEvent(SPRemoteEventProperties properties)

  1. Tried this code

                var fileRef = listItem.FieldValues["FileRef"];
                var srcWeb = context.Web;
                Microsoft.SharePoint.Client.File file =  srcWeb.GetFileByServerRelativeUrl(fileRef.ToString());
                string nLocation = context.Web.ServerRelativeUrl.TrimEnd('/') + "/" + "testdert".Replace(" ", "") + "/" + file.Name;
    
                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context,nLocation , fileInfo.Stream, true);
    

giving error

The property or field 'ServerRelativeUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

  1. Third solution I tried

    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(@"http/spe196:2222/" + fileRef);
    

giving error

Invalid URI: The format of the URI could not be determined.

Can anyone please suggest me how to do this.

2

Hey for your tried code 2,

Just replace this

FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);

With this

var clientResultStream = file.OpenBinaryStream();
clientContext.ExecuteQuery();
var stream = clientResultStream.Value;

And then try to save the data.

Update : Code to create file in document library.

You need to convert your data to byte array. And then use this code

 using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = creds;
//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle("TestLibrary1");

var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream

fileCreationInformation.Content = byteArray;
//Allow owerwrite of document

fileCreationInformation.Overwrite = true;
//Upload URL

fileCreationInformation.Url = siteURL + "TestLibrary1/Test/" + fileName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);

//Update the metadata for a field having name "DocType"
uploadFile.ListItemAllFields["tests"] = "Favourites";

uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}

Inform me if you need some more help.

  • hey @aakash thanks it worked for me now it is not giving any error but the error is changed from 401 to 403 on SaveBinaryDirect() Method .can you provide more detail on how to save it to another document library? – Codec Aug 3 '16 at 8:59
  • Updating my answer. Please wait – Aakash Maurya Aug 3 '16 at 9:08
  • 1
    Thanks @Aakash it solved my problem which i am trying from last 8 days .Thanks a lot – Codec Aug 3 '16 at 9:32
  • Glad to help :-) – Aakash Maurya Aug 3 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.