#region Using Directives
using System; using System.IO; using System.Linq; using SP = Microsoft.SharePoint.Client;
using System.Net;
#endregion
namespace SharePointClient { class Program { static void Main(string[] args) { string srcUrl = "https://aaa/bbb/ccc/"; string destUrl = "https://aaa/bbb/ddd/";; System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("aaa", "ccc", "vvvv");
string srcLibrary = "MySrc Library";
string destLibrary =#region "MyDestUsing Library";Directives
//copy latest full version ofusing fileSystem;
CopyDocuments(srcUrl, destUrl, srcLibrary, destLibrary, credentials); using System.IO;
using System.Linq;
using SP = }Microsoft.SharePoint.Client;
#region Supportingusing MethodsSystem.Net;
#endregion
public static void CopyDocuments(string srcUrl, string destUrl,
namespace stringSharePointClient
{
srcLibrary, string destLibrary, System.Net.NetworkCredentialclass credentials)Program
{
// set up the src client
static usingvoid Main(SP.ClientContext srcContext = newstring[] SP.ClientContext(srcUrl)args)
{
// set up the destination
using (SP.ClientContextstring destContextsrcUrl = new SP.ClientContext(destUrl))
{"https://aaa/bbb/ccc/";
string srcContext.CredentialsdestUrl = credentials;"https://aaa/bbb/ddd/";;
System.Net.NetworkCredential credentials = new destContextSystem.CredentialsNet.NetworkCredential("aaa", ="ccc", credentials;"vvvv");
// get the source web
SP.Web srcWeb = srcContext.Web;
SP.List srcList =string srcWeb.Lists.GetByTitle(srcLibrary);
// SP.CamlQuery query = SP.CamlQuery.CreateAllItemsQuery(5);
//
SP.CamlQuery query = new SP.CamlQuery();
query.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ICD_x0020_ID' /><Value Type='Text'>6382</Value></Eq></Where></Query></View>";
"MySrc Library";
SP.ListItemCollectionstring srcItemsdestLibrary = srcList.GetItems(query);
srcContext.Load(srcItems);
"MyDest srcContext.ExecuteQuery();Library";
// get the destination web
copy latest SP.Webfull destWebversion =of destContext.Web;file
destContext.LoadCopyDocuments(destWeb);
srcUrl, destUrl, srcLibrary, destLibrary, destContext.ExecuteQuery(credentials);
}
#region Supporting Methods
foreach
public static void CopyDocuments(varstring itemsrcUrl, instring srcItemsdestUrl, string srcLibrary, string destLibrary, System.Net.NetworkCredential credentials)
{
// set up the src client
using (SP.ClientContext srcContext = new SP.ClientContext(srcUrl))
{
// set up the destination
using (SP.ClientContext destContext = new SP.ClientContext(destUrl))
{
trysrcContext.Credentials = credentials;
destContext.Credentials = credentials;
// get the source web
SP.Web srcWeb = srcContext.Web;
SP.List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
// SP.CamlQuery query = SP.CamlQuery.CreateAllItemsQuery(5);
//
SP.CamlQuery query = new SP.CamlQuery();
query.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ICD_x0020_ID' /><Value Type='Text'>6382</Value></Eq></Where></Query></View>";
SP.ListItemCollection srcItems = srcList.GetItems(query);
srcContext.Load(srcItems);
srcContext.ExecuteQuery();
// get the destination web
SP.Web destWeb = destContext.Web;
destContext.Load(destWeb);
destContext.ExecuteQuery();
foreach (var item in srcItems)
{
if (item.FileSystemObjectType == SP.FileSystemObjectType.File)try
{
// get the first file version
SP.File file = item.File;
SP.FileVersionCollection versions = file.Versions;
srcContext.Load(versions);
srcContext.ExecuteQuery();
//SP.FileVersion firstVersion =if versions.OrderBy(L => L.ID)item.FirstOrDefault();
FileSystemObjectType == SP.FileVersion firstVersion = versions.OrderBy(L => L.ID).Last();
//
foreach (SPFileSystemObjectType.FileVersion _version in versionsFile)
{
// get the first file version
SP.File file = item.File;
SP.FileVersionCollection versions = file.Versions;
srcContext.Load(versions);
srcContext.ExecuteQuery();
//SP.FileVersion firstVersion = versions.OrderBy(L => L.ID).FirstOrDefault();
SP.FileVersion firstVersion = versions.OrderBy(L => L.ID).Last();
//
foreach (SP.FileVersion _version in versions)
{
Console.WriteLine("Version :{0}\t", _version.VersionLabel);
}
//
if (destContext.HasPendingRequest)
destContext.ExecuteQuery();
string filePath = Path.Combine(@srcUrl, firstVersion.Url).Replace(" ", "%20");
string fileName = Path.GetFileName(filePath);
// build new location url
string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "%20") + "/" + fileName;
WebClient client = new System.Net.WebClient();
client.Credentials = credentials;
using (Stream fileRef = client.OpenRead(filePath))
{
SP.File.SaveBinaryDirect(destContext, nLocation, fileRef, true);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("File Error = " + ex.ToString());
}
}
}
}
}
}
#endregion
}
}
}