I'm developing a C# based application that requires to download, checkout, upload, check in on a specific file from/to Sharepoint with CSOM. So I have two questions here:

Firstly, on download, is there others way to download a specific named file under folder "Document" instead of searching through GetItemByID(). Please refer to code below:

string siteUrl = @"http://test.com/sites/company/";

ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new NetworkCredential("username", "password" , "domain");            
ctx.AuthenticationMode = ClientAuthenticationMode.Default;

var list = ctx.Web.Lists.GetByTitle("Document");
var listItem = list.GetItemById();

ctx.Load(listItem, i => i.File);

var fileRef = listItem.File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileRef);
var fileName = Path.Combine("C:\\", (string)listItem.File.Name);

using (var fileStream = System.IO.File.Create(fileName))

Secondly, in regards to the workflow (download, modify, check out, upload, check in), is this feasible of doing?

Thanks in advance.


You could use caml query to filter based on file name.

var list = clientContext.Web.Lists.GetByTitle("DocSet");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                                           <Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>testdata.xlsx</Value></Eq></Where>
                ListItemCollection listItems = list.GetItems(camlQuery);

Download file in workflow would be touch based on my knowledge, while you could create custom service host in SharePoint so you could call the service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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