先创建一个项目,然后写一个转移方法
public static void movefiles()
{
try{
ClientContent clientContext = new ClientContext(siteUrl);
clientContext.RequestTimeout = Timeout.Infinite;
var user name = "";
var password ="";
clientContext.Credentials = new NetworkCredential(username,password,domain);
clientContext.Credentials = CredentialCache.DefaultCredentials;
Guid listId = new Guid("A9999---");
Web web = clientContext.Web;
List docList = web.Lists.GetById(listId);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "";
ListItemCollection items = docList.GetItems(camlQuery);
clientContext.Load(docList);
clientContext.Load(items);
clientContex.ExecuteQuery();
int count = 1;
foreach(var item in items)
{
Console.WriteLine("共"+items.Count + "个");
SP. File file = item.Fiel;
clientContext.Load(file);
clientContex.Load(web);
clientContex.ExecuteQuery();
string destLocation = "/site/library/folder/"+file.name;
//FileInfomation fileInfo = SP.File.OpenBinaryDirect(clientContex,file.ServierRelativeUrl);
//SP.File.SaveBinaryDirect(clientContex,destLocation,fileInfo.Stream,true);
if(item.FileSystemObjectType == FileSytemObjectType.File)
{
file.MoveTo(destLocation,MoveOperations.Overwrite);
clientContex.ExecuteQuery();
var uploadfile = clientContext.Web.GetFileByServerRelatioveUrl(destLocation);
var listitem = uploadfile.ListItemAllFields;
listitem["ContentTypeId"] = "0x0....";
listitem.Update();
clientContext.ExecuteQuery();
count ++;
clientContext.Dispose();
Console.WriteLine("完成。")
}catch(Exception e)
{
Console.WriteLine("{0}",e.Message);
return;
}
}
下载文档的方法:
FileInformation fInfo = SP.File.OpenBinaryDirect(clientContext,item["FileRef").ToString();
stream s= fInfo.Stream;
string saveTo=@"D:\\folder"+filename;
FileStream writeStream = new FileStream(saveTo,FileMode.Create,FileAccess.Write);
ReadWriteStream(s,writeStream);