Jump to content
b33bs

Acesso a documentos do Sharepoint 2010 via CSharp

Recommended Posts

b33bs

Bom dia povo, sou novo aqui no Forum e também no que diz respeito a Sharepoint 2010.

Ora, tenho um sharepoint 2010 instalado num servidor ao qual tenho acesso, mas queria, via csharp tentar aceder e sacar (tipo, getBytes() ou algo assim do genero) um documento duma determinada libraria, mas para ja nao tou a ter sucesso.

Ja consegui criar programas que me permitam entrar no Sharepoint, consigo ter acesso a todas as listas e libraries, mas nao consigo aceder aos documentos.

Acho que me esta a faltar algo, mas pra ja nao consigo encontrar nada na net que me ajude.

alguma sugestao??

Obrigado,

Abraco

Share this post


Link to post
Share on other sites
b33bs

Boas a todos...

ja consegui ter acesso a documentos e fazer download para o meu PC...

Obrigado..

abraço

Share this post


Link to post
Share on other sites
b33bs
Ca vai a solucao:

Criei uma console app


using System;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;


namespace ConsoleApplication1
{

    class Program
    {
        static private void CopyStream(Stream source, Stream destination)
        {
            byte[] buffer = new byte[100000];
            int bytesRead;
            do
            {
                bytesRead = source.Read(buffer, 0, buffer.Length);
                destination.Write(buffer, 0, bytesRead);
            } while (bytesRead != 0);
        }

        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("http://ServidorSharepoint");
            
            List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Shared Documents");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
            @"<View>
                <Query>
                  <Where>
                        <Contains>
                            <FieldRef Name='FileLeafRef'/>
                            <Value Type='Text'>.pdf</Value>
                        </Contains>              
                  </Where>
                  <RowLimit>1</RowLimit>
                </Query>
              </View>";
            ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
            clientContext.Load(sharedDocumentsList);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            if (listItems.Count > 0)
            {
                for (int i = 0; i < listItems.Count; i++)
                {
                    ClientOM.ListItem item = listItems[i];
                    Console.WriteLine("\n nome do documento: {0}", item["FileLeafRef"]);
                    FileInformation fileInformation =
                    ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);

                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        CopyStream(fileInformation.Stream, memoryStream);
                        long len;
                        len = memoryStream.Length;
                        byte[] buffer = new byte[len];
                        buffer = memoryStream.GetBuffer();
                        try
                        {
                            FileStream fs = new FileStream(@"c:\" + (string)item["FileLeafRef"], FileMode.Create, FileAccess.ReadWrite);
                            BinaryWriter bw = new BinaryWriter(fs);
                            bw.Write(buffer);
                            bw.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            else
                Console.WriteLine("Document nao encontrado.");
    
            Console.ReadLine();

        }
    }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.