|
|
|
using System; |
|
using System.Web.UI; |
|
using Microsoft.SharePoint.Client; |
|
using File = System.IO.File; |
|
using Microsoft.SharePoint; |
|
using System.Security; |
|
using System.IO; |
|
using System.Net; |
|
using Microsoft.Extensions.DependencyModel; |
|
using SP = Microsoft.SharePoint.Client; |
|
using System.Linq; |
|
using System.Web; |
|
|
|
|
|
|
|
public partial class test : Page |
|
{ |
|
public void UploadDocument(string siteURL, string serverRelativeDestinationFilenamePath, string fullLocalFilenamePathToImport) |
|
{ |
|
using (ClientContext context = new ClientContext(siteURL)) |
|
{ |
|
// Credentials hardcoded here for brevity, in reality you |
|
// want to store them in a secure location or request user to input them |
|
string password = "Ox0ypVcVgXXXX"; |
|
string account = "admin@xxx.com"; |
|
var secret = new SecureString(); |
|
foreach (char c in password) |
|
{ |
|
secret.AppendChar(c); |
|
} |
|
context.Credentials = new SP.SharePointOnlineCredentials(account, secret); |
|
|
|
using (FileStream fs = new FileStream(fullLocalFilenamePathToImport, FileMode.OpenOrCreate)) |
|
{ |
|
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, serverRelativeDestinationFilenamePath, fs, true); |
|
} |
|
var newFile = context.Web.GetFileByServerRelativeUrl(serverRelativeDestinationFilenamePath); |
|
|
|
context.Load(newFile); |
|
context.ExecuteQuery(); |
|
|
|
//check out to make sure not to create multiple versions |
|
newFile.CheckOut(); |
|
|
|
var item = newFile.ListItemAllFields; |
|
|
|
// update some metadata if you need to, for example: |
|
// item["Title"] = "My Uploaded File"; |
|
// item["CustomFieldName"] = "Some Custom Value"; |
|
// item.Update(); |
|
// context.ExecuteQuery(); |
|
|
|
// use OverwriteCheckIn type to make sure not to create multiple versions |
|
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn); |
|
} |
|
} |
|
protected void Page_Load(object sender, EventArgs e) |
|
|
|
{ |
|
|
|
UploadDocument("https://xxxx.sharepoint.com", "/test/document.pdf", @"h:\document.pdf"); |
|
} |
|
} |