I need to move a file from one site collection to another with CSOM. There is a working sample here.
When running the code I get an 401. It must be related to using modern authentication. Anyone knows how to do the same with modern auth?
var sourceSite = "https://xxx.sharepoint.com/sites/afd-286-oprydning-test-source";
var targetSIte = "https://xxx.sharepoint.com/sites/afd-286-oprydning-test-target";
var ctxSource = ClientContextExtension.GetAppContext(sourceSite, _config.AppRegistrationId, _config.AppRegistrationKey, "SaveToExcel", new EmptyLogger());
var ctxTarget = ClientContextExtension.GetAppContext(targetSIte, _config.AppRegistrationId, _config.AppRegistrationKey, "SaveToExcel", new EmptyLogger());
Web destWeb = ctxTarget.Web;
ctxTarget.Load(destWeb);
ctxTarget.ExecuteQuery();
Microsoft.SharePoint.Client.List list = ctxSource.Web.GetDocumentsList();
var item = list.GetItemById(1);
ctxSource.Load(item);
ctxSource.ExecuteQuery();
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
var file = item.File;
ctxSource.Load(file);
ctxSource.ExecuteQuery();
var destLibrary = "Delte dokumenter";
string location = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary + "/" + file.Name;
var stream = file.OpenBinaryStream();
ctxSource.ExecuteQuery();
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctxTarget, location, stream.Value, true);
}