0

I want to give option to user on .aspx page to upload and download file to SharePoint Document library.

I created an ASPX form page. There is an option to upload / download the document for storage.

My requirement is,

  • I need both upload and download button in the same page.

  • While clicking the upload from the .aspx page the document should store in SharePoint 2010 document library.

  • While clicking download from .aspx page it should download from the document library, using the document library for Document storage.

How to achieve this?

1 Answer 1

0

If your form is hosted inn sharepoint, You can use server object model for this

If it is hosted outside, you can use client object model:

public void UploadDocument(string siteURL, string documentListName,
string documentListURL, string documentName,

byte[] documentStream)
{

using (ClientContext clientContext = new ClientContext(siteURL))
{        

//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);

var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream

fileCreationInformation.Content = documentStream;
//Allow owerwrite of document

fileCreationInformation.Overwrite = true;
//Upload URL

fileCreationInformation.Url = siteURL + documentListURL + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
    fileCreationInformation);

//Update the metadata for a field having name "DocType"
uploadFile.ListItemAllFields["DocType"] = "Favourites";

uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();

}
}

public Stream DownloadDocument(string siteURL, string documentName)
{

ListItem item = GetDocumentFromSP(documentName);
if (item != null)

{
using (ClientContext clientContext = new ClientContext(siteURL))

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

return fInfo.Stream;
}

}
return null;

}


private static ListItem GetDocumentFromSP(string documentName)
{

//This method is discussed above i.e. Get List Item Collection from SharePoint
//Document List
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", 
    documentName, "Text", 1);


return (listItems != null && listItems.Count == 1) ? listItems[0] : null;

}
2
  • hi Aanchal, its hosted outside...
    – sairam
    Commented Sep 29, 2014 at 7:03
  • So, you can use above code and use CSOM.
    – Aanchal
    Commented Sep 29, 2014 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.