{Sharepoint} How to copy files from one sharepoint site to another

When we need to copy files from one sharepoint server site to another we cannot use .CopyTo.

CopyTo works  only if the root site of the two locations is same. However if the root site is different , we need to read all the files in byte array and create them as new files in the second share-point location. The mentioned below code snippet shows how this can be achieved

public void CopyFromSubmittedToPublishedFolderUpdated(string siteUrl1, string siteURL2, string folderLocationSource, string folderLocationDestination)
{
ExecuteFaultHandledOperation(() =>
{
using (var publishedClientContext = new ClientContext(siteURL2))
using (var clientContext = new ClientContext(siteUrl1))
{
Web site = clientContext.Web;
Web publishedSite = publishedClientContext.Web;
Folder submittedFolder = site.GetFolderByServerRelativeUrl(folderLocationSource);
Folder publishedFolder = publishedSite.GetFolderByServerRelativeUrl(folderLocationDestination);
clientContext.Load<Folder>(submittedFolder);
clientContext.ExecuteQuery();
publishedClientContext.Load<Folder>(publishedFolder);
publishedClientContext.ExecuteQuery();

FileCollection files = submittedFolder.Files;
clientContext.Load<FileCollection>(files);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File f in files)
{
clientContext.Load(f);
clientContext.ExecuteQuery();
FileInformation info = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, f.ServerRelativeUrl);
var memory = new MemoryStream();
string fileUrl = publishedSharepointURL + dealName.Trim() + “/” + sighterRootFolder + “/” + sighterName.Trim() + “/” + publishedFolderRoot + “/” + f.Name;

CopyStream(info.Stream, memory);
var fileCreationInformation = new FileCreationInformation();
var byteRep = memory.ToArray();
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = fileUrl;
fileCreationInformation.Content = byteRep;
Microsoft.SharePoint.Client.File uploadFile = publishedFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
publishedClientContext.ExecuteQuery();

}
}
});
}

 

Advertisements

Calling CRM Reports from .Net Code

On many occasions we need to execute CRM reports from a .Net code component and save the results which are returned as byte array results. The mentioned below code snippet shows how that could be accomplished.

For calling a CRM report we need the following values

  1. CRM Report URL(reportServerURL) – http://{crmreportserver}/ReportServer/ReportService2005.asmx
  2. CRM Report Execution URL (reportExecutionURL) – http://{crmreportserver}/ReportServer/ReportExecution2005.asmx
  3. CRM Report Path – Path of the report which needs to be called.
  4. ReportingServerAdminCredentials – Domain name, Username and password of the user which is having report browsing right on the report being called.

Mentioned below is the code snippet for the same

  • When we call the reporting server we can specify the document type and the orientation¬†in which we want the results to be returned. For example in the below example, we are calling the reporting server and passing document type as word and orientation as landscape.
  • If there is a custom datasource that is declared in the report, we can also pass the corresponding login credentials for the database. In the below example, its shown how that could be achieved.

public byte[] ReadWordFormatFile(string dealID, string reportServerURL, string reportExecutionURL, string reportURL, string dbConnectionName, string username, string password, NetworkCredential reportingCredential)
{
string historyID = null;
string deviceInfo = “<DeviceInfo><OutputFormat>WORD</OutputFormat><Orientation>Landscape</Orientation><PageWidth>11in</PageWidth><PageHeight>8.5in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.25in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>”;

string format = “Word”;
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
string[] streamIDs = null;
RExecutionProxy.Warning[] warnings = null;

// Name of the report – Please note this is not the RDL file.
try
{
ReportingService2005 rs;
ReportExecutionService rsExec;
rs = new ReportingService2005();
rsExec = new ReportExecutionService();
rs.Credentials = reportingCredential;
rsExec.Credentials = reportingCredential;
rs.Url = reportServerURL;
rsExec.Url = reportExecutionURL;
RExecutionProxy.DataSourceCredentials cred = new RExecutionProxy.DataSourceCredentials();
cred.DataSourceName = dbConnectionName;
cred.UserName = username;
cred.Password = password;
RExecutionProxy.ExecutionInfo ei = rsExec.LoadReport(reportURL, historyID);
RExecutionProxy.DataSourceCredentials[] credentials = new RExecutionProxy.DataSourceCredentials[1];
credentials[0] = cred;
rsExec.SetExecutionCredentials(credentials);
rs.Url = reportServerURL;
rsExec.Url = reportExecutionURL;
RExecutionProxy.ParameterValue[] parameters = new RExecutionProxy.ParameterValue[1];
parameters[0] = new RExecutionProxy.ParameterValue();
parameters[0].Name = “DealID”;
parameters[0].Value = dealID;
rsExec.SetExecutionParameters(parameters, “en-us”);
results = rsExec.Render(format, deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);

return results;
}
catch (Exception ex)
{
LogDispatcher.Logger.Log(ex);
return null;
}}