{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;
}}

 

{New utility}Activity Timeline for Dynamics CRM 2015

An activity timeline is a chart that depicts how a set of activities were planned over

you’re managing a Sales pipeline and want to illustrate who is doing what and when, or if

you’re organizing a conference and need to show progress, a timeline is often a reasonable

visualization choice.

Sales manager wants to see activities scheduled by the team and see the progress over

time. Also he can plan the future activities after looking at this visualization.

Once planned, can view the future activities and adjust overlapping ones, which is difficult to

monitor on a Grid.

Mentioned below is the youtube link for the same.

Download link: http://1drv.ms/1MMYbQB

  1. Open to the CRM organization and navigate to Settings->Solutions.
  2. Now click on import solution and select the managed solution of activity timeline control.
  3. Now click on next and install the solution. Review that the solution installs without any error.

img1 img2

  1. Now open the entity form on which you want to enable the time line control. Add a new tab and in the tab, add a web resource pointing to “dya_timelinecontrol”

img3

 

  1. For better visual representation, set the number of rows property to 18.

img4

Save and publish the customizations.

  1. Open the form to see the web resource.

img5

7.   Hover over the dot symbol to view the activities on that date. As per the color coding there will be various activities. If you click on any of the row, it will redirect to the respective activity.

img6

img7

img8

 

Adding Localization in CRM 2013 USD

The blog explains how to implement localization in custom made hosted controls of USD. For implementing localization we need to follow the below mentioned steps

1. Create a project and add all the required the required strings or messages to be localized as resource files. Following screenshot describes the implementation.

ResourceProject

The name should of the format Strings.{lang code}. For example for English it should be Strings.en-US.resx and for portugal it should be Strings.pt-BR.resx. Create a resource file for neutral culture as well.

2. Now create another project. The purpose of this project is to identify dynamically the current culture profile of the user, load the corresponding resource dll and then to translate the text passed to it. You need to add the reference of the project created in first step. Mentioned below is the pseudo code of the classes used in it.

a) Create an interface ITranslationProvider with the mentioned below declaration.

ITranslationProvider

b) Firstly class should implement the interface “ITranslationProvider” and should have a member variable of the above created interface. It should also have a method to return the translated text as per the loaded resource assembly. Mentioned below is the screenshot for the same.

 

TranslationManager

 

Refer below for the constructor and the translation function.

private static TranslationManager _translationManager;

private ITranslationProvider translationProvider;

/// <summary>
/// Gets or sets the translation provider.
/// </summary>
/// <value>The translation provider.</value>
public ITranslationProvider TranslationProvider
{
get
{
return this.translationProvider;
}
set
{
this.translationProvider = value;
}
}

/// <summary>
/// Gets or sets the current language.
/// </summary>
/// <value>The current language.</value>
public CultureInfo CurrentLanguage
{
get
{
return Thread.CurrentThread.CurrentUICulture;
}
set
{
if (value != Thread.CurrentThread.CurrentUICulture)
{
Thread.CurrentThread.CurrentUICulture = value;
}
}
}

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static TranslationManager Instance
{
get
{
if (_translationManager == null)
_translationManager = new TranslationManager();

return _translationManager;
}
}

/// <summary>
/// Translates the specified key.
/// </summary>
/// <param name=”key”>The key.</param>
/// <returns></returns>
public object Translate(string key)
{
if (TranslationProvider == null)
{
TranslationManager.Instance.TranslationProvider = new ResourceTranslationProvider(“Cielo.USD.LocalizationResources.Resources.Strings”, typeof(LocalizationResources.Resources.Strings).Assembly);
string value = System.Configuration.ConfigurationManager.AppSettings[“LanguageCulture”];
if(value == null || value == “”)
{
value = “pt-br”;
}

TranslationManager.Instance.CurrentLanguage = new System.Globalization.CultureInfo(value);
}

if (TranslationProvider != null)
{
object translatedValue = TranslationProvider.Translate(key);
if (translatedValue != null)
{
return translatedValue;
}
}
return string.Format(“{0}”, key);
}
}

Refer to the name of the localization resource project assembly dll, which is used wile making the object of TranslationProvider.

c) Now create a new file TranslationExtention that should invoke the above mentioned class for translation

public static class TranslationExtensions
{
/// <summary>
/// Converts a string to the localized string.
/// </summary>
/// <param name=”sourceString”>The source string.</param>
/// <returns></returns>
public static string ToLocalizedString(this string sourceString)
{
object obj = TranslationManager.Instance.Translate(sourceString);
if (obj == null)
{
return sourceString;
}
else
{
return obj.ToString();
}
}
}

d) Similar files needs to be made for xaml conversion as well.

e) Build the above mentioned project and add the required dll’s in all the hosted control projects. Now use the below mentioned code snippet for implementing translation in both cs and xaml files.

XamlAssembly

XamlAssemblyTextLabel

“SalesProcedureText” is the key of the text label defined in resource file.

CSTextLabel