Two ways to Upload file from Client Object Model

Hi Guys,

Just  a quick example, Take a scenario that you are creating an Application (in our case WinForm Application). and you want to upload a document to document library from that application, there are two ways to do, One is to upload from filecreation and the other one is to directly upload with file.savebinary method.

public void uploadfile(string siteurl,string fileName,string libraryName, string subfolderPath)
{

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

string uploadLocation = string.Empty;
if (!string.IsNullOrEmpty(
subfolderPath))
{uploadLocation = string.Format("/{0}/{1}/", libraryName, subfolderPath);}
else
{
uploadLocation = string.Format("/{0}/", libraryName);
}

#region Upload through filecreation
var fileCreationInformation = new FileCreationInformation();

byte[] bytefile = System.IO.File.ReadAllBytes(fileName);
fileCreationInformation.Content = bytefile;
fileCreationInformation.Overwrite = true;
uploadLocation = uploadLocation + Path.GetFileName(fileName);
fileCreationInformation.Url = uploadLocation;

list.RootFolder.Files.Add(fileCreationInformation);
clientContext.ExecuteQuery();
#endregion

#region Uploading directly
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{

Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, uploadLocation, fs, true);

}

#endregion

}

}

 

Please don’t forget to cast your opinion below about this blog
Cheers,
Happy SharePointing,
Advertisements

Download multiple file as Zip in SharePoint 2010

Today i have come across with a question from my friend, (Syed Ahmed Osama). He was trying to create a feature which enables to download multiple files as a single zip file.

After googling it for some time, i found a very nice feature

http://www.deviantpoint.com/post/2010/05/08/SharePoint-2010-Download-as-Zip-File-Custom-Ribbon-Action.aspx

In order to deploy it, you can use the following post
http://jethroseghers.blogspot.com/2011/04/download-multiple-files-from-sharepoint.html.

(For the step 4 mention in the above blog, use this ps command

Install-SPSolution -Identity 2010_5_DeviantPoint.DownloadZip.wsp -GACDeployment -force

(stsdam won’t be available for the next version, so get yourself ready guys 🙂 )

It works for me like a charm, All marks to Bart X. Tubalinal. Good job mate
Cheers,

Happy SharePointing