Load Document from Document Library using the Client Object Model
-
Sunday, August 26, 2012 7:42 PM
I`m able to receive all document items from a document library using the c# client object model, but when i try to download it using the OpenBinaryStream() function each of the items return a null value for the stream. Can somebody help me?
regards Joerg
All Replies
-
Monday, August 27, 2012 8:41 AMIs it possible that the API is broken?
-
Monday, August 27, 2012 3:05 PM
You can use following code for download documents
using (ClientContext ctx = new ClientContext("Site URL"))
{
List _list = ctx.Web.Lists.GetByTitle("Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View/>";
ListItemCollection Items = _list.GetItems(camlQuery);
ctx.Load(Items);
ctx.ExecuteQuery();
foreach (ListItem Item in Items)
{
if (Item != null)
{
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx,Item["FileRef"].ToString());
//Path where you want to save the file
string strPath = "C:\\Copy";
FileStream writeStream = new FileStream(strPath, FileMode.Create, FileAccess.Write);
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = fInfo.Stream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = fInfo.Stream.Read(buffer, 0, Length);
}
fInfo.Stream.Close();
writeStream.Close();}
}
}Navneet Singh
- Proposed As Answer by Navneet..Singh Monday, August 27, 2012 3:06 PM
- Marked As Answer by Humberto LezamaMicrosoft Employee, Moderator Saturday, September 01, 2012 12:45 AM
-
Monday, December 03, 2012 2:51 AM
Hi, i'm able to get the document reference
but i'm stuck at this line:
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx,Item["FileRef"].ToString());
"The remote server returned an error: (403) Forbidden
any idea how should I overcome this?
- Proposed As Answer by alexms_2001 Thursday, January 17, 2013 3:17 PM
-
Wednesday, December 05, 2012 10:25 PM
Hi Winston,
If this is a secure server you're trying to access, do you have the appropriate credentials to the server? If not, that can account for that error.
Regards,
- Kemp Brown [MSFT]
-
Thursday, January 17, 2013 3:20 PM
> i'm stuck at this line:
> FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx,Item["FileRef"].ToString());
... because it is wrong. Correct version:using SPC = Microsoft.SharePoint.Client;. . .
string relPath = Item.FieldValues["FileRef"].ToString();
using (FileInformation fInfo = SPC.File.OpenBinaryDirect(ctx, relPath))etc.
Alexms_2001@yahoo.com

