Download Document from Sharepoint Library C#

This is the code from which we can allow the user to download the document without giving him any access rights.

This is the condition when want to give one functionality for downloading in asp.net site.

Function 1. In first function we need to pass filename, document Id and serverpath.

server path is a temperory path we need to copy file on server until user download it.

public string DownloadDocument(string FileName, int documentId, string serverpath)

{

ContractDataAccess cda = new ContractDataAccess();

Stream file;

try

{

{

file = cda.GetDocumentById(documentId);

FileStream fsDocFile;

StreamReader srWebSrc = new StreamReader(file);

string filepath = serverpath + FileName;

if(System.IO.File.Exists(filepath))

fsDocFile = System.IO.File.Open(filepath, FileMode.Truncate);

else

fsDocFile = System.IO.File.Open(filepath, FileMode.Create);

file.CopyTo(fsDocFile);

srWebSrc.Close();

fsDocFile.Close();

return filepath;

}

}

catch (Exception ex)

{

throw ex;

}

}

we need to pass sharepoint ID of the document to the function.

public Stream GetDocumentById(int documentId)

{

//The four values below are retrieved from config file. They gives the values for authenticated user, sharepoint website path, password, and server name. This can be used on same server only.

string user = ConfigurationManager.AppSettings["User"].ToString();

string webUrl = ConfigurationManager.AppSettings["WebUrl"].ToString();

string pass = ConfigurationManager.AppSettings["Password"].ToString();

string server = ConfigurationManager.AppSettings["Server"].ToString();

NetworkCredential credentials = new NetworkCredential(user, pass, server);

ListItem item = GetdocumentFromId(documentId);

if (item != null)

{

using (ClientContext clientContext = new ClientContext(webUrl))

{

clientContext.Credentials = credentials;

FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());

return fInfo.Stream;

}

}

return null;

}

public ListItem GetdocumentFromId(Int32 documentId)

{

Contract contract = null;

List resultlist = null;

ListItem contractItem = null;

try

{

//The four values below are retrieved from config file. They gives the values for authenticated user, sharepoint website path, password, and server name. This can be used on same server only.

string webUrl = ConfigurationManager.AppSettings["WebUrl"].ToString();

string user = ConfigurationManager.AppSettings["User"].ToString();

string pass = ConfigurationManager.AppSettings["Password"].ToString();

string server = ConfigurationManager.AppSettings["Server"].ToString();

NetworkCredential credentials = new NetworkCredential(user, pass, server);

using (var clientContext = new ClientContext(webUrl))

{

clientContext.Credentials = credentials;

resultlist = clientContext.Web.Lists.GetByTitle("<ListName>");

contractItem = resultlist.GetItemById(documentId);

clientContext.Load(resultlist);

clientContext.Load(contractItem);

clientContext.ExecuteQuery();

}

contract = new Contract();

}

catch (Exception exception)

{

Console.WriteLine(exception.Message);

}

return contractItem;

}


Submitted By Nipesh Shah @ 9/26/2011 7:51:10 PM

Search This Blog

Link Within Related Posts Plugin for WordPress, Blogger...