2
TeamSiteDataContext context = new TeamSiteDataContext(new Uri("http://server/_vti_bin/ListData.svc"));
context.Credentials = new NetworkCredential("username", "password");
var f = context.SharedDocuments.Where(a => a.Name == "document1.docx").SingleOrDefault();

How can I get the content of the file f as a byte array? I am using Webservices Rest Interface for sharepoint 2010.

3

I assume that you don't have problems to get the SPFile. Here is a little example:

SPFile xmlFile = ...


using (var sr = new StreamReader(xmlFile.OpenBinaryStream()))
{
    String xmlString = sr.ReadToEnd();
    ...
}

OpenBinaryStream gives you back a stream. You will get the naked bytes back ;)

  • I am developing a client-side application which accesses data from a remote sharepoint server. From what I googled I found that SPFile class is in Microsoft.Sharepoint.dll which can be used only if the application is on the same server. Am I understanding it correctly? Or do I need to dig in deeper? Other than the rest interface there is also a soap interface which uses the Lists.asmx web service. But that looked a bit complicated to me. Is there a way to get the content using ListData.svc because it is easier to use? – AbhinavRanjan Apr 4 '13 at 8:34
1

Finally found an easy way to do this. Looks like there are different web services to achieve different functionality. I finally added a web reference to

http://server/_vti_bin/copy.asmx

where server should be replaced by sharepoint server name

Suppose, I give it a name CopyReference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;



namespace Asmx
{
    class Program
    {
        static void Main(string[] args)
        {

            CopyReference.Copy copyService = new CopyReference.Copy();
            copyService.Url = @"http://server/_vti_bin/copy.asmx"; // change    server to your sharepoint server
            copyService.Credentials = new NetworkCredential("username", "password");


            string copySource = @"http://server/Shared Documents/document1.docx"; //location of the file on server
            Console.WriteLine("Here");

            CopyReference.FieldInformation myFieldInfo = new  CopyReference.FieldInformation();
            CopyReference.FieldInformation[] myFieldInfoArray = { myFieldInfo };
            byte[] myByteArray;
            Console.WriteLine("Here");

            copyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);
            Console.WriteLine(myByteArray.ToString());
            Console.ReadKey();


        }
     }
}

myByteArray will give you the bytes in the document.

  • Great that the webservice approach is working for you :) Be careful where you store your username and password and don't send it through an unsecure connection. – hupseb Apr 6 '13 at 14:31

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.