Hi,
I want to upload a document from physical client location to SharePoint Document library using Microsoft.SharePoint.Client library code.
Any suggestion?
My test C# code below.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
// Starting with ClientContext, the constructor requires a URL to the server running SharePoint.
using (ClientContext client = newClientContext("http://servername146/sites/test/"))
{
//client.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Assume that the web site has a library named "FormLibrary".
var formLib = client.Web.Lists.GetByTitle("Documents");
client.Load(formLib.RootFolder);
client.ExecuteQuery();
// FormTemplate path, The path should be on the local machine/server !
string fileName = @"C:\File.txt";
var fileUrl = "";
//Craete FormTemplate and save in the library.
using (var fs = newFileStream(fileName, FileMode.Open))
{
var fi = newFileInfo("test.txt"); //file Title
fileUrl = String.Format("{0}/{1}", formLib.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, fileUrl, fs, true);
client.ExecuteQuery();
}
// Get library columns collection.
var libFields = formLib.Fields;
client.Load(libFields);
client.ExecuteQuery();
Microsoft.SharePoint.Client.File newFile = client.Web.GetFileByServerRelativeUrl(fileUrl);
Microsoft.SharePoint.Client.ListItem item = newFile.ListItemAllFields;
item["Title"] = "test";
item.Update();
client.ExecuteQuery();
}
}
catch (Exception ex)
{
}
}
}
}