Plug-In that Converted Note entity pre-existing attachment XML file into new .MMP file - plugins

strong text [Plugin error at Note entity][1]
[1]: http://i.stack.imgur.com/hRIi9.png
Hi,Anyone resolved my issue i got a Plug-in error which i worked at Update of "Note" entity.Basically i want a Plugin which converted pre-exiting Note attachment XML file into new .MMP extension file with the same name.
I have done following procedure firstly i created a "Converter_Code.cs" dll which contains Convert() method that converted XML file to .MMP file here is the constructor of the class.
public Converter(string xml, string defaultMapFileName, bool isForWeb)
{
Xml = xml;
DefaultMapFileName = defaultMapFileName;
Result = Environment.NewLine;
IsForWeb = isForWeb;
IsMapConverted = false;
ResultFolder = CreateResultFolder(MmcGlobal.ResultFolder);
}
In ConvertPlugin.cs Plug-in class firstly i retrieved Note entity attachment XML file in a string using following method in
IPluginExecutionContext context =(IPluginExecutionContext)serviceProvider.
GetService (typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory= (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService
(context.UserId);
if (context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
var annotationid = entity.GetAttributeValue<Guid>("annotationid");
if (entity.LogicalName != "annotation")
{
return;
}
else
{
try
{
//retrieve annotation file
QueryExpression Notes = new QueryExpression { EntityName ="annotation"
,ColumnSet = new ColumnSet("filename", "subject", "annotationid",
"documentbody") };
Notes.Criteria.AddCondition("annotationid", ConditionOperator.Equal,
annotationid);
EntityCollection NotesRetrieve = service.RetrieveMultiple(Notes);
if (NotesRetrieve != null && NotesRetrieve.Entities.Count > 0)
{
{
//converting document body content to bytes
byte[] fill = Convert.FromBase64String(NotesRetrieve.Entities[0]
.Attributes["documentbody"].ToString());
//Converting to String
string content = System.Text.Encoding.UTF8.GetString(fill);
Converter objConverter = new Converter(content, "TestMap", true);
objConverter.Convert();
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("something is going wrong");
}
}
}
}
and than A string is passed to "Converter" constructor as a parameter.
finally i merged all dll using ILMerge following method:
ilmerge /out:NewConvertPlugin.dll ConvertPlugin.dll Converter_Code.dll
and NewConvertPlugin is registered successfully but while its working its generate following error:
Unexpected exception from plug-in (Execute): ConverterPlugin.Class1: System.Security.SecurityException: That assembly does not allow partially trusted callers.
i also debug the plugin using Error-log but its not worked so i could not get a reason whats going wrong.

The error is caused by the library you merged inside your plugin.
First of all you are using CRM Online (from your screenshot) and with CRM Online you can use only register plugins inside sandbox.
Sandbox means that your plugins are limited and they run in a partial-trust environment.
You merged an external library that requires full-trust permissions, so your plugin can't work and this is the reason of your error.
Because you are in CRM Online, or you find another library (the Converter) that requires only partial-trust, hoping that the merge process will work, or you include (if you have it) the source code of the converter library directly inside your plugin.

Related

How to display local image as well as resources image in .Net MAUI Blazor

In .Net MAUI Blazor I can use an img tag to display an image from wwwroot folder. But how to display an image from the device's internal storage? And how to display images from application resources?
From Internal storage
We can read it into bytes and convert it to base64 string , then show on img tag .
Giving that we've put an image called dog.png in FileSystem.CacheDirectory folder.
Sample code
#if (imageSource is not null)
{
<div>
<img src="#imageSource" width="200" height="200" />
</div>
}
#code {
private string? imageSource;
protected override void OnInitialized()
{
var newFile = Path.Combine(FileSystem.CacheDirectory, "dog.png");
var imageBytes = File.ReadAllBytes(newFile);
imageSource = Convert.ToBase64String(imageBytes);
imageSource = string.Format("data:image/png;base64,{0}", imageSource);
}
}
To display from resource, see … Blazor Hybrid static Files / .Net Maui:
Add file to project, in a folder named Resources/Raw.
Make sure file / Properties / Build Action = MauiAsset.
Create a razor component that:
Calls Microsoft.Maui.Storage.FileSystem.OpenAppPackageFileAsync to obtain a Stream for the resource.
Reads the Stream with a StreamReader.
Calls StreamReader.ReadToEndAsync to read the file.
Example razor code (from that link):
#page "/static-asset-example"
#using System.IO
#using Microsoft.Extensions.Logging
#using Microsoft.Maui.Storage
#inject ILogger<StaticAssetExample> Logger
<h1>Static Asset Example</h1>
<p>#dataResourceText</p>
#code {
public string dataResourceText = "Loading resource ...";
protected override async Task OnInitializedAsync()
{
try
{
using var stream =
await FileSystem.OpenAppPackageFileAsync("Data.txt");
using var reader = new StreamReader(stream);
dataResourceText = await reader.ReadToEndAsync();
}
catch (FileNotFoundException ex)
{
dataResourceText = "Data file not found.";
Logger.LogError(ex, "'Resource/Raw/Data.txt' not found.");
}
}
}
To access local file (not an asset in resources) from razor code, you’ll need a service that given the file name (or relative path), returns the file contents as a stream.
I’m not finding a doc saying how to do that for Maui, then inject that into razor code.
Such a service would use .Net File System Helpers to access the file. This would be similar to the MauiAsset example above, but using one of the path helpers, NOT calling OpenAppPackageFileAsync.
TBD - someone give reference link or example?
From my research :
You can actually get the path of the wwwroot folder in the razor application with : AppDomain.CurrentDomain.BaseDirectory.
In windows you can add files in this folder that will be accessible from the Blazor HTML. However, in Android the wwwroot folder is embeded in the app and will not be accessible (AppDomain.CurrentDomain.BaseDirectory return a empty folder).
After looking on the .NET MAUI github repo in the BlazorWebView class I found :
public virtual IFileProvider CreateFileProvider(string contentRootDir)
{
// Call into the platform-specific code to get that platform's asset file provider
return ((BlazorWebViewHandler)(Handler!)).CreateFileProvider(contentRootDir);
}
Which can be used to pass files to Blazor. For exemple if you want to make accessible all the files from the AppDataDirectory :
public class CustomFilesBlazorWebView : BlazorWebView
{
public override IFileProvider CreateFileProvider(string contentRootDir)
{
var lPhysicalFiles = new PhysicalFileProvider(FileSystem.Current.AppDataDirectory);
return new CompositeFileProvider(lPhysicalFiles, base.CreateFileProvider(contentRootDir));
}
}
Then in MainPage.xaml :
<local:CustomFilesBlazorWebView HostPage="wwwroot/index.html" x:Name="WebView">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</local:CustomFilesBlazorWebView>
For exemple if in AppDataDirectory you have a file images/user.png in any Blazor component you can use :
<img src="images/user.png" />
I solved in this way.
Add the png image to Resources\Raw and set to MauiAsset compilation type
Check the project file to avoid that the image is excluded via ItemGroup-> None Remove. In this case delete the ItemGroup related to the image.
After this:
In my razor component HTML
<img src="#imageSource">
In the code part:
private string? imageSource;
protected override async Task OnInitializedAsync()
{
try
{
using var stream =
await FileSystem.OpenAppPackageFileAsync("testimage.png");
using var reader = new StreamReader(stream);
byte[] result;
using (var streamReader = new MemoryStream())
{
stream.CopyTo(streamReader);
result = streamReader.ToArray();
}
imageSource = Convert.ToBase64String(result);
imageSource = string.Format("data:image/png;base64,{0}", imageSource);
}
catch (Exception ex)
{
//log error
}
}

Write CRM Plugin that Fires when a Field is updated

We're on CRM 2013 on-premise. I'm writing a plugin that fires when a field on Quote entity is updated.
So I registered my plugin on 'Update' message. Then the event is 'Post-operation'. (I tried Pre-operation but still no luck)
Basically the goal is when the field is updated, create a new entity 'ContractShell' and then create relationship between the Quote and the newly created 'ContractShell'.
However my problem is when the field is updated, my plugin never seems to fire. I just simply put a InvalidPluginExecutionException in my code, but for some reason it never fires.... Any ideas? Thanks.
Here's a screenshot of my plugin step:
Here's my code:
var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// The InputParameters collection contains all the data passed in the message request.
var targetEntity = context.GetParameterCollection<Entity>(context.InputParameters, "Target");
if (targetEntity == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");
if (!context.OutputParameters.Contains("id"))
return;
Guid QuoteId = (Guid)targetEntity.Attributes["quoteid"];
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
var contractShellEntity = new Entity();
contractShellEntity = new Entity("new_);
//assign the portfolio
if (targetEntity.Attributes.Contains(Schema.Quote.Portfolio))
{
var quotePortfolio = (EntityReference)targetEntity.Attributes[Schema.Quote.Portfolio];
contractShellEntity[Schema.new_ContractShell.PortfolioName] = new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
}
var contractShellId = service.Create(contractShellEntity);
throw new InvalidPluginExecutionException(OperationStatus.Failed, "I created New Contract Shell");
//Creating relationship between Contract Shell and the newly created Accounts
var quoteContractReferenceCollection = new EntityReferenceCollection();
var quoteContractRelatedEntity = new EntityReference
{
Id = contractShellId,
LogicalName = contractShellEntity.LogicalName
};
quoteContractReferenceCollection.Add(quoteContractRelatedEntity);
var quoteContractReferenceCollectionRelRelationship = new Relationship
{
SchemaName = Schema.new_ContractShell.ContractQuoteRelationship
};
service.Associate("quote", QuoteId, quoteContractReferenceCollectionRelRelationship, quoteContractReferenceCollection);
You need to register not only the plugin but an SDKMessageProcessingStep. Also, you have to implement the Execute method in your plugin to be able to register it, so either you're missing code in your snippet, or your code is the problem.
Also, your InvalidPluginExecutionException is nested after a number of checks. Theres a good chance you don't have any output parameters if you don't know how to register a plugin, so your code would actually return before you hit the exception.

CRM 2011 - Plug-In - Operation Timed-Out error while trying to do a HTTP Post in a Plug-in

I'm having problems when I try to do a HTTP Post in my Plugin (in PostUpdate). I'm getting the "The Operation Has Timed Out"-Error...
Here below you have the C#-code :
//PUBLISH TO ROBAROV
WebRequest webRequest = WebRequest.Create(newUri);
webRequest.Timeout = 2000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
Stream os = null;
try
{
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
finally
{
if (os != null)
{
os.Close();
}
}
//ERROR HAPPENS HERE
string responseText = "";
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
responseText = sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
throw new Exception("Error with response : " + ex.Message);
}
The error happens when I'm trying to get the response => webRequest.GetResponse();!
I've tried the code out in a simple "Class"-library and there it works like a charm! Is there something I'm doing wrong? The HTTP Post is to a webpage that's not in the same domain....
UPDATE :
Same happens when I do the following with a webclient... And it works in a normal "Console"-application :
private string HttpPostTest(string URL)
{
WebClient webClient = new WebClient();
System.Collections.Specialized.NameValueCollection formData = new System.Collections.Specialized.NameValueCollection();
formData["state"] = "yes";
byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
string Result = Encoding.UTF8.GetString(responseBytes);
return Result;
}
I'm getting the following error in the "Event Viewer" :
Inner Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Crm.Setup.DiffBuilder, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
It looks like it can't find a CRM assembly: Microsoft.Crm.Setup.DiffBuilder.dll, is this something which you explicitly call methods from? If so I'd check if the assembly is registered with the plug-in (some instructions below). If not then there are some errors associated with this library from roll up 6, which roll up are you using? You may consider roll up 7 if you are not using that.
Is your plug-in registered in the database or on disk?
If registered on disk then you will need your external assembly in the /server/bin/assembly directory under the CRM installation folder.
If it is registered in the database and you are including a custom external assembly (the error suggests that an assembly cannot be loaded, so this sounds possible), then you will have to ILMerge your assemblies before registering them in the database. This would explain why it works for your local console application and not when run as a plug-in.
If this is the case then you can follow a script like below to ILMerge and register your 'combined' assembly:-
http://www.2wconsulting.com/2010/11/using-ilmerge-with-crm-plugin-assemblies/

The given key was not present in the dictionary

I am developing a plugin in crm 5.0 to retrieve date "ct_valuedate" from an entity called "ct_marketvalue" and formatting and saving in a field called "ct_dateserial"
I get an error while I debug "The given key was not present in the dictionary"
public class MarketValueDateFormatting : PluginBase
{
protected override void ExecutePlugin()
{
try
{
switch (_crmMessage)
{
case CrmPluginMessageEnum.Create:
if (_context.InputParameters.Contains("ct_marketvalue"))
{
//Obtain the logical name of the entity
string moniker1 = ((EntityReference)_context.InputParameters["EntityMoniker"]).LogicalName;
//Verify that the target entity represents an Account.
//If not, this plug-in was not registered correctly.
if (moniker1.Equals("ct_marketvalue"))
{
Entity marketvalueimage = (Entity)_context.PostEntityImages["ct_marketvalue"];
Guid marketvalueid = marketvalueimage.Id;
if (marketvalueimage.Contains("ct_valuedate"))
{
DateTime dateserial = (DateTime)marketvalueimage.Attributes["ct_valuedate"];
String dateserialstring = dateserial.ToString("YYYYMMdd");
Ct_marketvalue marketvalue = new Ct_marketvalue();
marketvalue.Ct_dateserial = dateserialstring;
marketvalue.Id = marketvalueid;
_serviceContext.UpdateObject(marketvalue);
}
}
}
break;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Few notes about your code.
You should check in your code that _context.PostEntityImages contains "ct_marketvalue". It's possible either to forgot register or to do a mistake in image name.
Might be better use .ToEntity rather than access attributes using .Attributes["ct_valuedate"].
I'm not sure what is purpose of the plugin you wrote, but it looks it is post stage plugin and it updates the same entity instance, that was in InputParameters. Might be better to make this plugin pre stage and update value directly in InputParameters. Because, if not "The given key was not present in the dictionary" exception, it will cause infinite loop. You will need check context.Depth.

Reading from an assembly with embedded resources

I built an assembly containing one js file.
I marked the file as Embedded Resource and added it into AssemblyInfo file.
I can't refernce the Assembly from a web site. It is in the bin folder but I don't see the reference to it.
It seems like not having at least a class inside the assembly I can't reference it.
I would include the js file into my pages from the assembly.
How should I do this?
Thanks
I do exactly the same thing in one of my projects. I have a central ScriptManager class that actually caches the scripts as it pulls them, but the core of extracting the script file from the embedded resource looks like this:
internal static class ScriptManager
{
private static Dictionary<string, string> m_scriptCache =
new Dictionary<string, string>();
public static string GetScript(string scriptName)
{
return GetScript(scriptName, true);
}
public static string GetScript(string scriptName, bool encloseInCdata)
{
StringBuilder script = new StringBuilder("\r\n");
if (encloseInCdata)
{
script.Append("//<![CDATA[\r\n");
}
if (!m_scriptCache.ContainsKey(scriptName))
{
var asm = Assembly.GetExecutingAssembly();
var stream = asm.GetManifestResourceStream(scriptName);
if (stream == null)
{
var names = asm.GetManifestResourceNames();
// NOTE: you passed in an invalid name.
// Use the above line to determine what tyhe name should be
// most common is not setting the script file to be an embedded resource
if (Debugger.IsAttached) Debugger.Break();
return string.Empty;
}
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
m_scriptCache.Add(scriptName, text);
}
}
script.Append(m_scriptCache[scriptName]);
if (encloseInCdata)
{
script.Append("//]]>\r\n");
}
return script.ToString();
}
}
EDIT
To provide more clarity, I've posted my ScriptManager class. To extract a script file, I simply call it like this:
var script = ScriptManager.GetScript("Fully.Qualified.Script.js");
The name you pass in it the full, case-sensitive resource name (the exception handler gets a list of them by calling GetManifestResourceNames()).
This gives you the script as a string - you can then put it out into a file, inject it into the page (which is what I'm doing) or whatever you like.
Assembly myAssembly = // Get your assembly somehow (see below)...
IList<string> resourceNames = myAssembly.GetManifestResourceNames();
This will return a list of all resource names that have been set as 'Embedded Resource'. The name is usually the fully qualified namespace of wherever you put that JS file. So if your project is called My.Project and you store your MyScript.js file inside a folder in your project called Resources, the full name would be My.Project.Resources.MyScript.js
If you then want to use that JS file:
Stream stream = myAssembly.GetManifestResourceStream(myResourceName);
Where myResourceName argument might be "My.Project.Resources.MyScript.js". To get that JS file in that Stream object, you'll need to write it as a file to the hard drive, and serve it from your website as a static file, something like this:
Stream stream = executingAssembly.GetManifestResourceStream(imageResourcePath);
if (stream != null)
{
string directory = Path.GetDirectoryName("C:/WebApps/MyApp/Scripts/");
using (Stream file = File.OpenWrite(directory + "MyScript.js"))
{
CopyStream(stream, file);
}
stream.Dispose();
}
And the code for CopyStream method:
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
You might want to stick all this code in an Application_Start event in your Global.asax. You don't want it to run for each request
Now getting a reference to your Assembly is a different matter, there are many ways. One way is to include all the above code in your Assembly in question, then make sure you reference that Assembly from your main WebApp project in Visual Studio, then get a reference to the currently executing Assembly like so.
namespace My.Project
{
public class ResourceLoader
{
public static void LoadResources()
{
Assembly myAssembly = Assembly.GetExecutingAssembly();
// rest of code from above (snip)
}
}
}
Then call ResourceLoader.LoadResources() from your Application_Start event in your Global.asax.
Hope this helps
Fully working example (I hope):
namespace TestResources.Assembly
{
public class ResourceLoader
{
public static void LoadResources()
{
Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream stream = myAssembly
.GetManifestResourceStream("TestResources.Assembly.CheckAnswer.js");
if (stream != null)
{
string directory = Path.GetDirectoryName("C:/WebApps/MyApp/Scripts/");
using (Stream file = File.OpenWrite(directory + "MyScript.js"))
{
CopyStream(stream, file);
}
stream.Dispose();
}
}
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}
Some caveats:
Change "C:/WebApps/MyApp/" to wherever your web app is located, maybe write some code to work this out dynamically
Make sure the /Scripts folder exists in your webapp root
I think it will overwrite the 'MyScript.js' file if it already exists, but just in case you might want to add some code to check for that file and delete it
Then stick a call to this code in your Global.asax file:
protected void Application_Start()
{
ResourceLoader.LoadResources();
}
Then the path for your web site will be /Scripts/MyScript.js eg:
<head>
<!-- Rest of head (snip) -->
<script type="text/javascript" src="/Scripts/MyScript.js"></script>
</head>