How to store & Retrieve files from SharePoint Document Library


In this article I'm trying to explain how to store the files in SharePoint Document Library and how to fetch from SharePoint Library and display in our Application. This article will be usefull for those who are working in SharePoint Developing.

How to store & Retrieve files from SharePoint Document Library:


In this article I'm trying to explain how to store the files in SharePoint Document Library and how to fetch from SharePoint Library and display in our Application.

Index:



Here, I'm trying to clear few points those are
1) How to store files or Documents in SharePoint site
2) How to fetch that file or document from SharePoint site and display that in our Application
3) What is the difference between to store the files in SharePoint location and Database. Performance wise which one is better.

How to store files or Documents in SharePoint Document Library:



To store files in SharePoint Document Library, we must create Document Library first. After creating Document Library if required create columns to that. Refer below link how to create Document Library, columns for Document Library in SharePoint site.

Document Library



After creating Document Library then use that Document Library while store files in SharePoint.

Just create one dal file like below.


public class SPDocumentLibrary
{
//for inserting purpose
public Guid AddLibraryItemAttachment(string SiteURL, string WebURL, string ListName, byte[] Attachment, string AttachmentName, Hashtable dtList);

//for fetching records purpose
public DataTable ListContent(string SiteURL, string WebURL, string ListName, string Query);
}



And use this reference call this dal in our application for inserting and retrieving files from SharePoint location.


protected void btnUpload_OnClick(object sender, EventArgs e)
{
SPDocumentLibrary objFile = new SPDocumentLibrary();
Hashtable taskHash = new Hashtable();
// here Design_Issue_Id as a Document Library column name
taskHash["Design_Issue_Id"] = Design_Issue_Id;
string FileName = fup.FileName;
string FileType = FileName.Substring(FileName.IndexOf('.') + 1);
byte[] File = fup.FileBytes;
if (fup.HasFile)
{
objFile.AddLibraryItemAttachment("SiteURL", "webURL", "DocumentLibraryName", File, FileName + "." + FileType, taskHash);
}
}




Using above sample code we can able to store the files in SharePoint Document Library.

Inserting File

How to fetch files or documents from SharePoint Document Library:



Upto now we are performing to store the files or documents in SharePoint Document Library. Now, we can fetch that files and show that into our applications based on conditions.

Ex:

In this example I want to show that saved files in Gridview Control.
OnRowDataBound event of Gridview I just fetch that files and show that in controls.


protected void GvDesignLog_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SPDocumentLibrary objFile = new SPDocumentLibrary();
string Design_Id = ((Label)e.Row.FindControl("lblDesignId")).Text;
string Query = "<Where>" +
"<Eq><FieldRef Name='Design_Issue_Id'/><Value Type='Text'>" + Design_Id + "</Value></Eq>" +
"";
DataTable dtCC = objFile.ListContent("SiteURL", "webURL", "DocumentLibraryName", Query);

ImageButton imgbtn = ((ImageButton)e.Row.FindControl("imgbtn"));

if (dtCC.Rows.Count > 0)
{
imgbtn.PostBackUrl = dtCC.Rows[0]["EncodedAbsUrl"].ToString();
imgbtn.ImageUrl = "/_layouts/images/dms/pdf.png";
}
else
{
imgbtn.Visible = false;
}
}
}




Fetching file

What's the difference between store files in SharePoint Library and Database:



SharePoint Document Library:

1) It is easy to store and retrieve the files from SharePoint Document Library.

2) Performance wise this is best way to use Document Library.

3) We can store any type of file in SharePoint Document Library.

4) If the file name is same then old file is replaced with new file. But we can able to manage the same file using version History. This is great approach to use SharePoint Document Library.

Database:

1) It is very difficult to store and retrieve the files from Database.

2) Performance also decreases, while fetching files or documents from Database.

3) If the file name and file type is same then old file is replaced with new file. But we can't able to take the older file again. This is the one of the drawback to stores files in database.


Concusion:



This article will help you to store files in SharePoint Document Library and fetching files from Document Library.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

Author: Phagu Mahato03 Dec 2013 Member Level: Gold   Points : 8

Thank for posting useful artilce on How to store & Retrieve files from SharePoint Document Library. We can also use below example codes of sharepoint document


public static void CopyStream(Stream input, Stream output) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, len);
}
}

protected void Page_Load(object sender, EventArgs e)
{
string url = "https://myserver.com/test/Shared%20Documents/Yourpic.jpg";
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
Stream fs = response.GetResponseStream() as Stream;

using (FileStream localfs = File.OpenWrite(@"c:\temp\Youraspdownloadedfile.jpg"))
{
CopyStream(fs, localfs);
}
}

Code 2

public string GetFileContents(string websiteUrl)
{
string fileContentString = string.Empty;
SPSite site = new SPSite(websiteUrl);
if (site != null)
{
SPWeb web = site.OpenWeb();
if (web != null)
{
fileContentString = web.GetFileAsString(websiteUrl);
SPFile file = web.GetFile(websiteUrl);

}
else
{
Console.WriteLine("Could not open website {0}", websiteUrl);
}
site.Dispose();
}
}
return fileContentString;



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: